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

请各位高手指点一下,这个题目哪里错了。。(最好说明一下c_str的用法)

ku_klox 发布于 2010-08-17 13:58, 678 次点击
#include <iostream>
#include <string>
using namespace std;
class A
{
private:
    string name;
public:
    A();
    void show();
};
A::A()
{
    name.assign("ku");
}
void A::show()
{
    cout<<name.c_str<<endl;
   
}
void main()
{
    A b();
    b.show();
}
3 回复
#2
ciweitou1632010-08-17 14:08
程序代码:
#include <iostream>
#include <cstring>        //C++编译的

using namespace std;

class A
{
private:
    string name;
public:
    A();
    void show();
};
A::A()
{
    name.assign("ku");
}
void A::show()
{
    cout<<name.c_str()<<endl; //The function c_str() returns a const pointer to a regular C string,
                                                     
//identical to the current string.
   
}
int main()
{
    A b;
    b.show();
   
    return 0;
}
#3
pangding2010-08-18 01:04
你就 cout << name << endl; 又有何不可?
#4
b8705162010-08-18 09:31
#include <iostream>
#include <string>
using namespace std;
class A
{
private:
    string name;
public:
    A();
    void show();
};
A::A()
{
    name.assign("ku");
}
void A::show()
{
    cout<<name.c_str()<<endl;//这里加上圆括号
   
}
void main()
{
    A b;//你把b后面的括号去掉就行了。
    b.show();
}
1