如何处理参数中的基类和指针变量

How to deal with base class and pointer variable in argument

本文关键字:基类 指针 变量 参数 何处理 处理      更新时间:2023-10-16

我正在尝试处理抽象类参数,试图弄清楚该怎么做,在运行下面的代码后出现错误:"在函数'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;
    }
};