吴亦凡演唱会门票:C++程序设计中的string 类型的问题

来源:百度文库 编辑:高校问答 时间:2024/05/10 12:02:07
#include <iostream.h>
#include <cstring.h>

using namespace std;

const string FIRST = "Herman";
const string LAST ="Smith";
const char MIDDLE='G';

int main()
{
string firstLast;
string lastFirst;
firstLast = FIRST+" "+LAST;
cout<<"Name in first-last format is "<<firstLast<<endl;
lastFirst =LAST+", "+FIRST+", ";
cout <<"Name in last-first-initial format is ";
cout << lastFirst <<MIDDLE<<'.'<<endl;

return (0);
}

上面是我的程序设计,我定义了子符串常量,而且也加了<string.h>头文件,可在编译的时候老提示我sting没有初始化?为什么?下面是提示的错误信息
error 6: constant variable 'sting' must be initalized.
我用的是borland c++3.1 和TC3.0的编译器。请各位高手帮忙
下面是用VC++6.0调试结果,显示的错误。

C:\BC31\BIN\printname.cpp(4) : error C2146: syntax error : missing ';' before identifier 'FIRST'
C:\BC31\BIN\printname.cpp(4) : error C2734: 'string' : const object must be initialized if not extern
C:\BC31\BIN\printname.cpp(4) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

printname.obj - 3 error(s), 0 warning(s)

加入#include"string.h",也不能调试通过。

CString.h这个有用么,CString是MFC中的一个类,如果你要用string类型的,是不能这样包含头文件的。要用

#inlcude <string>//没有.h
using namespace std;//一旦使用std命名空间,就意味着你要使用stl了

用这个头文件试试、
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
如果不成就把电脑砸了。

是不是还要加#include"string.h",我看你的程序里没有