- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从事一个项目,试图实现一个独立的基类 (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/
谁能解释一下原因: (define a (lambda() (cons a #f))) (car (a)) ==> procedure ((car (a))) ==> (procedure . #f)
这是 PyBrain 网站的摘录。我了解大部分正在发生的事情,但是一行让我完全难住了。我以前从未在 python 代码中看到过这样的东西。这是整个循环,对于上下文: for c in [0,
我是gradle / groovy的新手。我想创建将做一些事情的自定义任务。我的第一个问题是任务完成时该如何做?我可以覆盖doFirst / doLast闭包吗?也许我可以重写某些在开始和结束时都会执
我刚刚开始评估 MS 企业库。他们使用以下指令来获取实例: var customerDb = EnterpriseLibraryContainer.Current.GetInstance("C
这是我的 if else Ansible 逻辑.. - name: Check certs exist stat: path=/etc/letsencrypt/live/{{ rootDomain
我正在使用construct 2.8 对一些失传已久的 Pascal 程序创建的一些文件的 header 进行逆向工程。 header 由许多不同的记录组成,其中一些是可选的,我不确定顺序是否固定。
我在将 getchar() 的输入放入 char *arr[] 数组时遇到问题。我这样做的原因是因为输入数据(将是一个带有命令行参数的文件)将存储在一个 char 指针数组中以传递给 execvp 函
通常我们不能约束类型参数 T派生自密封类型(例如 struct 类型)。这将毫无意义,因为只有一种类型适合,因此不需要泛型。所以约束如下: where T : string 或: where T :
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
#include using namespace std; class A { private: int m_i; friend int main(int argc, char cons
这个问题在这里已经有了答案: Are there legitimate uses for JavaScript's "with" statement? (33 个答案) 关闭 9 年前。 我有这个代
在this answer我看到了下一个 Bash 结构。 yes "$(< file.txt)" 什么意思 "$(< file.txt)" ? 我明白了 命令替换 - $(command)用命令的结
if (a == 1) //do something else if (a == 2) //do something else if (a == 3) //do somethi
关于构造的快速简单的问题。 我有以下用于将项目添加到 ListView 的代码。 ListViewItem item = new ListViewItem(); item.Text = file; i
我想使用 std::vector 来控制给定的内存。首先,我很确定这不是好的做法,但好奇心占了上风,无论如何我都想知道如何做到这一点。 我遇到的问题是这样的方法: vector getRow(unsi
下面显示了一段简单的javascript: var mystring = ("random","ignored","text","h") + ("ello world") 这个字符串会生成 hello
在 Java 中,创建对象的标准方法是使用 MyClass name = new MyClass(); 我也经常看到构造 new MyClass() { /*stuff goes in here*/
我正在编写 C++ ndarray 类。我需要动态大小和编译时大小已知的数组(分别分配自由存储和分配堆栈)。我想支持从嵌套的 std::initializer_list 进行初始化。 动态大小的没问题
我正在将一个项目从 Visual Studio 2005 转换为 Visual Studio 2008,并提出了上述结构。 using Castle.Core.Resource; using Cast
我想知道我在这里的想法是否正确,我主要针对接口(interface)进行编程,所以我想知道下面的类是否应该通过 DI 注入(inject),或者我应该自己实例化一个类... 注意:这些服务保存在我的核
我是一名优秀的程序员,十分优秀!