2008年5月11日日曜日

C++のテンプレートに悩む

C++のテンプレートにはまった. 下のようにテンプレートの片方のパラメータの特殊化しようとしたが, コンパイルが通らない... C++難しいなあ.


(a.cpp)
01: #include <iostream>
02:
03: using namespace std;
04:
05: template<typename T, int N>
06: struct A{
07: int val() const;
08: };
09:
10: template<>
11: template<typename T>
12: int A<T,1>::val() const{
13: return 1;
14: }
15:
16: int main(void){
17: A<int,1> a;
18: cout << a.val() << endl;
19: return 0;
20: }

環境: gcc version 4.2.1 20070719 [FreeBSD]

a.cpp:12: error: invalid use of incomplete type 'struct A'
a.cpp:6: error: declaration of 'struct A'
*** Error code 1

0 件のコメント: