public:
string str;
Test(string& str){
this->str=str;
cout<<"constructor"<<endl;
}
};
int main() {
Test t="test";
return 0;
}
在这段代码中我得到错误"..\src\Test.cpp:30:9: error: conversion from 'const char [5]' to non-scalar type 'Test' requested "
?
那么为什么下面的代码可以呢?
#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;
}