問題:
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