注册 登录
编程论坛 C++教室

c++疑惑

魔鬼之子 发布于 2009-09-23 21:29, 495 次点击
# include <iostream.h>
# include <string.h>

template <class T>
T Max(T a,T b){
    return (a>b)?a:b;
}

void main(){
    int ix=12,iy=13;
    double dx=9.9,dy=7.8;
    string sx("abv"),sy("uid");
   
    cout<<"Type int:"<<Max(ix,iy)<<endl;
    cout<<"Type double:"<<Max(dx,dy)<<endl;
    cout<<"Type string:"<<Max(sx,sy)<<endl;
}

以上是源程序,可是在编译时却出现以下错误:

Compiling...
temexample.cpp
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(12) : error C2065: 'string' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(12) : error C2146: syntax error : missing ';' before identifier 'sx'
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(12) : error C2065: 'sx' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(12) : error C2065: 'sy' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(14) : error C2065: 'cout' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(14) : error C2297: '<<' : illegal, right operand has type 'char [10]'
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(14) : error C2065: 'endl' : undeclared identifier
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(15) : error C2297: '<<' : illegal, right operand has type 'char [13]'
c:\program files\microsoft visual studio\myprojects\first\temexample.cpp(16) : error C2297: '<<' : illegal, right operand has type 'char [13]'
执行 cl.exe 时出错.

First.exe - 1 error(s), 0 warning(s)
万望指教!
2 回复
#2
gz812009-09-23 21:34
# include <iostream>
# include <string>
using namespace std;
 
template <class T>
T Max(T a,T b){
    return (a>b)?a:b;
}
 
int main(){
    int ix=12,iy=13;
    double dx=9.9,dy=7.8;
    string sx("abv"),sy("uid");
     
    cout<<"Type int:"<<Max(ix,iy)<<endl;
    cout<<"Type double:"<<Max(dx,dy)<<endl;
    cout<<"Type string:"<<Max(sx,sy)<<endl;
}



运行结果:


Type int:13
Type double:9.9
Type string:uid
请按任意键继续. . .
#3
shl3052009-09-23 21:44
string sx("abv"),sy("uid");改为
std::string sx("abv"),sy("uid");
1