2015-11-15 24 views
0

我想將引用變量傳遞給具有下面的代碼的模板,但我收到編譯器錯誤。任何人都可以找出原因。謝謝。將變量的引用傳遞給模板

#include<iostream> 
using namespace std; 

template<int &T> 
class Test 
{ 
public: 
    static void do_it() 
    { 
     T=1; 
    } 
}; 

struct A{ 
    static int x; 
}; 

int A::x=0; 

int main() 
{ 
    Test<A::x> test; 
    test.do_it(); 
    cout<<A::x; 
    return 0; 
} 

錯誤:

error C2143: syntax error : missing ',' before '.'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
fatal error C1004: unexpected end-of-file found

回答

3

Test<A.x> test;應該Test<A::x> test;

+0

優秀的感謝, 任何具體的原因,爲什麼它允許通過只靜態memeber但不正常的數據成員? – Raj

+0

@Raj,我想這是因爲你使用非類型參數,參數應該在編譯時知道。但我不太確定。 –

相關問題