问题:
t->package().ship_id(sqlRow[1]);
其中 ship_id为 结构体package中的string类型。
以下:
typedef struct Package
{
string ship_id;
....
}Package_t;sql
给ship_id赋值时报错:翻译
no match for call to ‘(std::__cxx11::string {aka std::__cxx11::basic_stringcode
参考答案:ip
翻译一下以下答案:string
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.it
由于这不是初始化, 这是赋值。赋值和初始化一块儿时可使用"="符号。io
Initialization is what gives a value to an object upon construction. function
初始化是在已经构形成功的基础上进行的。基础
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