立此存照(17)[C++]error C2040: "s":"std::string"与"char [21]"的间接寻址级别不同

来源:互联网 发布:网络作家富豪榜2017 编辑:程序博客网 时间:2024/05/16 06:43
#include <iostream>#include <cstdlib>#include <vector>#include <list>#include <deque>#include <string>using namespace std;int main(){char s[] = "Hello string of C11.";vector<char> cvec(s, s + sizeof(s) / sizeof(s[0]));string s(cvec.begin(), cvec.end());cout << s << endl;//system("pause");return 0;}

报错:

错误1error C2040: “s”:“std::string”与“char [21]”的间接寻址级别不同
错误<span style="white-space:pre"></span>2<span style="white-space:pre"></span>error C2088: “<<”: 对于 class 非法
虽然看到错误,但当时没理解。

原来是变量名重定义。char数组名s,与string对象名s,冲突了,但编译器报错,让我一时没转过弯。


将string对象s重命名为str,问题解决:

#include <iostream>#include <cstdlib>#include <vector>#include <list>#include <deque>#include <string>using namespace std;int main(){char s[] = "Hello string of C11.";vector<char> cvec(s, s + sizeof(s) / sizeof(s[0]));string str(cvec.begin(), cvec.end());//cout << str << endl;//system("pause");return 0;}







0 0
原创粉丝点击