no appropriate default constructor available

//classbase.h

class CClassBase 
{
public:
 CClassBase(int a,int b=2,int c=3);
 virtual ~CClassBase();
protected:
 int m_a;
 int m_b;
 int m_c;

};

//classbase.cpp

CClassBase::CClassBase(int a,int b,int c):m_a(a),m_b(b),m_c(c)
{

}

CClassBase::~CClassBase()
{

}

 

//classb.h

class CClassB 
{
public:
 CClassB();
 virtual ~CClassB();
 CClassBase m_test;
};

//classb.cpp

CClassB::CClassB()
{

}

CClassB::~CClassB()
{

}

 

报错:error C2512: 'CClassBase' : no appropriate default constructor available

解决:

CClassB::CClassB():m_test(0)
{

}

原因很简单,m_test的产生是在classB的构造函数中,因此在这里才会检查到cclassB的构造函数必须传入参数

多做总结,有好处!

你可能感兴趣的:(c,Constructor)