4

我有以下代码

#include <iostream>
template <class T>
class A
{
public:
    static constexpr int arr[5] = {1,2,3,4,5};
};

template<> constexpr int A<int>::arr[5];

int main()
{
    A<int> a;
    std::cout << a.arr[0] << std::endl;
    return 0;
}

编译通过就好了,但我有一个我不明白的链接错误

g++ -std=c++11 test.cpp -o test
/tmp/ccFL19bt.o: In function `main':
test01.cpp:(.text+0xa): undefined reference to `A<int>::arr'
collect2: error: ld returned 1 exit status
4

1 回答 1

7

你不能只为一种类型定义它,你需要

template<class T> constexpr int A<T>::arr[5];
于 2013-10-03T10:39:04.923 回答