2012-12-16 47 views
1
奇怪的錯誤

可能重複:
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 =? 操作者和數字,當我運行該程序我得到的誤差是隨機generated.but

會員 getp1' in Q」,它是非類類型 generateRandomQuiz()()' " "request for member的的getP2' 在 Q', which is of non-class type generateRandomQuiz()

「請求() '的構件getOp' in Q 「 」 請求',其​​是非類類型的`generateRandomQuiz()()「」

回答

3

查找 」最棘手的解析「,修復是:

generateRandomQuiz Q; 

您的原始代碼聲明名爲Q的函數返回generateRandomQuiz

+0

非常感謝 – User14229754

+0

還有一個問題。程序總是給出答案是錯的 – User14229754