原因:对在类外对某个的拷贝构造函数或者赋值运算符函数进行实现,但是在类中没有进行声明。
如下:
test.h:
class Data
{
public:
Data(Data*other = nullptr);
~Data();
private:
Data* m_pdate;
};
test.cpp:
#include "test.h"
#include <utility>
Data::Data(Data* other)
:m_pdate(other)
{}
Data::Data(const Data& other)
{
if (other.m_pdate != nullptr)
{
m_pdate = new Data(*other.m_pdate);
}
}
Data& Data::operator= (const Data& other)
{
if (this != &other)
{
Data tmp = other;
std::swap(tmp,*this);
}
return *this;
}
Data::~Data()
{
if (m_pdate != nullptr)
{
delete m_pdate;
}
}
解决方法:在类中对重新实现的拷贝构造函数或者赋值运算符函数进行声明。
这种就属于编译错误,如果在类的外部重新实现了拷贝构造函数或者赋值运算符函数,但是在类中没有进行声明,因为类中默认就是有这两种函数的,只实现不声明编译的时候就会出现问题,vs的报错很奇怪,或者不明确。大概是vs自己的原因,有时候面对这个问题vs会报编译错误,有时候会出现标题的这种行为,就会让人很懵逼。