C++ 链接时对基类函数的 undefined reference (--不是--vtables、构造函数、模板或纯虚函数)

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

我正在从事一个项目,试图实现一个独立的基类 (A),它可以由以后的类(例如 B 和 C)扩展。我希望能够独立地使用它们,因此没有纯虚函数,并且我只想要任何派生类中每个类的一个拷贝(因此使用虚拟函数来避免可怕的钻石)。有些类,比如 D,需要组合所有以前扩展的类,保留它们的所有功能,同时实现额外的功能。

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

一切都编译得很好,但无论何时何地,我总是在链接处收到“对‘[基类函数]’的 undefined reference ”。

到目前为止,我能找到的所有答案都是关于错误地实现纯虚函数、试图对构造函数进行欺骗,或者未能实现一个类的所有成员(因此出现 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 相同的基本模式。

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

最佳答案

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

A 类

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

实现

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

Thera 在第二种情况下没有 A::

关于C++ 链接时对基类函数的 undefined reference (--不是--vtables、构造函数、模板或纯虚函数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940134/

相关文章:

c++ - 转发 "Pre-declaring"C++ 中的一个类

c++ - 从具有基类函数的派生类打印

android - 如何以编程方式在 Android 应用程序中显示当前的 Git 分支?

c++ - 强制 GLUtesselator 只生成 GL_TRIANGLES?

c++ - 指向成员变量的指针作为静态成员

c++ - QT 离屏渲染保持黑色

c++ - 固定大小 vector 的C++有效增长 vector

java - JarScan,扫描特定类的所有子文件夹中的所有 JAR 文件

c++ - 在C++中使用虚拟析构函数

macos - 有没有人设置tomcat来使用mod_jk运行虚拟主机