views:

168

answers:

4

I'm developing in native C++, using visual studio.


I have one project which contains infrastructures - base classes which I want to derive classes from in other projects in the same solution. Say I have a base class in the infrastructres project:

file base.h:

class Base
{
      public:
          void Foo();
      protected: 
       void Bar();
};

and in another project, a class derived from A, try to call the method bar:

file derived.h:

class Derived : Base
{
    public:
     void DoSomething();
};

file derived.cpp:

void Derived::DoSomething()
{
 Bar();
}

file main.cpp:

void main()
{
  Derive d;
  d.Foo(); //OK
  d.DoSomething(); // Linker error
}

generates the following linker error:

Error 1 error LNK2001: unresolved external symbol "public: virtual void __thiscall Base::Bar(void)" (?Bar@Base@@UAEXXZ) main.obj CplusplusTestProject

What am I doing wrong?

A: 

The simplest possible problem is that you are not linking the library generated in the other project into your own executable.

David Rodríguez - dribeas
But in that case I should get an error for calling Foo as well.It only happens if the method called is protected. If I change it to public everything works fine.
Shelly Nezri
I had skipped over that part... Now next question is whether `Bar` is actually implemented in the original library?
David Rodríguez - dribeas
+1  A: 

Make sure your Base::Bar() method has its implementation somewhere. You can just add curly bracers after its definition and rebuild your project.

Serge
A: 

You need a definition of the Bar member. Add a definition of Bar in the class definition or in a separate base.cpp file.

dirkgently
A: 

This is Shelly again. Don't know how to add comments to the answers above. I change the inheritance to public - still not working. Yes, there is an implemenetation to Bar, I just didn't put it in the question. The link error disapears if I change Bar's access level to public - it only apears if Bar is protected. Could it be that the VS linker doesn't add protected and private methods to the symbol table?

Shelly Nezri
To add a comment to your question (or to an answer), click the "add comment" below the post.
Jason Govig
Yes, thank you, but I posted the question as unregistered user, and only later I registered - so the "add comment" button appears only beneath this answer - I cna not add comments to any of the other questions....
Shelly Nezri