【发布时间】:2016-03-26 04:41:34
【问题描述】:
我有一个基类 class A<T> 和一个派生类 class B : public A<string>。
class A 有一个以整数作为唯一参数的构造函数和一个标准的复制构造函数。它没有无参数的默认构造函数(不是我定义的;我意识到编译器可能正在创建一个,这可能与我的问题有关)。
class B 的构造函数如下所示:
B()
: A(37)
{
}
我得到的错误是:
编译器将基构造函数调用错误地初始化为字段
fileB.h: In constructor ‘B::B()’:
fileB.h:25: error: class ‘B’ does not have any field named ‘A’
和
编译器认为正在使用默认(无参数)基类构造函数,并建议使用两个非默认基类构造函数...
// Where is it getting this? A() is never written explicitly
fileB.h:25: error: no matching function for call to ‘A<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::A()’
fileA.h:37: note: candidates are: A<T>::A(const A<T>&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
...其中一个应该与被调用的内容匹配。
// This one matches A(37), right?
fileA.h:24: note: A<T>::A(const int&) [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]
我相信这个错误来自我的编译器的安装或配置方式,因为当我在我的计算机上编译我的代码时,我没有收到任何错误,但是当我将所有内容 scp 到我的学校的电脑并在那里编译。我在这两个地方都使用了 g++,但显然某处存在一些差异。有什么区别,错误来自哪里?
不存在从 A 到 int 的转换运算符,反之亦然。
我在两个系统上都使用 c++03。
这是重现错误的完整代码示例(同样,仅在一个系统上):
/*******************************
* fileA.h
******************************/
template <class T>
class A
{
int member;
public:
A(const int & m)
: member(m)
{
}
A(const A & copyFrom)
{
member = copyFrom.member;
}
};
/*******************************
* fileB.h
******************************/
#include <string>
#include "fileA.h"
using namespace std;
class B : public A<string>
{
B()
: A(37)
{
}
};
【问题讨论】:
-
我在哪里做的?
-
class B : public class A<string>- 它真的是这样编译的吗,在A<string>之前加上class这个词? -
Grr。不,抱歉,这只是
class B : public A<string>。我现在就编辑它。 -
提供具有可重现错误的可编译代码示例可能是有意义的。看起来 GCC 应该按原样编译您的构造函数(即我的原始答案不正确)。
标签: c++ templates inheritance compiler-errors g++