首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

string跟指针

2013-04-21 
string和指针#include iostream#include stringusing namespace stdint main(){string ahellocha

string和指针


#include <iostream>
#include <string>
using namespace std;
int main()
{
    string a="hello";
    char *b=a;//a不是一个地址吗?为什么这样赋值不行呢
    cout<<b;
    system("pause");
    return 0;
}

[解决办法]
a是一个std string模板类对象 不是直接地址
[解决办法]
char*b=a.c_str();这样就可以了
[解决办法]
a是一个string对象,不是地址。


string a="hello";
改成
char a[] = "hello";或char* a = "hello";
应该可达到你想要的情形。
[解决办法]
很可能是string对=操作符进行了重载

热点排行