尝试引用子类

时间:2017-10-30 17:37:24

标签: c++

我试图理解在c ++中创建多态解决方案的纯虚方法。我目前有3个类,表达式(抽象),常量(表达式的子)和unop(表达式的子)。类unop保存对另一个表达式的引用(它可以保存unop,constant或将来的binop)。当我尝试编译unop.cpp时,我收到以下错误:

unop.cpp:6:20: error: ‘expression’ is not a type
 unop::unop(char c, expression *e){
                    ^
unop.cpp:6:1: error: prototype for ‘unop::unop(char, int*)’ does not match any in class ‘unop’
 unop::unop(char c, expression *e){
 ^
In file included from unop.cpp:2:0:
expression.h:21:7: error: candidates are: unop::unop(const unop&)
 class unop: public expression{
       ^
expression.h:23:2: error:                 unop::unop(char, expression*)
  unop(char, expression*);
  ^
expression.h:29:14: error: ‘expression* unop::expression’ is private
  expression *expression;
              ^
unop.cpp:6:20: error: within this context
 unop::unop(char c, expression *e){

我的代码如下:

expression.h

class expression{
public:
    expression(){}
    virtual ~expression() {}
    int virtual eval() = 0;

    char virtual * infix() = 0;
    char virtual * postfix() = 0;
};

class constant : public expression{
public:
    constant(int);
    int eval();
    char * infix();
    char * postfix();
private:
    int constantInt;
};

class unop : public expression{
public:
    unop(char, expression*);
    int eval();
    char * infix();
    char * postfix();
private:
    char operatorChar;
    expression *expression;
};

constant.cpp

#include "expression.h"
#include <stdio.h>
#include <stdlib.h>

constant::constant(int i){
        constantInt = i;

}

int constant::eval(){
    return constantInt;
}

char * constant::infix(){
    char *retval = (char *) malloc(100 * sizeof(char));
    sprintf(retval, "%d", constantInt);
    return retval;
}

char * constant::postfix(){
    return infix();
}

unop.cpp

#include "expression.h"
#include <stdio.h>
#include <stdlib.h>

unop::unop(char c, expression *e){
        operatorChar = c;
        expression = e;
}

int unop::eval(){
    return 0;
}

char * unop::infix(){
    return NULL;
}

char * constant::postfix(){
    return NULL;
}

的main.cpp

#include <iostream>
#include "expression.h"

using namespace std;
int main(){

    expression *test = new constant(5);

    cout << test -> eval();

    return 0;

}

任何帮助将不胜感激。我开始慢慢了解发生了什么,但我发现这比我使用的其他语言更复杂。

2 个答案:

答案 0 :(得分:1)

问题是:

class unop : public expression{
public:
    unop(char, expression*);
    int eval();
    char * infix();
    char * postfix();
private:
    char operatorChar;
    expression *expression; // <<<HERE
};

当你写:

unop::unop(char c, expression *e)
expression的上下文中查找

unop,并找到本地成员变量; 不是基类。

您需要更改成员的名称:expr或许?

修改:此外,正如Raindrop7指出的那样,unop.cpp定义constant::postfix的另一个(不同)副本,而非定义unop::postfix。看起来像是复制粘贴错误!

答案 1 :(得分:0)

有一些错误:

expression *pExpression;

因此,只要您找到expression =,就将其更改为pExpression =

您不能将类型用作以下标识符:

int int = 0; // error

所以将其更改为其他内容:

 expression* pExpression; // for example

此外,您还有链接时错误:您必须在派生类中实现所有纯虚方法才能实例化派生类:

我认为你的意思是:

char * unop::postfix(){ // not char * constant::postfix in Unop.cpp
    return NULL;
}