我创建了一种从车主那里交换汽车的方法。
bool owner::changeCar(owner& other, car& mCar){
if(this == &other) return false;
car* m = &this.mCar;
this->mCar =&mCar;
other.mCar = m;
return true;
}
但是我收到错误
Request for member ‘mCar’ in ‘this’, which is of non-class type ‘owner* const’
在线 car* m = &this.mCar;
。
有人知道我做错了什么。 类主有私家车。
最佳答案
你在下面的行中做对了; this
是一个指针,因此您需要->
来访问它的成员mCar
。此外,mCar
显然已经是一个指针(至少您将其用作一个指针),因此它之前的 &
是多余的。
car* m = this->mCar;
关于c++ - 请求 ‘mCar’中的成员 ‘this’,非类类型 ‘owner* const’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9838800/