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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM