【问题标题】:C++ Undefined reference to base class functions at link time (--not-- vtables, constructors, templates, or pure virtual functions)C++ 在链接时未定义对基类函数的引用(--not-- vtables、构造函数、模板或纯虚函数)
【发布时间】:2024-01-23 17:43:01
【问题描述】:

我正在开展一个项目,试图实现一个独立的基类 (A),该基类 (A) 可以由以后的类(例如 B 和 C)扩展。我希望能够独立使用它们,因此没有纯虚函数,并且我只想要任何派生类中的每个类的一个副本(因此虚数可以避免 Dreaded Diamond)。有些类,比如 D,需要结合所有以前扩展的类,在实现附加功能的同时保留它们的所有功能。

为了增加乐趣,我(卡住)使用 C++03、gcc 4.4.6 和 make 3.81。

一切都编译得很好,但无论何时我尝试在派生类中使用基类函数,我都会在链接处得到“未定义的对“[基类函数]”的引用。

到目前为止,我能找到的所有答案都是关于不正确地实现纯虚函数、试图与构造函数一起玩,或者未能实现类的所有成员(因此出现 vtable 错误)。我没有(故意)做或打算做这些事情。

如果将这些实现为模板会很酷,但遗憾的是由于复杂性而无法实现。我无法发布实际代码,但我已经模拟了类似于基本结构和行为的示例:

声明类的头文件:

#ifndef CODE_H
#define CODE_H

class A
{
protected:
   int a_;
   int b_;
   int c_;

public:

   explicit A() :
      a_(0), b_(0), c_(0) {}

   A(const int X, const int Y, const int Z) :
      a_(X), b_(Y), c_(Z) {}

   A(const A& X) :
      a_(X.A()), b_(X.B()), c_(X.C()) {}

   int doA1(const A& X) const;

   int doA2() const;

   A getA() const;

   A& operator=(const A& X);

   const int& A() const {return a_;}
   const int& B() const {return b_;}
   const int& C() const {return c_;}

   //One for each operator but not shown for brevity
   A operator+(const A& X) const;
   void operator+=(const A& X);
   A operator+(const int X) const;
   void operator+=(const int X);
};

class B : public virtual A
{
protected:
   int d_;

public:

   explicit B() :
      A(), d_(0) {}

   B(const int D, A X) :
      A(X), d_(D) {}

   B(const int D) :
      d_(D) {}

   int doB1(const B& X) const;

   B getB() const;

   B& operator=(const B& X);

   const int& D() const {return d_;}

   //One for each operator but not shown for brevity.
   //int d_ doesn't need to be modified by the operators, so it's ignored in their implementation.
   B operator+(const B& X) const {return B(d_, (A::operator+(X.getA())));}
   void operator+=(const B& X)   {A::operator+=(X.getA());}
   B operator+(const int X) const; {return B(d_, (A::operator+(X)));}
   void operator+=(const int X)    {A::operator+(X);}
};

class C : public virtual A
{
protected:
   int e_;
   int f_;
   int g_;

public:

   explicit C() :
      A(), e_(0), f_(0), g_(0) {}

   C(const int U, const int V, const int W, 
     const int X, const int Y, const int Z) :
         A(U, V, W), e_(X), f_(Y), g_(Z) {}

   C(const A& W, 
     const int X, const int Y, const int Z) :
         A(W), e_(X), f_(Y), g_(Z) {}

   C(const C& X) :
         C(X.A(), X.B(), X.C()), e_(X.E()), f_(X.F()), g_(X.G()) {}

   int doC1(const C& X) const;

   C getC() const;

   C& operator=(const C& X);

   const int& E() const {return e_;}
   const int& F() const {return f_;}
   const int& G() const {return g_;}

   //One for each operator but not shown for brevity
   C operator+(const C& X) const;
   void operator+=(const C& X);
   C operator+(const int X) const;
   void operator+=(const int X);
};

class D : public virtual B, public virtual C
{
public:

   explicit D() :
      B(), C() {}

   D(const int D, C Y) :
      B(D), C(Y) {}

   int doD1(const D& X) const;

   int doD2() const;

   D getD() const;

   D& operator=(const D& X);

   //One for each operator but not shown for brevity.
   D operator+(const D& X) const {return D(d_, (C::operator+(X.getC())));}
   void operator+=(const D& X)   {C::operator+=(X.getC());}
   D operator+(const int X) const; {return D(d_, (C::operator+(X)));}
   void operator+=(const int X)    {C::operator+(X);}
};
#endif

实现 A 的文件:

#include header.h

int A::doA1(const A& X) const
{
   int doStuff;
   //does stuff.
   return doStuff;
}

int A::doA2() const
{
   int doStuff2;
   //does stuff.
   return doStuff2;
}

A A::getA() const
{
   A out(a_, b_, c_);
   return out;
}

A& A::operator=(const A& X)
{
   if (this != &X)
   {
      a_ = X.a_;
      b_ = X.b_;
      c_ = X.c_;
   }
   return *this;
}

//One for each operator but not shown for brevity
A A::operator+(const A& X) const
{
   return A(a_ + X.a_, b_ + X.b_, c_ + X.c_);
}

void A::operator+=(const A& X)
{
   a_ += X.a_;
   b_ += X.b_;
   c_ += X.c_;
}   

 A A::operator+(const int X) const
{
   return A(a_ + X, b_ + X, c_ + X);
}

void A::operator+=(const int X)
{
   a_ += X;
   b_ += X;
   c_ += X;
}   

实现 B 的文件:

#include header.h

int B::doB1(const B& X) const
{
   //Linker complains on both doA1 and getA, even if I qualify them or remove all the qualifiers
   return (A::doA1(X.getA()));
}

B B::getB() const
{
   //Also complains here, with or without qualifiers.
   B out(d_, A::getA());
   return out;
}

B& B::operator=(const B& X)
{
   if (this != &X)
   {
      //Also here, for presumably the same reason.
      A::operator=(D.getA());
      d_ = X.d_;
   }
   return *this;
}

我也可以为 C 和 D 提供模型,尽管它们遵循与 A 和 B 相同的基本模式。

根据我到目前为止看到的答案,我最多只需要将基类限定符添加到函数中以避免任何意外的名称隐藏,但这似乎不起作用。同样奇怪的是:实例化的对象似乎找不到它们的公共函数(就像在 doB1 中一样)。我只是一个白痴错过了一些明显的东西(如果我有 C 背景,我不会感到惊讶)?

【问题讨论】:

  • 真的需要在你的“mock up”中包含成员变量和多个成员函数吗?请降低噪音。发布minimal reproducible example
  • 我回滚了您的更改。这些错别字是一个错误,而且是一个重大错误。与其应用答案中提出的修复,不如花时间处理minimal reproducible example
  • @StoryTeller 您是否阅读了该答案下方的评论?它解释了为什么它被修复了,而且看起来完全合法。话虽如此,在已知可以编译并显示相关错误之前,不应发布代码。
  • 这段代码有很多错误。构造函数返回值,函数体前的分号。不要在 SO 编辑器中编写代码。从真实文件中复制。
  • explicit B() 你想用 explicit 做什么?

标签: c++ class virtual multiple-inheritance virtual-inheritance


【解决方案1】:

您正在定义全局函数而不是类方法。

class A

int doA1(const A& X) const;
int doA2() const;

实现

int A::doA1(const A& X) const
int doA2() const

Thera 在第二种情况下不是 A::

【讨论】:

  • 我向您致敬,因为您筛选了所有内容以发现此错误。
  • 感谢您的回复!不幸的是,这是我提交的一个错字;我不得不重新创建代码并在实现中错误地省略了限定符,这让我很懊恼。我已更正提交以更准确地反映代码(我无法直接复制)。
最近更新 更多