PDA

View Full Version : Eclipse CDT Inheritance "undefined reference to" base class contructor problem



ddili
April 4th, 2009, 11:57 PM
hey all!

i've been trying to fix this problem for some time now. i'm working with public inheritance and i'm trying to achieve this model: (functions are just for testing purposes)

testBase/base.h

#ifndef __BASE_H__
#define __BASE_H__

#include <iostream>
#include <string>

using namespace std;

class base
{
public:
base( void );
virtual int base1( int );
virtual void base2( int, int );
virtual string base3( int );
virtual void base4( int, string );
};

#endif

testBase/base.cpp

#include "base.h"

base::base(){ }

int base::base1( int i )
{
cout << "base1: " << i << endl;

return ++i;
}

void base::base2( int i, int j )
{
cout << "base2: " << i << " " << j << endl;
}

string base::base3( int i )
{
cout << "base3: " << i << endl;

return "1";
}

void base::base4( int i, string j )
{
cout << "base4: " << i << " " << j << endl;
}
testDerived/derived.h:

#ifndef __DERIVED_H__
#define __DERIVED_H__

#include "../testBase/base.h"

using namespace std;

class derived: public base
{
public:
derived();
~derived();
void derived1( int );
private:
int _var1;
bool _var2;
int* _var3;
};

#endif
testDerived/derived.cpp

#include "derived.h"

derived::derived( void )
: base(), _var1( 0 ), _var2( false ), _var3( new int( 3 ) )
{}

derived::~derived( void )
{
delete _var3;
}

void derived::derived1( int i )
{
base1( 1 );
base2( 2, 3 );
base3( 4 );
base4( 5, "6" );

cout << "derived1: " << i << endl;
}
testDerived/main.cpp

#include "derived.h"

using namespace std;

int main()
{
derived d;

d.derived1( 1 );

return 0;
}

and here's how my eclipse workspace looks like:

workspace/

testBase/

base.h
base.cpp



testDerived/

derived.h
derived.cpp
main.cpp



now if i try to to compile this using g++ in the command line it works fine:
$ g++ testBase/base.cpp testDerived/derived.cpp testDerived/main.cpp

however when i try to build it in Eclipse i get an error in the derived class' constructor:
undefined reference to `base::base()'

i tried adding the base directory as an include in the derived's C++ project build settings but still nothing. i am at a loss of how to fix this problem. any help at would be very much apreciated!

thanks!

amittalkin
October 19th, 2010, 01:26 PM
hello

i am new to eclipse and i have a similar error. it works fine when i write my own makefile and try to compile it with g++ and run it.

but when i do the same thing with eclipse it gives me "undefined reference to ....." error.

can some one help

please this is my project and it is nearing my deadline....

anyone please please help me.....

thnx in advance

amittalkin
October 19th, 2010, 01:33 PM
hello any one there!!

dwhitney67
October 19th, 2010, 02:28 PM
I'm here, and even though I have been programming in C++ for over a decade, I do not, and will not ever, use Eclipse.

So, since it seems that the issues you are experiencing are Eclipse problems, could you at least post the output generated by Eclipse that shows what it is compiling and then linking together. Please make sure you post the information within CODE tags.

amittalkin
October 19th, 2010, 06:47 PM
@dwhitney67 (http://ubuntuforums.org/member.php?u=322753)

thanks for ur response

my problem was due to importing header(and its implementation) files written in C lang.
anyway..i found a wayout. i was inspecting some header files and some of them contained code like:


#ifdef __cplusplus
extern "C" {
#endif

<<<<<<<<<rest of the header>>>>>>>>>>>>>>>

#ifdef __cplusplus
}
#endif


and i inserted this into my header files
and this solved my problem..

thanks once again @dwhitney67 for ur effort.

amittalkin
October 19th, 2010, 07:05 PM
i guess thats something to do with Name mangling...not sure!!
can any one explain what does it have to do...??

thnks in advance.