如何处理参数中的基类和指针变量
How to deal with base class and pointer variable in argument
我正在尝试处理抽象类参数,试图弄清楚该怎么做,在运行下面的代码后出现错误:"在函数'int main(('中:67:12:错误:'('标记之前的预期主表达式">
#include <iostream>
#include <string>
using namespace std;
class base1 {
protected:
int i;
public:
base1(int x) {
i=x;
cout << "Constructing base1n";
}
virtual ~base1() {
cout << "Destructing base1n";
}
};
class derived: public base1 {
int j;
public:
derived(int x, int y): base1(y){
j=x;
cout << "Constructing derivedn";
}
~derived() {
cout << "Destructing derivedn";
}
void show() {
cout << i << " " << j << " " << "n";
}
};
class Isolver
{
public :
Isolver(){};
virtual ~ Isolver(){};
virtual void x(base1* pboard)=0;
};
class vr:public Isolver
{
void x(base1* pboard)
{
cout << "My virtual fun and base constructor are not workingn"<<endl;
};
};
int main()
{
vr obj;
obj. x(10, );
derived ob(3,4);
ob.show();
return 0;
}
就像保罗·鲁尼指出obj.x(10,)
里面的逗号不应该存在,因为使用逗号编译器需要两个参数,并且由于逗号后没有任何内容,因此它会显示错误。这是你需要在main中做的事情:
vr obj;
base1 b(1);
obj. x(&b);
或者这个:
vr obj;
derived ob(3,4);
obj. x(&ob);
ob.show();
和内部class vr
class vr:public Isolver
{
public:
void x(base1* pboard)
{
cout << "My virtual fun and base constructor are not workingn"<<endl;
}
};
相关文章:
- 如何使用基类指针引用派生类成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 使用基类指针调用基类的值构造函数的语法是什么?
- 如何在基类指针向量的元素上应用重载的多态函数
- C++函数解析基类指针到派生类指针
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 我们可以在不知道其真实类型的情况下将基类指针转换为派生类指针吗?
- 从基类指针派生派生类的模板类型
- 当键是虚拟继承中涉及的基类指针时,对 std::unordered_map 项的访问崩溃
- 如何使用函数(而不是构造函数)将派生类对象分配给基类指针
- 创建基类指针的向量并将派生类对象传递给它(多态性)
- 无法将派生类存储在基类指针的向量中
- 有没有办法在没有虚拟的情况下使用基类指针调用派生类函数
- 基类指针:在哪里使用 new 和 delete
- 如何在CRTP实现中传递基类指针
- C++继承从基类指针访问派生类中的非虚拟函数
- 通过基类指针获取派生类对象的引用
- C++通过基类指针提升子类的序列化
- 将派生对象分配给函数内的基类指针
- 如果基类指针无法访问派生类成员函数,那么多态性有什么方便的呢?