2016-07-14 157 views
0

我想要編譯:模板錯誤的代碼塊,C++,代碼塊,錯誤

#include<iostream> 
#include"gettype.*" 
using namespace std; 


int main() { 
int i; 
cout << getType(i) << endl; 
unsigned int ui; 
cout << getType(ui) << endl; 
char c; 
cout << getType(c) << endl; 
double d; 
cout << getType(d) << endl; 
bool b; 
cout << getType(b) << endl; 
float f; 
cout << getType(f) << endl; 
} 

有了這個爲模板:

#ifndef GETTYPE_T 
#define GETTYPE_T 

template <typename T> 
std::string getType(T t) { return "unbekannter Typ";} 
template<typename T> std::string getType(int t) { return "int";} 
template<typename T> std::string getType(unsigned int t) { return "unsigned     int";} 
template<typename T> std::string getType(double t) { return "double";} 
template<typename T> std::string getType(char t) { return "char";} 
template<typename T> std::string getType(bool t) { return "bool";} 
#endif 

我收到此錯誤,從控制檯的代碼塊:

||=== Build: Debug in gettype.t (compiler: GNU GCC Compiler) ===| 
gettype.t.c|6|error: expected '=', ',', ';', 'asm' or '__attribute__' before  '<' token| 
gettype.t.c|9|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token| 
gettype.t.c|10|error: expected '=', ',', ';', 'asm' or '__attribute__'  before '<' token| 
gettype.t.c|11|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token| 
gettype.t.c|12|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token| 
gettype.t.c|13|error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token| 
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 

我不知道這裏有什麼錯... :-(提前爲您的時間:-) 歡呼聲。

+4

'的#include 「gettype。*」'??? –

+2

僅供參考,問題標題不是標籤,而是標題。 – HolyBlackCat

+0

#包括「gettype。*」是一個錯誤...它不適用於擴展名.t – James

回答

1

你並不需要「專注」功能的模板版本,你只需要重載,無template<typename T>爲已知類型:

std::string getType(int t) { return "int";} 
std::string getType(bool t) { return "bool";} 

等。這些會超載模板版本。

0

你寫的:

#include "gettype.*" 

錯誤消息告訴你,GCC是包括一個名爲gettype.t.c文件,並試圖將其編譯爲C文件。

我不知道你想做什麼,但如果您修正包括所以它只是包括gettype.tgettype.*它會工作(爲什麼你的頭有一個.t擴展名呢?)