Hi,
can anybody help me ?
I have a code Index which gave error message "Bus error (core dumped)" in the line push_back() function was called
as shown as following:
Index::Index(string fname) : vector<CatPair>(0), _range(vector<CatPair::index_type>(0))
{
ifstream is(fname.c_str());
if(is.fail()){
cerr << "Cannot access " + fname << endl;
exit(1);
}
while(is.good()){
CatPair cp;
cp.read(is);
if(is.eof())
continue;
push_back(cp);
//here I got the error (Bus error, core dumped);
}
is.close();
if(size() == 0) cout << "No events" << endl;
}
CatPair is a class as shown as following,
// this is CatPair.cc file
#include "CatPair.h"
#include <iostream>
using namespace std;
bool CatPair:perator<(const CatPair& rhs) const
{
return second < rhs.second;
}
void CatPair:rint(ostream& os) const
{
os << first << " " << second;
}
void CatPair::write(ostream& os) const
{
os.write(reinterpret_cast<const char*>(&first), sizeof(first));
os.write(reinterpret_cast<const char*>(&second), sizeof(second));
}
void CatPair::read(istream& is)
{
cout << " first=" << sizeof(first) << endl;
cout << " second=" << sizeof(second) << endl;
is.read(reinterpret_cast<char*>(&first), sizeof(first));
is.read(reinterpret_cast<char*>(&second), sizeof(second));
}
and the header file (CatPair.h)is following,
// this is CatPair.h file
#ifndef CATPAIR_H
#define CATPAIR_H
#include <string>
#include <iostream>
#include <utility>
class CatPair : public std:air<unsigned int, std::string>
{
public:
CatPair(unsigned int f = 0, std::string s = " ") : std:air<unsigned int,std::string>(f,s) { }
CatPair(std::string s) : std:air<unsigned int,std::string>(0,s) { }
bool operator<(const CatPair& rhs) const;
void print(std:stream& os) const;
void write(std:stream& os) const;
void read(std::istream& os);
typedef unsigned int index_type;
typedef double value_type;
};
inline std:stream& operator<<(std:stream& os, const CatPair& p)
{
p.print(os); return os;
}
#endif
The Index Code works if I define the CatPair as a pair(int, double), but code gave error if I define the CatPair as a pair(int, string).
It is very appreciate if anybody can hive me a hand.
Thank you very much !!
Victor
can anybody help me ?
I have a code Index which gave error message "Bus error (core dumped)" in the line push_back() function was called
as shown as following:
Index::Index(string fname) : vector<CatPair>(0), _range(vector<CatPair::index_type>(0))
{
ifstream is(fname.c_str());
if(is.fail()){
cerr << "Cannot access " + fname << endl;
exit(1);
}
while(is.good()){
CatPair cp;
cp.read(is);
if(is.eof())
continue;
push_back(cp);
//here I got the error (Bus error, core dumped);
}
is.close();
if(size() == 0) cout << "No events" << endl;
}
CatPair is a class as shown as following,
// this is CatPair.cc file
#include "CatPair.h"
#include <iostream>
using namespace std;
bool CatPair:perator<(const CatPair& rhs) const
{
return second < rhs.second;
}
void CatPair:rint(ostream& os) const
{
os << first << " " << second;
}
void CatPair::write(ostream& os) const
{
os.write(reinterpret_cast<const char*>(&first), sizeof(first));
os.write(reinterpret_cast<const char*>(&second), sizeof(second));
}
void CatPair::read(istream& is)
{
cout << " first=" << sizeof(first) << endl;
cout << " second=" << sizeof(second) << endl;
is.read(reinterpret_cast<char*>(&first), sizeof(first));
is.read(reinterpret_cast<char*>(&second), sizeof(second));
}
and the header file (CatPair.h)is following,
// this is CatPair.h file
#ifndef CATPAIR_H
#define CATPAIR_H
#include <string>
#include <iostream>
#include <utility>
class CatPair : public std:air<unsigned int, std::string>
{
public:
CatPair(unsigned int f = 0, std::string s = " ") : std:air<unsigned int,std::string>(f,s) { }
CatPair(std::string s) : std:air<unsigned int,std::string>(0,s) { }
bool operator<(const CatPair& rhs) const;
void print(std:stream& os) const;
void write(std:stream& os) const;
void read(std::istream& os);
typedef unsigned int index_type;
typedef double value_type;
};
inline std:stream& operator<<(std:stream& os, const CatPair& p)
{
p.print(os); return os;
}
#endif
The Index Code works if I define the CatPair as a pair(int, double), but code gave error if I define the CatPair as a pair(int, string).
It is very appreciate if anybody can hive me a hand.
Thank you very much !!
Victor