2011-06-22 8 views
5

私は次のエラーを取得しています:エラー: '<' tokenの前にunqualified-idが必要です。

preprocessor_directives.cpp|15|error: expected unqualified-id before '<' token| 
preprocessor_directives.cpp|26|error: expected `;' before "int"| 
||=== Build finished: 2 errors, 0 warnings ===| 
#include <iostream> 

using namespace std; 

// Avoid. Using #define for constants 
#define MY_CONST1 1000 

// Use. Equivalent constant definition 
const int MY_CONST2 = 2000; 

// Avoid. Using #define for function like macros 
#define SQR1(x) (x*x) 

// Use. Equivalent function definition 
inline template <typename T> 
T SQR2 (T a) { 
    return a*a; 
} 
// Writing #define in multiple lines 
#define MAX(a,b) \ 
((a) > (b) ? (a) : (b)) 

// Compile time flags 
#define DEBUG 

int main() 
{ 
    cout << "SQR1 = " << SQR1(10) << endl; 
    cout << "SQR2 = " << SQR2(10) << endl; 
    cout << "MAX = " << MAX(10,11) << endl; 
    cout << "MY_CONST1 = " << MY_CONST1 << endl; 
    cout << "MY_CONST2 = " << MY_CONST2 << endl; 

    return 0; 
} 
+0

問題はインラインテンプレートの定義です。なぜここでインラインキーワードを使用しますか? – poseid

+0

+1は '回避する。 #define for ....を使用しています。自己学習を続けてください。ところで、「MAX」のマクロも避けてください。 – Nawaz

+0

私はこのサイトから入手しましたhttp://login2win.blogspot.com/2008/06/c-preprocessor-directives.html – pandoragami

答えて

9

移動templateキーワードの後inlineキーワード。

template <typename T> inline 
T SQR2 (T a) { 
    return a*a; 
} 
6
template <typename T> 
inline T SQR2 (T a) { 
    return a*a; 
} 
関連する問題