C++ 如何修复编译器错误“类没有名为 X 的成员”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12679693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:30:49  来源:igfitidea点击:

How to fix compiler error "class has no member named X"?

c++objectinheritancepolymorphismvirtual

提问by Chris

I'm currently coding an expression evaluator and have run into an issue regarding polymorphism. My class hierarchy is as follows: Divide inherits from Operator which inherits from the base class Expression. When I instantiate an Object of type Divide with base class Expression and try to access the function divide(int, int) i get the following error:

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

"Expression.cpp: In member function ‘int Expression::evaluate()': Expression.cpp:37:6: error: ‘class Expression' has no member named ‘divide'"

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

Here is "Expression.h"

这是“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

And here is "Expression.cpp"

这是“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, " ");
   }

  } 

And now the "operator.h"

现在是“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

"Operator.cpp"

“操作员.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; 
}

"Divide.h"

“除.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

"Divide.cpp"

“划分.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;
}

and for in case the makefile

以防万一makefile

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

I have left out all the other operators such as eg. Plus, minus etc because they are all identical to what Divide is and would just complicate this question even further.

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

回答by Luchian Grigore

It's pretty clear, right? Expressiondoesn't have the method divide.

这很清楚,对吧?Expression没有方法divide

Expression *tmp;
Divide d;
tmp = &d;
tmp->divide(4, 2); //tmp is an Expression*

Also... why would you do this?What's wrong with:

还有……你为什么要这么做?有什么问题:

Divide d;
d.divide(4,2);

回答by bohney

Your problem is that you are calling divideon the variable called tempwhich is a pointer to an Expressionhere: tmp->divide(4, 2);You should call divide on your dvariable.

你的问题是你正在调用被调用divide的变量temp,它是一个指向Expression这里的指针:tmp->divide(4, 2);你应该对你的d变量调用divide 。