所有
我在继承链中有3个类(在C ++中),每个类都设置了默认的基础构造函数。但是,链中的第3类抱怨第1个没有匹配的构造函数。下面是构造函数的代码:
class Base
{
protected:
int a, b;
public:
Base::Base(int first, int second)
{
this->a = first;
this->b = second;
}
}
class Second : protected Base
{
protected:
int c;
public:
Second::Second(int first, int second, int third = 2) : Base(first, second)
{
this->c = third;
}
}
class Final : protected Second
{
private:
int d;
public:
Final::Final(int first, int second, int third = 2) : Second(first, second, third)
{
}
}
在编译时我收到错误
"在构造函数Final :: Final(int first,int second,int third)
中没有与Base()"
的匹配调用为什么这会尝试调用Base()而不是Base(第一,第二)?
答案 0 :(得分:0)
这可能会帮助您构建此类层次结构。
class Base {
protected:
int m_a, m_b;
public:
Base( int first, int second );
virtual ~Base(){}
};
Base::Base( int first, int second ) :
m_a( first ),
m_b( second )
{}
class Second : protected Base {
protected:
int m_c;
public:
Second( int first, int second, int third = 2 );
virtual ~Second(){}
};
Second::Second( int first, int second, int third ) :
Base( first, second ),
m_c( third )
{}
class Final : protected Second {
private:
int m_d;
public:
Final( int first, int second, int third = 2 );
virtual ~Final(){}
};
Final::Final( int first, int second, int third ) :
Second( first, second, third )
{}
如果你想在类中保留你的定义而不是在类声明之外定义它们,那么试试这个
class Base {
protected:
int m_a, m_b;
public:
Base( int first, int second ) : m_a( first ), m_b( second ) {}
virtual ~Base(){}
};
class Second : protected Base {
protected:
int m_c;
public:
Second( int first, int second, int third = 2 ) :
Base( first, second ),
m_c( third ) {}
virtual ~Second(){}
};
class Final : protected Second {
private:
int m_d;
public:
Final( int first, int second, int third = 2 ) :
Second( first, second, third ) {}
virtual ~Final(){}
};
优良作法是确保在使用继承时将析构函数声明为虚拟。如果要在类外部定义构造函数或函数,则只需使用类作用域解析运算符,并设置基本成员变量,不需要使用this->指针运算符,您可以使用我在定义构造函数时显示的初始化列表。
答案 1 :(得分:0)
首先,在类定义结束时缺少select *
from pointlayer as p
left join linelayer as l on
Distance(p.geometry, l.geometry) in (
select MIN(Distance(p.geometry, geometry))
from linelayer
where pk in (
select rowid
from SpatialIndex
where f_table_name = 'linelayer'
and search_frame = BuildCircleMbr(X(b.geometry), Y(b.geometry),25))
)
。此外,在类中实现成员函数时,不能使用得分运算符;
,它在C ++中无效。您可能会发现This有用。