Crane.cpp:18: error: Could not convert ‘((Crane*)this)->Crane::ship.Ship::operator=(((const Ship&)(& Ship(0,std::basic_string,std::allocator >(((const char*)”arrive”),((const std::allocator&)((const std::allocator)(& std::allocator())))),std::allocator >(((const char)”Ship”),((const std::allocator&)((const std::allocator*)(& std::allocator()))))))))’ to ‘bool’
Crane::Crane(int craneID,int cranestatus,bool free,Ship ship){ setCraneID(craneID); setCranestatus(cranestatus); setFree(free); setShip(ship);}Crane::Crane(){}Crane::~Crane(){}voID Crane::print(){ cout << "Crane ID: " << craneID << endl; cout << "Crane Status: " << cranestatus << endl; cout << "Crane is free: " << free << endl; if (ship = NulL) //this is the problem { cout << " " << endl; } else { ship.print();//i have another print method in the Ship class }}
我试过了
if (ship == NulL)
但我收到此错误消息
Crane.cpp:18: error: no match for ‘operator==’ in ‘((Crane*)this)->Crane::ship == 0’
怎么做对吗?
解决方法 那是因为ship不是Ship的指针,即Ship *,但它本身就是Ship对象;那么你不能将它转换为0,这是…指针的空地址.如果你想要一个指向Ship的指针你应该这样做
ShiP* ship = new Ship;// Catch a std::bad_alloc exception if new fails
然后,如果您将指针作为函数参数获取,则可以测试它是否为null:
voID foo(ShiP* ship_pointer){ if(ship_pointer == 0) // Oops pointer is null... else // Guess it's OK and use it.}总结
以上是内存溢出为你收集整理的c中的空对象全部内容,希望文章能够帮你解决c中的空对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)