我有这个...
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/