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

class test
{
public:
    string str;
    Test(string& str){
        this->str=str;
        cout<<"constructor"<


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


解决办法

1

#include 
using namespace std;

class Test{

public:
    string str;

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

};

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

#include 

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");
参考


你可能感兴趣的:(c++)