C++模板的问题,请问我这个程序哪里错了?
//ConsoleApplication1.cpp:定义控制台应用程序的入口点。//#include"stdafx.h"#include<iostream>templat...
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
template<class T>;
int swap(T &a, T &b)
{
T &c;
c = a;
a = b;
b = c;
return 1;
}
int main()
{
int a = 1, b = 2;
int & ra = a;
int & rb = b;
std::cout << "a="<<a<<"\t"<<"b="<<b;
std::cin >> a;
}
运行环境visual studio2017
结果报错
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E0020 未定义标识符 "T" ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 9
错误(活动) E0020 未定义标识符 "a" ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 9
错误(活动) E0018 应输入“)” ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 9
错误(活动) E0065 应输入“;” ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 10
错误 C2988 不可识别的模板声明/定义 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 7
错误 C2059 语法错误:“<end Parse>” ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 7
错误 C2065 “T”: 未声明的标识符 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 9
错误 C2065 “a”: 未声明的标识符 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 9
错误 C2065 “b”: 未声明的标识符 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 9
错误 C2448 “swap”: 函数样式初始值设定项类似函数定义 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 10 展开
//
#include "stdafx.h"
#include<iostream>
template<class T>;
int swap(T &a, T &b)
{
T &c;
c = a;
a = b;
b = c;
return 1;
}
int main()
{
int a = 1, b = 2;
int & ra = a;
int & rb = b;
std::cout << "a="<<a<<"\t"<<"b="<<b;
std::cin >> a;
}
运行环境visual studio2017
结果报错
严重性 代码 说明 项目 文件 行 禁止显示状态
错误(活动) E0020 未定义标识符 "T" ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 9
错误(活动) E0020 未定义标识符 "a" ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 9
错误(活动) E0018 应输入“)” ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 9
错误(活动) E0065 应输入“;” ConsoleApplication1 c:\Users\ruodaicu\Documents\Visual Studio 2017\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 10
错误 C2988 不可识别的模板声明/定义 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 7
错误 C2059 语法错误:“<end Parse>” ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 7
错误 C2065 “T”: 未声明的标识符 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 9
错误 C2065 “a”: 未声明的标识符 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 9
错误 C2065 “b”: 未声明的标识符 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 9
错误 C2448 “swap”: 函数样式初始值设定项类似函数定义 ConsoleApplication1 c:\users\ruodaicu\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 10 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询