错误C2065:模板函数中未声明的标识符

时间:2014-01-03 23:14:37

标签: c++ visual-studio-2010 templates

我是C ++的新手,我正试图把一个小项目放在轮子上,但是我很难解决这些错误而且我不知道我做错了什么。我想,我已经包含了所有必要的头文件。 我做错了什么?

提前谢谢!

d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(31): error C2065: 'CmdAritmetice' : undeclared identifier

d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(31): error C2062: type 'int' unexpected

d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(37): error C2065: 'cmd1' : undeclared identifier

d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(37): error C2228: left of '.Execute' must have class/struct/union

type is ''unknown-type''

修改

如果我使用CmdAritmetice <int, Suma> cmd1("+");修改UI::CmdAritmetice<Calcule::Suma<int>> cmd1("+");,则在尝试编译时会出现以下错误:

1>d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(32): error C2440: 'specialization' : cannot convert from 'T (__cdecl *)(T,T)' to 'int (__cdecl *)(int,int)'
1>          None of the functions with this name in scope match the target type
1>d:\files\visual studio 2010\projects\calculator\aplicatie.cpp(39): error C2660: 'Calculator::UI::CmdAritmetice<Operatie>::Execute' : function does not take 1 arguments
1>          with
1>          [
1>              Operatie=0x0
1>          ]

Main.cpp

#include "aplicatie.h"
using namespace Calculator;

int main()

{
    Aplicatie app;
    app.Run();
    return 0;
}

aplicatie.cpp 已编辑

#include <iostream>
#include "aplicatie.h"
#include "Calcule\operatii.h"
#include "UI\cmdaritmetice.h"

using namespace std;

namespace Calculator{

(...)

    void Aplicatie::Run()
    {
        TestSuma();
        CmdAritmetice <int, Suma> cmd1("+");     //here i have errors
        cmd1.Execute("Introduceti doua numere intregi (x, y)");
    }

    void Aplicatie::TestSuma()
    {
        int x = 10, y = 20;
        int r = Calcule::Suma(x,y);
    }    
}

cmdaritmetice.h

#ifndef ARITMETICE_H
#define ARITMETICE_H
#include "UI\comanda.h"

namespace Calculator{
    namespace UI{

        template<int Operatie(int, int)>
        class CmdAritmetice : public ComandaCalcule
        {

        public:
            CmdAritmetice(const string &nume) : ComandaCalcule(nume)
            {
            }
            void Execute()
            {
            cout << Nume() << endl;
            cout << "Introduceti doua numere intregi (x, y)\n";
            int x, y;
            cin >> x >> endl;
            cin >> y >> endl;
            cout << x << " " << Nume() << " " << y << " = " << Operatie (x,y) <<endl;
            }
        };
    }
}
#endif

LE

**operatii.h**

#ifndef OPERATII_H
#define OPERATII_H

namespace Calculator{
    namespace Calcule{

    template<typename T>
    T Suma(T x, T y)
    {
        return x + y;
    }

}
#endif

1 个答案:

答案 0 :(得分:0)

CmdAritmetice在名称空间Calculator::UI中定义,而Aplicatie在名称空间Calculator中定义。因此,您必须在CmdAritmetice命名空间中将UI::CmdAritmetice称为Calculator

至于无法找到Suma的错误,你还没有发布Suma的定义,所以很难说那里发生了什么,但检查以确保它也不是'在一些嵌套的命名空间中定义。

编辑:
CmdAritmetice的定义是

template<int Operatie(int, int)>
class CmdAritmetice : public ComandaCalcule
{
    // ...
};

因此,它需要一个带有签名int(int, int)的函数的模板参数,即一个接受2 int并返回int的函数。在Aplicatie::Run()内,您尝试将类实例化为

CmdAritmetice <int, Suma>

显然这不起作用,模板参数需要是与您在定义中指定的签名匹配的函数。此外,对Suma的引用需要被限定为Calcule::Suma

我认为你打算做的是

UI::CmdAritmetice<Calcule::Suma<int>> cmd1("+");