为什么string在类的构造函数里不能用

u010235951 2013-06-02 09:03:33
比如
class Student
{private:
string name;
public:
Student(string n):name(n){} //为什么这样不对
//要这样 Student(const char*n):name(n){}

}
int main()
{
Student s("Nick");

return 0;
}


求解
...全文
478 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sumos 2013-06-06
  • 打赏
  • 举报
回复
应该是与string的拷贝构造函数冲突了 string(const string &str){}
橡木疙瘩 2013-06-06
  • 打赏
  • 举报
回复
楼主用的什么编译器?用的哪个stl库? 在我这里是没有问题的(除了类定义少个分号) 检查一下你的stl库中basic_string的构造,如果

basic_string( char const *)
或是

basic_string( char const *, A const & = A())
类型的构造函数被加上了explicit声明,那么Student s("Nick")就无法通过编译,必须显示指明char const * 到string的转换:Student s(string("Nick"));
billzheng 2013-06-06
  • 打赏
  • 举报
回复
It's so interesting that no one answers a correct answer in such a simple question:
Student(const string& n):name(n){}
Student s("Nick");
Use const reference which elides one copy of parameter, also it allows to bind to a rvalue.
我看你有戏 2013-06-06
  • 打赏
  • 举报
回复

class Student
{private:
string name;
public:
	Student(string n):name(n.c_str()){} //为什么这样不对
	//要这样 Student(const char*n):name(n){}

};
int main()
{
	Student s(string("Nick"));

	return 0;
}
试试这个,理解string的构造函数的参数
snake_xiongyang 2013-06-06
  • 打赏
  • 举报
回复
我试了
#include<string>
using namespace std;
class Student
{
    private:
string name;
public:
Student(string n):name(n.c_str()){}//为什么这样不对  实际对的
  //要这样 Student(const char*n):name(n){}

};
int main()
{
Student s("Nick");

return 0;
}
turing-complete 2013-06-02
  • 打赏
  • 举报
回复
引用 6 楼 adlay 的回复:
好吧 既然你愿意把一个错误的答案当作正解我也不能阻止你. 但是出于人道, 我还是提醒一下:
大尾巴猫 2013-06-02
  • 打赏
  • 举报
回复
可以用const char* n做形参,也也可以用const string & str做形参。 看你构造的时候传什么类型。 对于默认的数据类型,参数一般直接传值,对于对象类型,参数传引用。这样效率高。
www_adintr_com 2013-06-02
  • 打赏
  • 举报
回复
引用 3 楼 u010235951 的回复:
[quote=引用 2 楼 adlay 的回复:]
不会哦, Student(string n):name(n){} 没有错, 只是效率可能有问题, 最好是 Student(const string& n):name(n){}
有问题的是, 类定义没有用 ; 结束!


不好意思,我打上了忘记打了.1楼正解 也谢谢你[/quote]

好吧
既然你愿意把一个错误的答案当作正解我也不能阻止你.
但是出于人道, 我还是提醒一下:

1. 默认构造函数是不带参数的构造函数, string(char*)是不能称为默认构造函数的.
2. string 是有 string(const char*) 这种形式的构造函数的.
qq453345496 2013-06-02
  • 打赏
  • 举报
回复
引用 4 楼 u010235951 的回复:
[quote=引用 1 楼 u010928856 的回复:] "name"这个是C字符串吧,C字符串可以给string赋值,但是string没有默认构造函数string(char*),你可以试试Student s(string("Nick"));
ok,这样就可以了[/quote] 我也刚开始学习,有用就赏点分吧
u010235951 2013-06-02
  • 打赏
  • 举报
回复
引用 1 楼 u010928856 的回复:
"name"这个是C字符串吧,C字符串可以给string赋值,但是string没有默认构造函数string(char*),你可以试试Student s(string("Nick"));
ok,这样就可以了
u010235951 2013-06-02
  • 打赏
  • 举报
回复
引用 2 楼 adlay 的回复:
不会哦, Student(string n):name(n){} 没有错, 只是效率可能有问题, 最好是 Student(const string& n):name(n){} 有问题的是, 类定义没有用 ; 结束!
不好意思,我打上了忘记打了.1楼正解 也谢谢你
www_adintr_com 2013-06-02
  • 打赏
  • 举报
回复
不会哦, Student(string n):name(n){} 没有错, 只是效率可能有问题, 最好是 Student(const string& n):name(n){} 有问题的是, 类定义没有用 ; 结束!
qq453345496 2013-06-02
  • 打赏
  • 举报
回复
"name"这个是C字符串吧,C字符串可以给string赋值,但是string没有默认构造函数string(char*),你可以试试Student s(string("Nick"));

64,648

社区成员

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

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