模板错误代码块,C++,代码块,错误
template error codeblocks,c++, codeblocks, error
我想编译:
#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)) ===|
我不知道这里有什么问题...:-(提前感谢你的时间:-)干杯。
你不需要
"专门化"函数的模板化版本,你只需要重载,而不需要对已知类型进行template<typename T>
:
std::string getType(int t) { return "int";}
std::string getType(bool t) { return "bool";}
等等。这些将使模板化版本过载。
你写过:
#include "gettype.*"
错误消息告诉您 GCC 包含一个名为 gettype.t.c
的文件,并尝试将其编译为 C 文件。
我不知道你想做什么,但是如果你修复你的包含,所以它只包含gettype.t
而不gettype.*
它会起作用(为什么你的标题有一个.t
扩展名?
相关文章:
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- VI工作室代码错误无法打开输出文件主.exe
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 有关矩阵的代码错误导致分段错误(内核转储)
- C++ Schannel POST 400 错误代码错误请求
- Java 本机访问代码错误:"Invalid memory access"
- 代码错误修改
- C++代码错误分配
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 代码错误(从 1 到 100 找到质数)
- OpenCV VisualStudio,C 代码错误尝试访问像素
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的NOOB C 输入/输出问题:代码错误
- 为什么enumProcessModules返回false值和299代码错误
- WinhttpSetoption()失败设置TLSV1.2,带有错误代码错误_internet_incorrect_ha
- 苹果安全传输代码错误
- C++ - 国际象棋主教移动代码错误
- 导入Python库时,嵌入了Python代码错误
- 编译提升C 代码错误