gpt4 book ai didi

c++ - 如何修复编译器错误 "class has no member named X"?

转载 作者:太空宇宙 更新时间:2023-11-04 16:25:13 26 4
gpt4 key购买 nike

<分区>

我目前正在编写一个表达式求值器,遇到了一个关于多态性的问题。我的类层次结构如下: Divide 继承自 Operator,Operator 继承自基类 Expression。当我使用基类 Expression 实例化类型为 Divide 的对象并尝试访问函数 divide(int, int) 时,出现以下错误:

"Expression.cpp: 在成员函数‘int Expression::evaluate()’中:Expression.cpp:37:6: 错误:‘class Expression’ 没有名为‘divide’ 的成员”

这里是“Expression.h”

#ifndef EXPRESSION_H
#define EXPRESSION_H
#include <string>
using namespace std;
class Operator;
class Divide;
class Expression
{
protected:
char **ana, *exp;

public:
Expression();
~Expression();
Expression(char* ex);
int evaluate();
void tokenize();

class EmptyException
{
public:
EmptyException(string a){reason = a;};
string getReason() const{return reason;};
private:
string reason;
};

};

#endif

这里是“Expression.cpp”

#include "Expression.h"
#include "Stack.h"
#include "Queue.h"
#include "Node.h"
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include "Operator.h"
#include "Divide.h"

Expression::Expression()
{

}

Expression::~Expression()
{
delete [] exp;
}

Expression::Expression(char* ex)
{
exp = ex;
//tokenize();

}

int Expression::evaluate()
{
Stack stack;

Expression *tmp;
Divide d;
tmp = &d;
tmp->divide(4, 2);
stack.push(tmp);
tmp = stack.pop();

}

void Expression::tokenize()
{
int space = 0;

for(int i =0 ; i < strlen(exp); i++)
{
if(exp[i] == ' ')
space++;
}

char ** ana = new char*[space + 1];
ana[0] = strtok(exp, " ");

for(int i = 1 ; i < space + 1; i++)
{
ana[i]= strtok (NULL, " ");
}

}

现在是“operator.h”

#ifndef OPERATOR_H
#define OPERATOR_H
#include "Expression.h"

using namespace std;

class Operator : public Expression
{
protected:
bool unary, binary;
public:
Operator();
bool isUnary() const;
bool isBinary() const;
};

#endif

"运算符.cpp"

#include "Operator.h"
#include <string>
#include <cmath>
#include "Expression.h"

Operator::Operator()
{

}

bool Operator::isUnary() const
{
if(unary)
return true;
else
return false;

}

bool Operator::isBinary() const
{
if(binary)
return true;
else
return false;
}

“除法.h”

#ifndef DIVIDE_H
#define DIVIDE_H

#include <string>
#include "Operator.h"
class Expression;
using namespace std;


class Divide : public Operator
{
private:
char id;
public:
Divide();
Divide(char);
int divide(int a, int b);
char identity() const;
};

#endif

“分割.cpp”

#include "Divide.h"

int Divide::divide(int a, int b)
{
return a/b;
}

Divide::Divide(char _id)
{
id = _id;
}

char Divide::identity() const
{
return id;
}

万一生成文件

main: Expression.o Equation.o Operator.o Divide.o Stack.o Queue.o main.o
g++ -static main.o Equation.o Expression.o Operator.o Divide.o Stack.o Queue.o -o main

main.o : main.cpp
g++ -c main.cpp

Equation.o : Equation.cpp Equation.h
g++ -c Equation.cpp

Expression.o : Expression.cpp Expression.h
g++ -c Expression.cpp

Operator.o : Operator.cpp Operator.h Expression.h
g++ -c Operator.cpp

Divide.o : Divide.cpp Divide.h Operator.h
g++ -c Divide.cpp

Stack.o : Stack.cpp Stack.h Node.h
g++ -c Stack.cpp

Queue.o : Queue.cpp Queue.h Node.h
g++ -c Queue.cpp

我省略了所有其他运算符,例如 eg.加号、减号等,因为它们都与 Divide 相同,只会使这个问题进一步复杂化。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com