Hi,
I have a problem with inheritance. It's the first time i use inheritance but i thought i understood it...
I get the following error when i try to compile the code.
/tmp/ccdOBxMZ.o(.text+0x2b): In function `main':
: undefined reference to `child::child()'
/tmp/ccdOBxMZ.o(.text+0x3d): In function `main':
: undefined reference to `child::~child()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
I have a mother class,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
class mother{
public:
mother::mother(TString);
mother::~mother();
protected:
ifstream filetoread;
TString filename;
vector<double> weight;
vector<double> werror;
};
mother::mother(TString filename){
//weight.push_back(0.8);
//weight.push_back(0.7);
//werror.push_back(0.1);
//werror.push_back(0.3);
double wei,err;
//TString filename = "table.txt";
TString aline="";
filetoread.open(filename);
if(!filetoread){
cerr << "FILE COULD NOT BE OPENED!"<<endl;
}
while(aline.ReadLine(filetoread)){
istringstream istr(aline.Data());
istr >> wei >> err;
weight.push_back(wei);
werror.push_back(err);
}
}
mother::~mother(){}
|
and a child class which inherits mother
1 2 3 4 5 6 7 8 9 10 11
|
class child : public mother{
public:
child();
~child();
child child::vary(child);
void proout();
};
|
1 2 3 4 5 6 7 8 9 10 11
|
child::child() : ("table.txt"){}
child::~child(){}
child child::vary(child Kind){
for(int i=0; i < Kind.weight.size() ; i++){
Kind.weight.at(i) = Kind.weight.at(i)+ Kind.werror.at(i)/2 ;
}
return Kind;
}
void child::proout(){
cout << weight.at(0) << endl;
}
|
I just post all the code, so you see that i actually did define my functions.
It all kinda worked. I just had a problem with the constructors because the compiler had no argument for mother when calling the child constructor. So i looked up the solution and tried what i wrote above:
|
( child::child() : ("table.txt"){} )
|
That sadly doesn't seem to work. Maybe i have another problem.
Oh Sorry i forgot the main function. It doesn't do anything except creating an child object:
There is something which is really strange.
When i create the object with
it works, but i get errors when i try to use the functions on Kind:
1 2
|
Kind.proout();
Kind=Kind.vary(Kind);
|
errors:
main.cc: In function `int main()':
main.cc:19: request for member `proout' in `Kind()', which is of non-aggregate
type `child ()()'
main.cc:20: request for member `vary' in `Kind()', which is of non-aggregate
type `child ()()'
make: *** [all] Error 1
I really appreciate your help.
Thank you very much for reading my wall of text.
Alex