no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string

问题:
 t->package().ship_id(sqlRow[1]);
其中 ship_id为 结构体package中的string类型。
如下:

typedef struct Package
{
    string ship_id;
    ....
}Package_t;

给ship_id赋值时报错:

no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string

 

参考答案:

https://stackoverflow.com/questions/17262984/c-error-no-match-for-call-to-stdstring-aka-stdbasic-stringchar-st

翻译一下如下答案:

Because that's not an initialization. That's an assignment. Both assignment an (copy-)initialization make use of the = sign, but don't let that fool you: the two things are fundamentally different.

因为这不是初始化, 这是赋值。赋值和初始化一起时可以使用"="符号。

Initialization is what gives a value to an object upon construction.

初始化是在已经构造成功的基础上进行的。

When your setName() member function gets called, the object on which it is invoked (as well as its data members) have already been constructed.

If you want to initialize them there, you're late: you've missed the train.

如果你在构造时没有使用赋值初始化,后面再使用就会出现问题。

In a constructor's initialization list, on the other hand, you could initialize your data members as follows:

Course::Course(std::string name) : courseName(std::move(name)) { }
//                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                                 This would be initialization

转载于:https://www.cnblogs.com/rohens-hbg/p/11303349.html

你可能感兴趣的:(no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_string)