不知错在哪里

renyaoming 2009-03-09 11:36:13
#include<iostream>
using namespace std;
#include<string>

int main()
{
string *a="123";

cout<<*a;

}


怎么不对
...全文
225 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
houzui 2009-03-16
  • 打赏
  • 举报
回复

string str(“123”);
or
string str = “123”;

string *pstr = &str;

cout < < *pstr;
Muer_yeah 2009-03-16
  • 打赏
  • 举报
回复
18楼是正确的。。。我在VC运行过。。。!!
cheidtf 2009-03-14
  • 打赏
  • 举报
回复
好多种写法啊!
zhaohongbo83 2009-03-14
  • 打赏
  • 举报
回复
mark
victor1960 2009-03-14
  • 打赏
  • 举报
回复
这么多人多这个小问题感兴趣啊!希望大家一直这样帮忙解决问题!
wuguang1235 2009-03-14
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
#include <string>

int main()
{
string s=123;
string *a=&s; //这里楼上的阐明了
cout < <*a;
return 0;//主函数main()需要返回值;}
lchchl92418qwe 2009-03-14
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
#include <string>

int main()
{
string s=123;
string *a=&s;

cout < <*a;

}
splendidvirtuouswu 2009-03-12
  • 打赏
  • 举报
回复
如何要使用指针的话,应该分配空间
点点灵犀 2009-03-12
  • 打赏
  • 举报
回复
up
w198905051989 2009-03-11
  • 打赏
  • 举报
回复
把 *a改成a不就得了啊!
jakqigle 2009-03-10
  • 打赏
  • 举报
回复
string str(“123”);
or
string str = “123”;

string *pstr = &str;

cout << *pstr;
wxgiter 2009-03-10
  • 打赏
  • 举报
回复
顶一下~~
zbihong 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 arong1234 的回复:]
string对象需要new来赋值,你直接给它一个"123"不能创建一个string对象
[/Quote]支持!!!!!!
zhyl105 2009-03-10
  • 打赏
  • 举报
回复
自我检讨了
回复错了
string是个类
回答到字符串指针去了
[Quote=引用 9 楼 zhyl105 的回复:]
string *a="123";
相当于
string *a;
a="123";
你定义的是个指针,初始化出错,a是个地址变量,初始化的应该是个地址

如7楼所示:
引用 7 楼 fableboy 的回复:
string str(“123”);
or
string str = “123”;

string *pstr = &str;

cout < < *pstr;


string str = “123”;
string *pstr = &str;
相当于
string *pstr;
pstr = &str;//初始化地址变量

[/Quote]
zhyl105 2009-03-10
  • 打赏
  • 举报
回复
string *a="123";
相当于
string *a;
a="123";
你定义的是个指针,初始化出错,a是个地址变量,初始化的应该是个地址

如7楼所示:
[Quote=引用 7 楼 fableboy 的回复:]
string str(“123”);
or
string str = “123”;

string *pstr = &str;

cout < < *pstr;
[/Quote]

string str = “123”;
string *pstr = &str;
相当于
string *pstr;
pstr = &str;//初始化地址变量
ypb362148418 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 yangch_nhcmo 的回复:]
C/C++ code#include<iostream>#include<string>usingnamespacestd;intmain()
{stringa("123");

cout<<a;return0;

}
[/Quote]

看看string的默认构造函数吧.
lingyin55 2009-03-09
  • 打赏
  • 举报
回复
int main()
{
string *a="123";

cout < <*a;

}

一种是设置一个string变量a,直接赋值然后输出,一种是利用指针,但是你要先分配内存空间(new)
yangch_nhcmo 2009-03-09
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;

int main()
{
string a("123");

cout << a;

return 0;

}
chin_chen 2009-03-09
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
#include <string>

int main()
{
string a="123";//改下吧,理由一楼说了。

cout <<a;

}
pengzhixi 2009-03-09
  • 打赏
  • 举报
回复
123是字符串常量。你这是将const char ×转换成string ×
改成 string s="123"; string × a=&s;
加载更多回复(2)

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧