I've been stuck on this one problem all day and maybe someone can explain it to me? I'm not providing my exact code, but a similar situation of what I'm trying to accompish (and the shortened version). It's a restaurant simulator.
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;
}
I'm getting the error:
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;
Can anyone tell me what's going on? To me it seems like it's passing the address of tableID in main() and assigning it to a pointer in the constructor, which I thought was legal but maybe it's the const throwing me off.
stringto a variable of type "pointer tostring". It doesn't work for the same reasonint* p = 42;wouldn't work - neither makes sense. - Igor Tandetnik