c++ - 大括号或等于初始化程序在 Visual Studio 2015 RC 中导致 C2098 和 C2059

标签 c++ visual-studio c++11 compiler-errors visual-studio-2015

<分区>

以下看似正确的代码无法在 Visual Studio 2015 RC 中编译,并出现如下错误:

test.cpp(6): error C2098: unexpected token after data member 'T'
  test.cpp(11): note: see reference to class template instantiation 'Foo<int>' being compiled
test.cpp(6): error C2059: syntax error: '>'

代码:

template <typename, typename> struct X {};

template <typename T>
struct Foo
{
    X<int, T> * p = new X<int, T>;
};

int main()
{
   Foo<int> f;
}

为什么会这样,我该如何克服这个问题?

最佳答案

您的编译器似乎没有正确实现 C++,特别是大括号或等于初始化程序。

一个简单的解决方法是用构造函数初始化器替换大括号或等于初始化器:

template <typename T>
struct Foo
{
    Foo() : p(new X<int, T>) {}
    //      ^^^^^^^^^^^^^^^^

    X<int, T> * p;  // no initializer
};

历史记录:在我将其替换为重现错误的最小示例之前,您原始帖子中的修复:

class ArdalanCollection : public ICollection<T>
{ 
public:
    ArdalanCollection()
    : storage(new Container_<int, T*>()), index(0) {}
//    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    virtual void add(T* obj) {
        storage->add(index++, obj);
    };

private:
    Container_<int, T*> *storage;   // no initializer here
    int index;
};

关于c++ - 大括号或等于初始化程序在 Visual Studio 2015 RC 中导致 C2098 和 C2059,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546363/

相关文章:

c++ - 具有 boost::shared_ptr 类型成员的 std::map 序列化失败

c++ - 用 alsa 录制 8 位有符号单声道 16khz 声音的最简单方法(在某种程度上它与脉冲兼容)?

visual-studio - 无法复制文件 .xbf,因为找不到

c++ - 如何在 C++ 中进行自动机/状态机编码?

c++ - Directwrite:获取字体的高度

c++ - if 和 else 没有大括号

c# - 新 Visual-Studio 项目的默认应用程序兼容性 list ?

visual-studio - 如何将VC++6.0项目升级到VS2010?

作为类成员的线程的 c++11 vector