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");参考
{
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");参考
相关文章推荐
- 在MyEclipse里部署web项目
- MySQL优化实例
- MySQL优化步骤和my.cnf优化配置
- OpenCV中Mat与IplImage和CvMat类型之间的相互转换
- MapReduce英语面试
- MySQL优化步骤和my.cnf优化配置
- MySQL优化实例
- 顺序输出从1到N位的所有数字(N可能是一个大数位)。
- 从javascript的循环问题来看待闭包本质
- linux高负载下mysql数据库彻底优化
- HttpWatch工具简介及使用技巧(转载)
- POJ 3276
- linux高负载下mysql数据库彻底优化
- 冒泡排序算法及其优化
- MySQL配置文件my.cnf详解
- MySQL配置文件my.cnf详解
- ubuntu 命令 笔记 持续更新
- hdu 2202 最大三角形_凸包模板
- 司以类聚,人以群分
- hdu 2202 最大三角形_凸包模板