从不同项目(C+;+;)中的派生类访问受保护的方法会生成链接器错误 我在C++中开发,使用VisualStudio.

从不同项目(C+;+;)中的派生类访问受保护的方法会生成链接器错误 我在C++中开发,使用VisualStudio.,c++,visual-studio-2005,linker,C++,Visual Studio 2005,Linker,我有一个包含基础设施的项目——我希望在同一解决方案中的其他项目中从中派生类的基类。假设我在Infrastructures项目中有一个基类: 文件库.h: class Base { public: void Foo(); protected: void Bar(); }; class Derived : Base { public: void DoSomething(); }; void Derived::DoSom

我有一个包含基础设施的项目——我希望在同一解决方案中的其他项目中从中派生类的基类。假设我在Infrastructures项目中有一个基类:

文件库.h:

class Base
{
      public:
          void Foo();
      protected: 
       void Bar();
};
class Derived : Base
{
    public:
     void DoSomething();
};
void Derived::DoSomething()
{
 Bar();
}
void main()
{
  Derive d;
  d.Foo(); //OK
  d.DoSomething(); // Linker error
}
在另一个项目中,从派生的类尝试调用方法栏:

文件派生。h:

class Base
{
      public:
          void Foo();
      protected: 
       void Bar();
};
class Derived : Base
{
    public:
     void DoSomething();
};
void Derived::DoSomething()
{
 Bar();
}
void main()
{
  Derive d;
  d.Foo(); //OK
  d.DoSomething(); // Linker error
}
文件派生的.cpp:

class Base
{
      public:
          void Foo();
      protected: 
       void Bar();
};
class Derived : Base
{
    public:
     void DoSomething();
};
void Derived::DoSomething()
{
 Bar();
}
void main()
{
  Derive d;
  d.Foo(); //OK
  d.DoSomething(); // Linker error
}
文件main.cpp:

class Base
{
      public:
          void Foo();
      protected: 
       void Bar();
};
class Derived : Base
{
    public:
     void DoSomething();
};
void Derived::DoSomething()
{
 Bar();
}
void main()
{
  Derive d;
  d.Foo(); //OK
  d.DoSomething(); // Linker error
}
生成以下链接器错误:

错误1错误LNK2001:未解析的外部符号“public:virtual void\uu thiscall Base::Bar(void)”(?Bar@Base@@UAEXXZ)main.obj cplusplustest项目


我做错了什么?

最简单的问题是,您没有将在其他项目中生成的库链接到您自己的可执行文件中。

确保您的Base::Bar()方法在某个地方有它的实现。您只需在其定义后添加花括号,然后重新生成项目。

您需要定义
成员。在类定义中或在单独的
base.cpp

文件中添加
Bar
的定义。

但在这种情况下,调用Foo也会出现错误。只有在调用的方法受到保护时才会发生这种情况。如果我把它改成公共的,一切正常。我跳过了那部分。。。现在下一个问题是
Bar
是否真的在原始库中实现了?这又是Shelly。不知道如何在上面的答案中添加注释。我将继承更改为public-仍然不起作用。是的,有一个实现的酒吧,我只是没有把它的问题。如果我将Bar的访问级别更改为public,链接错误将消失-只有在Bar受到保护时才会出现。可能是VS链接器没有向符号表添加受保护和私有方法吗?您的项目是DLL库吗?