访问类函数时出现奇怪错误

时间:2012-12-16 11:14:00

标签: c++

  

可能重复:
  Default constructor with empty brackets

class Question{
      protected:
              int op1;
              int op2;
              string operate;
      public:
         Question();
};
class generateRandomQuiz:Question{
  public: 
         generateRandomQuiz();
         int getp1();
         int getp2();
         string getOp();
}; 

class checkAnswer:generateRandomQuiz{
  private:
          int Ans;
  public:
         checkAnswer(int ans);
};

 Question::Question()
{
  op1=23;
  op2=12;
  operate="/";
}

generateRandomQuiz::generateRandomQuiz():Question()
{
  op1=rand()%50;
  op2=rand()%50;
  string s="+-/*";
  int n=rand()%4;
  operate=s[n];
}

int generateRandomQuiz::getp1()
{
return op1;
}

int generateRandomQuiz::getp2()
{
return op2;
}

string generateRandomQuiz::getOp()
{
    return operate;
}

 checkAnswer::checkAnswer(int ans):generateRandomQuiz()
 {
  Ans=ans;                       
  string operate=getOp();
  int op1=getp1();
  int op2=getp2();
  if (operate=="+")
  {
        if (op1+op2==Ans)
        {
            cout<<"Your answer is correct."<<endl;
        }
        else
        {
            cout<<"You can do better next time."<<endl;
        }
  }
  if (operate=="-")
  {
        if (op1-op2==Ans)
        {
            cout<<"Your answer is correct."<<endl;
        }
        else
        {
            cout<<"You can do better next time."<<endl;
        }
  }if (operate=="*")
  {
        if (op1*op2==Ans)
        {
            cout<<"Your answer is correct."<<endl;
        }
        else
        {
            cout<<"You can do better next time."<<endl;
        }
  }if (operate=="/")
  {
        if (op1/op2==Ans)
        {
            cout<<"Your answer is correct."<<endl;
        }
        else
        {
            cout<<"You can do better next time."<<endl;
        }
  }                                                                
}
int main()
{
 cout<<"This quiz is about evaluating an expression which is being generated randomly" 
 <<endl;

    generateRandomQuiz Q();
    int answer;
    int op1=Q.getp1();
    int op2=Q.getp2();
    string opr=Q.getOp();
    cout<<"What is: "<<op1<<op2<<op2<<"=?"<<endl;
    cin>>answer;
    checkAnswer A(answer);

 system("PAUSE");
 return 0;
}

我正在编写一个随机生成测验的程序,并要求用户提供这样的答案答案: 什么是:15/43 =? 运算符和数字是随机生成的。但是当我运行程序时,我得到了错误

  

“请求成员getp1' in Q',generateRandomQuiz ()()' " "request for member generateRandomQuiz()()'”中的非类型Q', which is of non-class type getp2'   “请求成员getOp' in Q',这是非类型的`generateRandomQuiz()()'”

1 个答案:

答案 0 :(得分:3)

搜索“最烦恼的解析”,解决方法是:

generateRandomQuiz Q;

您的原始代码声明了一个名为Q的函数,返回generateRandomQuiz