C++ &引用;使用未声明的标识符A";
你知道是什么导致了这个编译时错误吗 基本设置: main.cppC++ &引用;使用未声明的标识符A";,c++,C++,你知道是什么导致了这个编译时错误吗 基本设置: main.cpp #include <iostream> #include "GroupTheorizer.h" int main() { // ... functs::Adder<char> A; // error on this line / ... return 0; } #include "GroupTheorizer.h" #include <set>
#include <iostream>
#include "GroupTheorizer.h"
int main()
{
// ...
functs::Adder<char> A; // error on this line
/ ...
return 0;
}
#include "GroupTheorizer.h"
#include <set>
#include <iostream>
#include <limits>
#include <string>
// ... implementations of GroupTheorizer members
// ...
namespace functs
{
class Adder
{
private:
static const char symbol = '+';
public:
T operator() (const T & x, const T & y) const { return x + y; };
char getSymbol(void) const { return symbol; };
};
// other functors ...
// ...
}
GroupTheorizer.cpp
#include <iostream>
#include "GroupTheorizer.h"
int main()
{
// ...
functs::Adder<char> A; // error on this line
/ ...
return 0;
}
#include "GroupTheorizer.h"
#include <set>
#include <iostream>
#include <limits>
#include <string>
// ... implementations of GroupTheorizer members
// ...
namespace functs
{
class Adder
{
private:
static const char symbol = '+';
public:
T operator() (const T & x, const T & y) const { return x + y; };
char getSymbol(void) const { return symbol; };
};
// other functors ...
// ...
}
#包括“GroupTheorizer.h”
#包括
#包括
#包括
#包括
// ... GroupTheorizer成员的实现
// ...
命名空间函数
{
类加法器
{
私人:
静态常量字符符号='+';
公众:
T运算符()(常数T&x,常数T&y)常数{返回x+y;};
char getSymbol(void)const{return symbol;};
};
//其他函子。。。
// ...
}
我很确定我正确地将文件链接在一起了,那么问题出在哪里呢?看看你的
加法器的实现,你似乎想说它是一个模板,但没有这样写
您只缺少模板
行
template <typename T>
class Adder
{
private:
static const char symbol = '+';
public:
T operator() (const T & x, const T & y) const { return x + y; };
char getSymbol(void) const { return symbol; };
};
模板
类加法器
{
私人:
静态常量字符符号='+';
公众:
T运算符()(常数T&x,常数T&y)常数{返回x+y;};
char getSymbol(void)const{return symbol;};
};
Adder
似乎是一个模板类,但您的示例并未显示这一点。相反,Adder
不是模板类,但带有错误的行将其视为模板类来使用。由于\uuuuuuuu群论\uuuuuuuuuuuuuuuuuuu
,包含两个或多个相邻下划线的名称被保留,因此您的行为未定义(名称以一个下划线开头,后跟一个大写字母,在全局范围内,名称以一个下划线开头)。@owlstead这不是OP问题的原因,只是代码的一个单独问题。@JBentley好的,很抱歉,只是回顾一下问题,它看起来像是我的答案:)我认为这可能不是问题所在(但我很可能是错的):如果这是问题所在,那么OP应该在OP询问错误之前就得到关于T操作符()(…)
成员的错误。