【问题标题】:C++ template working fine for GCC but showing compile time error in LLVM-GCC compilerC++ 模板适用于 GCC,但在 LLVM-GCC 编译器中显示编译时错误
【发布时间】:2011-12-06 01:40:18
【问题描述】:
typedef char TCHAR;

template <class T> class MyTemplateString
{

};
template <class T> class MyList
{

};
typedef MyTemplateString<TCHAR>         MyString;

MyList<MyString> outlist;// here it's showing compile time error

错误是:

未定义模板MyList &lt;MyTemplateString&lt;char&gt;&gt;的隐式实例化

仅适用于 GCC 编译器,但不适用于 LLVM-GCC 编译器。

【问题讨论】:

  • 右大括号后需要分号。
  • 我很确定代码原样不会在任何地方编译,因为 MyString 根本没有定义。请展示真实的代码,而不仅仅是您认为重要的内容的解释。
  • –1;请发布实际,编译代码。以上内容在 GCC 中无法编译。
  • @KPT: error: 'TCHAR' was not declared in this scope 这仍然不是 实际 代码。写一个例子有什么难的,两个都试一下,只有当它真正在 gcc 中编译而不是在 llvm-gcc 中时才发布?
  • @KPT 您的更新并没有使问题变得更好。现在代码用 GCC 编译 但它也可以用 LLVM/GCC 和 LLVM/clang++ 编译

标签: iphone c++ xcode4 llvm-gcc


【解决方案1】:

使用 g++ 和 llvm-g++,上面发布的代码在这里编译良好,没有任何错误或警告:

$ g++ -Wall -c template.cpp 

$ llvm-g++ -Wall -c template.cpp 

版本信息:

$ g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~123/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

$ llvm-g++ -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/llvmgcc42/llvmgcc42-2335.9~9/src/configure --disable-checking --enable-werror --prefix=/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --enable-llvm=/var/tmp/llvmgcc42/llvmgcc42-2335.9~9/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.9)

我的猜测是您的真正问题出在其他地方。

【讨论】:

  • 我正在使用 XCode 4 并且在它们中没有 GCC 编译器的选项,所以我在 LLVM-GCC 中编译并且错误即将到来,但是当我在 XCode 3 中使用 GCC 编译器编译相同的代码时它编译得很完美。是否有任何设置问题可能导致该问题?
  • 您可以在 Xcode 4 项目设置中指定编译器,但首先您可以尝试按照我上面的回答从命令行编译代码示例(请注意,以上两种情况都使用相关的编译器来自 Xcode 4.0)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
相关资源
最近更新 更多