admin管理员组

文章数量:1667081

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

解决办法

#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;
}
#

本文标签: constconversionErrortypescalar