c++中的string初始化

jidakuner jidakuner 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我做的测试发现下面的代码可以测试通过:
g++ q.cc -o q
#include <iostream>
#include <string>

int main()
{
std::string tmp;
tmp="123";
std::cout<<tmp<<std::endl;
}

但是string tmp变成string tmp(),如下代码就会出错:
#include <iostream>
#include <string>

int main()
{
std::string tmp(); //这里只是多了一个括号()
tmp="123";
std::cout<<tmp<<std::endl;
}

错误是:
q.cc: In function 'int main()':
q.cc:7:6: error: assignment of function 'std::string tmp()'
q.cc:7:6: error: cannot convert 'const char [4]' to 'std::string()' in assignment
...全文
评论 11
收藏 已收藏
分享
返回顶部
jidakuner
C++ 语言 65,214 成员 250,514 内容