C++怎么定义字符串变量
C++怎么定义字符串变量,我用string不行,CString也不行 问题点数:20、回复次数:19Top
1 楼Frank001(Frank)回复于 2003-02-03 15:44:37 得分 0
可以的阿,
#include<iostream>
using namespace std;
void main()
{
string s="asdsdfs";
}
或者用字符数组也可以
char a[];Top
2 楼symbol68688(幻影s)回复于 2003-02-03 16:10:24 得分 0
不行,
C:\Zhylqmw\win982\ylqmyd\C++程序\xueshengcj\xueshengcj.cpp(10) : error C2065: 'string' : undeclared identifier
C:\Zhylqmw\win982\ylqmyd\C++程序\xueshengcj\xueshengcj.cpp(10) : error C2146: syntax error : missing ';' before identifier 's'
C:\Zhylqmw\win982\ylqmyd\C++程序\xueshengcj\xueshengcj.cpp(10) : error C2065: 's' : undeclared identifier
C:\Zhylqmw\win982\ylqmyd\C++程序\xueshengcj\xueshengcj.cpp(10) : error C2440: '=' : cannot convert from 'char [8]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
它说string s;语句中string(没定义?)s前少了";"(?)
怎么解决?Top
3 楼allen1981813(Nahe des Geliebten)回复于 2003-02-03 16:16:08 得分 0
string str("xxxx");Top
4 楼Frank001(Frank)回复于 2003-02-03 16:34:36 得分 0
你的源码?Top
5 楼efanl(传说中的一凡……)回复于 2003-02-03 17:21:56 得分 0
问一下,你用的是什么编译器?Top
6 楼idler(告别teenage)(偶是豆子。。。)(歇业休息。。。)回复于 2003-02-03 19:43:46 得分 0
using namespace std;
加了吗?Top
7 楼Snowxp2002(中文界面)回复于 2003-02-03 19:58:53 得分 0
#include <string>
不要忘了哦^^Top
8 楼Laney(6吨大笨猫)回复于 2003-02-03 20:21:08 得分 0
不对!在dev c++中就是不行!Top
9 楼symbol68688(幻影s)回复于 2003-02-03 20:24:44 得分 0
// demo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "string.h"
int main(int argc, char* argv[])
{
string s;
s="abcdefg";
return 0;
}
//***********************************************************
--------------------Configuration: demo - Win32 Debug--------------------
Compiling...
StdAfx.cpp
Compiling...
demo.cpp
C:\Zhylqmw\win982\ylqmyd\C++程序\demo\demo.cpp(9) : error C2065: 'string' : undeclared identifier
C:\Zhylqmw\win982\ylqmyd\C++程序\demo\demo.cpp(9) : error C2146: syntax error : missing ';' before identifier 's'
C:\Zhylqmw\win982\ylqmyd\C++程序\demo\demo.cpp(9) : error C2065: 's' : undeclared identifier
C:\Zhylqmw\win982\ylqmyd\C++程序\demo\demo.cpp(10) : error C2440: '=' : cannot convert from 'char [8]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
demo.exe - 4 error(s), 0 warning(s)
编译器vc6.0
怎么办?
怎么办?
怎么办?
怎么办?
怎么办?
怎么办?
Top
10 楼symbol68688(幻影s)回复于 2003-02-03 20:30:09 得分 0
using namespace std;
干嘛用的?
写别的行不行?Top
11 楼symbol68688(幻影s)回复于 2003-02-03 20:40:39 得分 0
using namespace std;
在一个类中怎么写?
类中定义的字符串变量一个都不对Top
12 楼stonerain(shift)回复于 2003-02-03 20:41:31 得分 0
你用string 就可以了。别忘了在前面定义“#include<string>"
如果用char s[]的话要在前面定义"#include<cstring>",还要定义你string的大小。Top
13 楼langhaixin(C++如此多娇,引无数高手尽折腰!)回复于 2003-02-03 21:53:59 得分 10
to symbol68688(幻影s)你的源码根本就是 C风格 用的是C的库 而且C是没有string这个类的,也没有class的概念
#include <string>
int main(int argc, char* argv[])
{
std::string s("Hello,World!");
std::cout<<s;
return 0;
}
Top
14 楼Frank001(Frank)回复于 2003-02-03 21:56:36 得分 5
加这两句
#include<iostream>
using namespace std;
在VC6里 <string> 可以不用写
Top
15 楼SilverChariot(丁丁在CSDN)回复于 2003-02-03 21:59:28 得分 0
gzTop
16 楼Frank001(Frank)回复于 2003-02-03 22:00:33 得分 0
补充<string> 最好也加上去,不然的话,有些对string的操作会不行.
Top
17 楼dzhcheng(逸枫)回复于 2003-02-04 20:39:53 得分 5
声明一下名字空间std
或者std::string 的Top
18 楼ehom(?!)回复于 2003-02-04 20:56:26 得分 0
用字符数组,绝对通用Top
19 楼cxjddd(又是花开时)回复于 2003-02-04 21:16:26 得分 0
最好这样:
string s("asdfa");Top




