您的位置:首页 > 其它

error: conversion from 'const char [ ]' to non-scalar type

2014-03-29 13:23 495 查看
class test
{
public:
string str;
Test(string& str){
this->str=str;
cout<<"constructor"<<endl;
}

};

int main() {
Test t="test";
return 0;
}

error: conversion from 'const char [ ]' to non-scalar type

解决办法

1
#include <iostream>
using namespace std;

class Test{

public:
string str;

Test(string str){
this->str=str;
cout<<"constructor"<<endl;
}

Test(const Test &test){
cout<<"copy constructor"<<endl;
this->str=test.str;
}

};

int main() {
Test t=Test("test");
return 0;
}2
#include <string>

struct Test
{
Test(const char* c) : s_(c) {}
std::string s_;
};

int main()
{
Test t = "Hello";
}3
Test t("test");

or

Test t = string("test");参考
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: