MSVC错误 - 错误C2373:'描述':重新定义;不同类型的修饰符

时间:2016-07-04 03:27:26

标签: c++ c++11 gcc visual-c++

#include <iostream>
#include <type_traits>


namespace MyNS {
    struct Test1 {};
    template <typename > class Test;

    template <> class Test<Test1> {
        public:
            constexpr static char const *description[] = { "X1", "Y1",};
    };
    constexpr char const *Test<Test1>::description[];
    static const char * getDesc(int i) {
        return MyNS::Test<MyNS::Test1>::description[i];
    }
}

int main()
{
    std::cout << MyNS::getDesc(0) << std::endl;
}

这与MSVC失败(错误C2373:&#39;描述&#39;:重新定义;不同的类型修饰符),但在GCC 4.8上成功编译。

是否有成功构建MSVC和GCC的解决方法?

2 个答案:

答案 0 :(得分:2)

删除描述的重新定义并进行编译。你还需要从main返回一个值。

#include <iostream>
#include <type_traits>


namespace MyNS {
    struct Test1 {};
    template <typename > class Test;

    template <> class Test<Test1> {
        public:
            constexpr static char const *description[] = { "X1", "Y1",};
    };

    //constexpr char const *Test<Test1>::description[];

    static const char * getDesc(int i) {
        return Test<Test1>::description[i];
    }
}

int main()
{
    std::cout << MyNS::getDesc(0) << std::endl;
    return 0;
}

答案 1 :(得分:1)

似乎MSVC希望我们精确地确定数组长度,这应该可行:

#include <iostream>
#include <type_traits>


namespace MyNS {
    struct Test1 {};
    template <typename > class Test;

    template <> class Test<Test1> {
        public:
            constexpr static char const *description[2] = { "X1", "Y1",};
    };
    constexpr char const *Test<Test1>::description[];
    static const char * getDesc(int i) {
        return MyNS::Test<MyNS::Test1>::description[i];
    }
}

int main()
{
    std::cout << MyNS::getDesc(0) << std::endl;
}

编辑:您只需要在第一个定义中确定长度。