我的程序是这样的:
#include "iostream"
#include "string"
#include "fstream"
using namespace std;
template <typename T> T sort(T a[],int n)
{
T t;
int i,j,z;
ifstream ofile;
ofile.open("d:\\a.txt");
for(i=0;i<n;i++)
for(j=0;j<n-1-i;j++)
if(a[j]<a[j+1])
t=a[j];a[j]=a[j+1];a[j+1]=t;
for(z=0;z<n;z++)
ofile<<a[z]<<"\t";
ofile.close;
}
void main()
{
int a[5]={1,2,3,4,5};
double b[5]={1.1,2.2,3.3,4.4,5.5};
string c[5]={a,b,c,d,e};
cout<<sort<int>(a,5);
cout<<sort<float>(b,5);
cout<<sort<string>(c,5);
}
这个是错误提示:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
D:\0424\1.cpp(25) : error C2440: 'initializing' : cannot convert from 'int [5]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\0424\1.cpp(25) : error C2440: 'initializing' : cannot convert from 'double [5]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\0424\1.cpp(25) : error C2440: 'initializing' : cannot convert from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [5]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<c
har> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\0424\1.cpp(25) : error C2065: 'd' : undeclared identifier
D:\0424\1.cpp(25) : error C2440: 'initializing' : cannot convert from 'int' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\0424\1.cpp(25) : error C2065: 'e' : undeclared identifier
D:\0424\1.cpp(25) : error C2440: 'initializing' : cannot convert from 'int' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >'
No constructor could take the source type, or constructor overload resolution was ambiguous
D:\0424\1.cpp(25) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
1.obj - 8 error(s), 0 warning(s)
请高手帮帮忙啊。。。。
编写排序的函数模板由大到小排序,将排序后的结果写入:e:\sort.txt文件中
答案:1 悬赏:50 手机版
解决时间 2021-01-25 23:09
- 提问者网友:沉默的哀伤
- 2021-01-25 08:11
最佳答案
- 五星知识达人网友:长青诗
- 2021-01-25 08:24
改了main里面的两个错误:
string c[5]={a,b,c,d,e};
字符串难道不用加分号么?
上面定义的是double数组,下面用float。
修改后如下,不过还有错误,自己再调试一下吧。
void main()
{
int a[5]={1,2,3,4,5};
double b[5]={1.1,2.2,3.3,4.4,5.5};
string c[5]={"a","b","c","d","e"};
cout<<sort<int>(a,5);
cout<<sort<double>(b,5);
cout<<sort<string>(c,5);
}
string c[5]={a,b,c,d,e};
字符串难道不用加分号么?
上面定义的是double数组,下面用float。
修改后如下,不过还有错误,自己再调试一下吧。
void main()
{
int a[5]={1,2,3,4,5};
double b[5]={1.1,2.2,3.3,4.4,5.5};
string c[5]={"a","b","c","d","e"};
cout<<sort<int>(a,5);
cout<<sort<double>(b,5);
cout<<sort<string>(c,5);
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯