MSVC++ compiler error C2143

前端 未结 3 775
孤独总比滥情好
孤独总比滥情好 2021-01-25 12:45

The following code excerpt is responsible for a cryptic MSVC++ compiler error:

template class Vec : public vector{
  public:
    Vec() :          


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 13:23

    Why are you attempting to inherit from vector? This will cause you a lot of problems. The least of which is that vector does not have a virtual destructor. This will cause the wrong destructor to be called when deleting a polymorphic reference to your class which will lead to memory leaks or general bad behavior.

    For instance, the following code will not call ~Vec() but will call ~vector() instead.

    vector *pVec = new Vec();
    delete pVec;  // Calls ~vector();
    

    The actual compile error you are seeing though is because you're using the template syntax for the base constructor call. Simply remove that and it should compile

    Vec() : vector() {}
    

提交回复
热议问题