【发布时间】:2018-04-20 23:49:49
【问题描述】:
我尝试构建一个在 OSX (clang) 和 Linux (GCC/Clang) 上编译得很好的库。我偶然发现了一个 MSVC/Visual Studio 2017 不理解的模板定义。我可以归结为:
#include "stdafx.h"
template<class T>
class Parent {
class Child {
friend class Parent;
public:
Child(const Child &r);
};
};
template<class T>
Parent<T>::Child::Child(const Parent<T>::Child &r) {
*this = r;
}
int main(int argc, char const *argv[]) {
/* code */
return 0;
}
这(仍然)会导致以下错误:
1>...consoleapplication1.cpp(13): error C2988: unrecognizable template declaration/definition
1>...consoleapplication1.cpp(13): error C2059: syntax error: '('
1>...consoleapplication1.cpp(13): error C2143: syntax error: missing ';' before '{'
1>...consoleapplication1.cpp(13): error C2447: '{': missing function header (old-style formal list?)
但是(仍然)在 OSX 上使用 Clang 或 GCC 编译。 stdafx.h 和 main 不是实际库代码的一部分,但已添加以允许在 VS Windows 命令行项目中使用它。
我在这里想念什么? MSVC 是否存在模板中的成员类或友元声明问题?
【问题讨论】:
-
This 解决了这个问题。看起来其中一个肯定是错误的
-
@PasserBy: 将
const Parent<T>::Child更改为const *typename* Parent<T>::Child确实解决了这个问题。也非常感谢你向我展示了这个非常有用的在线编译器。我一定会用它来解决未来出现的问题。
标签: c++ visual-c++ language-lawyer