c++ 模板遇到的有关问题
c++ 模板遇到的问题
在linux下用g++编译后出现以下错误,望各位高手求解。
test.cc: In function ‘void fung(K)’:
test.cc:27:16: error: expected primary-expression before ‘int’
test.cc:27:16: error: expected ‘;’ before ‘int’
test.cc: In function ‘void fung(K) [with K = int]’:
test.cc:31:8: instantiated from here
test.cc:27:2: error: no match for ‘operator<<’ in ‘std::cout << a.A::fun [with T = T, U = int]’
------解决思路----------------------
已经改好了
class A
{
public:
template<typename T>
int fun(int a);
private:
U u;
}; // end of A
template<typename U>
template<typename T>
int A<U>::fun(int a)
{
T b = 11;
return a + b;
}
template <typename K>
void fung(K k)
{
A<K> a; // 此行改为A<int> a; 可以编译通过
cout << a.fun<int>(k) << endl;
}
int main(int argc, char* argv[])
{
fung(9);
return 0;
}
在linux下用g++编译后出现以下错误,望各位高手求解。
test.cc: In function ‘void fung(K)’:
test.cc:27:16: error: expected primary-expression before ‘int’
test.cc:27:16: error: expected ‘;’ before ‘int’
test.cc: In function ‘void fung(K) [with K = int]’:
test.cc:31:8: instantiated from here
test.cc:27:2: error: no match for ‘operator<<’ in ‘std::cout << a.A::fun [with T = T, U = int]’
------解决思路----------------------
已经改好了