"Use of undeclared identifier A"

"Use of undeclared identifier A"

本文关键字:identifier of Use undeclared      更新时间:2023-10-16

你知道是什么导致了这个编译时错误吗?

基本设置:

main.cpp

#include <iostream>
#include "GroupTheorizer.h"
int main()
{
    // ... 
        functs::Adder<char> A; // error on this line
    / ...
    return 0;
}

GroupTheorizer.h

#ifndef __GroupTheory__GroupTheorizer__
#define __GroupTheory__GroupTheorizer__
class GroupTheorizer
{
   // definitions of members of a GroupTheorizer object
   // ...
};
#endif /* defined(__GroupTheory__GroupTheorizer__) */

GroupTheorizer.cpp

#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 ...
// ...
}

我相当确定我正确地将文件链接在一起,所以可能是什么问题?

看看你的Adder的实现,似乎你的意思是它是一个模板,但还没有这样写。

你只漏了template <typename T>行。

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; };
};