0

Header file :

class FakeMas
{
   FakeMas();
   ~FakeMas();
}

CPP file:

FakeMas::FakeMas(){........}
FakeMas::~FakeMas(){}

I write a cpp file to test the FakeMas:

class FakeMasTest{
    void test() 
    {
        FakeMas fakemas;            
        fakemas.start();
    }
}

the classes in the project called "zinc.testwebserver"

If I build it it says

FakeMasTest.cpp:29: undefined reference to `zinc::testwebserver::FakeMas::FakeMas()'"

If I change the test to:

FakeMas::FakeMas * fakemas = new FakeMas::FakeMas();

it says

error: ‘fakemas’ was not declared in this scope

If I change the test to:

FakeMas * fakemas = new FakeMas();

it says

undefined reference to `zinc::testwebserver::FakeMas::FakeMas()'

I really do not know how to do it...

get any ideas?

Thanks

5
  • Apart from not linking your implementation properly, just use FakeMas fakeMas;. Commented Jan 9, 2013 at 16:39
  • Please show your compile and link command lines, or makefile if you have one. Commented Jan 9, 2013 at 16:42
  • Do you include header with FakeMas into cpp file with FakeMasTest? Commented Jan 9, 2013 at 16:44
  • @chris As I state above, if I use FakeMas fakemas, it will say "undefined reference to `zinc::testwebserver::FakeMas::FakeMas()'" Commented Jan 9, 2013 at 16:49
  • @user1853170, Undefined reference means it can't find the definition, which most likely means you're not linking the cpp containing the definitions. Commented Jan 9, 2013 at 16:53

2 Answers 2

1

This sounds like a link error: the object code from FakeMas.cpp is not being linked with the object code from FakeMasTest.cpp

(these would be FakeMas.o and FakeMasTest.o on some platforms, .obj on others - you haven't specified yours).

So, your code compiles, but cannot be formed into an executable. The solution depends on your build system (which again you haven't specified), but is essentially to tell it that your executable depends on, and should contain, both object files.


The unrelated error you got is because this isn't valid:

FakeMas::FakeMas * fakemas = new FakeMas::FakeMas();

it should be:

FakeMas * fakemas = new FakeMas();

(and then you'll be back to the same link error).


From your Makefile:

CppUnit_Tests = fakemastest
TESTS = $(CppUnit_Tests)
testdir = $(prefix)/tests/@PACKAGE@/test
test_PROGRAMS = $(CppUnit_Tests)

fakemastest_SOURCES = FakeMasTest.cpp

you're telling Make that the executable fakemastest depends only on FakeMasTest.cpp. Tell it about FakeMas.cpp as well, by changing the last line:

fakemastest_SOURCES = FakeMasTest.cpp FakeMas.cpp

(this is making certain assumptions about your build rules, which aren't shown, but it's a sensible place to start).

Sign up to request clarification or add additional context in comments.

2 Comments

Just edit this into the question, it'll be easier than a chain of one-line comments
Yes, as I've edited into my answer. You can delete the one-line comments too, btw.
0

It seems that compiler cannot find the declaration of FakeMas. Either you didn't add

#include "FakeMas.h"

or you need to specify correct namespace:

using namespace namespace_with_FakeMas;

Compiler message shows that compiler is looking for a class declaration in namespace zinc::testwebserver. It seems that this is not the same namespace where FakeMas was declared.

2 Comments

in those three files(FakeMas.h, FakeMas.cpp, FakeMasTest.cpp) I all start with "NS_ZINC_TESTWEBSERVER_OPEN"
"NS_ZINC_TESTWEBSERVER" is namespace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.