开发者

Fixing C4150: deletion of pointer to incomplete type COldStuff without including header or changing old code

开发者 https://www.devze.com 2023-02-17 23:43 出处:网络
How to get around error C4150: deletion of pointer to incomplete type COldS开发者_C百科tuff; no destructor called

How to get around error

C4150: deletion of pointer to incomplete type COldS开发者_C百科tuff; no destructor called

without adding the header. E.g.

class COldStuff;

class CSomething
{
     ...
     CAnother<COldStuff> m_test;
}

I am using legacy code for all this (ie COldStuff, CAnother, CSomething). When I add the header to fix c4150, I run into circular dependency and especially redefinition problems. How else can I get around this problem; if I cannot turn off treat warning as error, or ignore this, or change the legacy code.... Can I somehow let the compiler know COldStuff's destructor or that COldStuff does have a destructor?


Potential fix: Explicitly define the destructor for CSomething in your source file (even if it's just empty).

Why this probably works: From the comments and Rup's excellent question, I assume that CAnother tries to delete COldStuff, at which point it has to be visible. Now if you do not explicitly define a destructor for CSomething, that part is instantiated inline too (basically in the header, giving you the error). However, if you explicitly define a destructor in the cpp file of CSomething, the implicit CAnother<COldStuff>::~CAnother is instantiated there and you can resolve the circular dependency.


Oh, that's bad.

Maybe you could introduce a base class with a virtual destructor, and delete that way. But you can't cast an incomplete type to its base class, nor can you call a member function which would know how to delete it properly.

I think ultimately whatever you pass these COldStuff objects to that knows how to use them, also needs to be responsible for deleting them.

At least some small changes to the legacy code will be necessary to fix this.


This isn't something you can get away with. When deleting an object, the compiler has to see the complete type. What if it has a destructor?

The usual fix is to put the delete in a cpp file where the complete type is visible. might be hard if the deleter is a template, but perhaps you can use a small helper function.


Replace forward declaration of COldStuff by including its header file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号