"使用未声明的标识符A"

时间:2014-06-08 05:45:38

标签: c++

有关导致此编译时错误的原因的任何想法?

基本设置:

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

我非常确定我正确地将文件链接在一起,那么可能是什么问题呢?

1 个答案:

答案 0 :(得分:1)

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