I'm unfamiliar to working in c++ under linux so I have some issues, one of them is that after I write a class and try to instantiate an object of that class I get to following error : "undefined reference to Constructor_of_that_class" . This happens for every class that I write , and it happens no matter where I try to do the instantiating, even if the code compiles without any problems .开发者_如何学Python What is going wrong here and what I have to do to get over this error ? Because the project that I'm working wasn't created by me I suspect that it has to do something with some settings but I don't know which.
Edit (pasted from comment):
Well if I define a class this:
class test {
public:
int a*;
test( int* a)
}
class test {
test::test( int* a)
{
this->a=a;
}
}
and then in any class of those who where previously defined I use:
test t =new test( anIntPointer);
then I get a undefined reference to test::test(int*)
;
I would be surprised if your code sample even compiles, so fixing all other compilation errors first would be a good start. Here is a short code sample that might help:
// Class declaration
class test
{
private:
// Member variable - should be private.
int* a;
public:
// Constructor declaration.
test(int* a);
// Inline function definition.
int getA()
{
return *a;
}
};
// Constructor declaration.
test::test( int* a)
{
this->a=a;
}
int main()
{
int i = 7;
test t(&i);
i++;
// Should return 8.
return t.getA();
}
Without code its impossible to tell, but make sure that your class definitions end with a semi-colon;
Do this:
test.h
class Test {
public:
int a*;
Test( int *a );
}; //Missing this semi colon might be your problem
test.cpp
#include "test.h"
Test::Test( int *a )
{
this->a = a;
}
int main()
{
int *anIntPointer;
Test t = new Test( anIntPointer );
return 0;
}
Don't wrap the constructor definition (the test::test()
function) inside a class test
block. That effectively defines a new class with the same name but it's different from the one in your header. Make it look like this:
// .h file
class test {
public:
int *a;
test( int* a)
};
// .cpp file
test::test( int* a)
{
this->a=a;
}
You should provide some code of one of your class (definition + implementation) if you want a better answer.
With the minimal explaination you provide, I think your constructor have no implementation.
Try this:
foo.h
class test {
public:
int a*;
test( int* a)
};
foo.cpp
test::test( int* a)
{
this->a=a;
}
Semantics aside from the comment I made above (I'm a little low on the whole blood sugar), you state that you're instantiating test
thus:
test t =new test( anIntPointer);
the new
operator returns a pointer to the object, not the object itself - you should be instantiating it:
test *t = new test(anIntPointer);
(and, going back to semantics, convention for C++ classes is a capitalised first letter, I believe :-) )
Your posted class definition is syntactically invalid. A correct equivalent would be:
class test {
public:
int *a;
test (int *a) : a(a) {}
};
Does this compile?
精彩评论