私はこの1つの問題を一日中終わらせましたが、誰かがそれを私に説明することができますか?私は私の正確なコードを提供していませんが、私が同行しようとしているもの(と短縮バージョン)の同様の状況。それはレストランのシミュレータです。ポインタへの文字列参照の割り当て
table.cc:9:18: error: cannot convert ‘const string {aka const std::__cxx11::basic_string}’ to ‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ in assignment this->tableID = tableID;
は、誰もが何が起こっているのかを教えてもらえます:
int main() {
Restaurant i;
string tableID;
cin >> tableID;
i.append(new Table(tableID));//this appends a new table to the linked list
}
//.h file we're not allowed to modify
class Table
{
public:
Table(const string& tableID);//constructor
private:
string* tableID;
}
// .cpp file I'm writing
Table::Table(const string& tableID){
this->tableID = tableID;
}
私はエラーを取得していますか?私にとってはtableID
のアドレスをmain()
に渡していて、コンストラクタのポインタに代入しているようですが、それは合法だと思っていましたが、それはconst
を投げ捨てています。
エラーメッセージは明らかです。 'string'の型の変数に' string'型の式を代入しようとしています。 'int * p = 42;と同じ理由でうまくいきません。うまくいかず、意味がありません。 –
そうだと思ったけど、どうやってその文字列をポインタに変換するのか分からなかった。 – Mike1982