C++模板和结构问题

标签 c++ templates struct

我有这个...

template <typename Key, typename Value>
class A {
public:
    //...
private:
    struct MyStruct{
        Key key;
        Value value;
    };
};

它给了我以下错误:

Error   1   error C2146: syntax error : missing ';' before identifier 'value' 
Error   2   error C4430: missing type specifier - int assumed.
Error   3   error C4430: missing type specifier - int assumed.

一些规范: 使用 Visual Studio 2010 Windows 7 x64

我的错误可以在这些行之前还是之后?

最佳答案

在代码的最后,类定义后忘记了分号。

在某些情况下,您需要在模板中编写 typename Key key; 而不是简单的 Key key;,因为编译器可能不知道 Key 实际上是一个类型名。所以试试这个:

template <typename Key, typename Value>
class A {
public:
    //...
private:
    struct MyStruct {
        /* typename not allowed here */ Key key;
        /* typename not allowed here */ Value value;
    };
};

关于C++模板和结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342383/

相关文章:

c++ - 是否有泛化 int4、uint4、float4 等的模板化类型?

c++ - 在 C++ 中,有两个结构引用彼此的变量

c - 函数调用后结构体指针值不会改变

c++ - 模板继承和运算符

模板化类对象的 C++ vector

c++ - std::multiset 为插入和比较定义比较器

C++模板编译

c++ - 通过结构指针将结构的成员初始化为零

c++ - Boost消息队列未跨两个进程接收

c++ - 如何在 OpenCV 2.4.13 中导入经过训练的 SVM 检测器