gpt4 book ai didi

C++模板和结构问题

转载 作者:行者123 更新时间:2023-11-30 04:32:55 25 4
gpt4 key购买 nike

我有这个...

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 2010Windows 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com