-4

我找不到我的错误在哪里,你能帮帮我吗?错误是预期的;或 } 但我已经把它放在那里我无法运行它。你知道为什么吗?驱动类由 ; 虽然程序仍然无法编译。请指导我谢谢

  #include <iostream>
    using namespace std;
    class myBase{
          private:
                  int foodmoney;
                  int hobbymoney;

                  public:
               void setdata();
          friend int caldata();
                 };
                 void myBase::setdata(){
                      cout<<"Enter foodmoney :" ;cin>>foodmoney;
                      cout<<"enter hoobymoney:";cin>>hobbymoney;
                      }
              int caldata(int sum,int foodmoney,int hobbymoney){
                        cin>>foodmoney;
                        cin>>hobbymoney;          
                        sum=(foodmoney+hobbymoney)/2;
                        return sum;

                             }


        class Derivedclass:public myBase{
                        int moneyforfood,moneyforhobby;
                        public:
               int setmydrived(int moneyforfood,int moneyforhobby){
                        cout<<"enter money for food(drived class)"<<endl;
                        cin>>moneyforfood;
                        cout<<"enter money for hobby(drived class)"<<endl;
                        cin>>moneyforhobby;
                        }

                        int calculatedrived(){

                   int sumd=(moneyforfood+moneyforhobby);
                     cout<<"avg money on frived class "<<sumd<<endl;
                     };


                    int main(){
                     myBase baseobj;
                     baseobj.setdata();
                     int caldata();
          Derivedclass obj;
                     int setmydrived();
                     obj.calculatedrived();
                      system("puase");

         }
4

3 回答 3

4

最后一个类的定义缺少右括号(和分号)。

如果您使用正常的缩进样式,则更容易发现这种情况。

您还缺少返回某些内容的函数中的两个返回语句。那是未定义的行为。

于 2013-03-24T10:21:50.620 回答
1

您应该;在类声明之后添加。例子:

class Whatever {

};
于 2013-03-24T10:22:19.017 回答
0

您的代码中几乎没有错误:

  • 您缺少一个右大括号 ( })class Derivedclass:public myBase

  • 系统中暂停的拼写不正确(“暂停”)

  • int setmydrivedand函数应该返回一个整数,int calculatedrived但你没有返回一个整数。

我已经纠正了错误。以下代码可以正常工作(参见IDEONE上的演示):

#include <iostream>
using namespace std;
class myBase
{
private:
    int foodmoney;
    int hobbymoney;

public:
    void setdata();
    friend int caldata();
};
void myBase::setdata()
{
    cout<<"Enter foodmoney :" ;cin>>foodmoney;
    cout<<"enter hoobymoney:";cin>>hobbymoney;
}
int caldata(int sum,int foodmoney,int hobbymoney)
{
    cin>>foodmoney;
    cin>>hobbymoney;          
    sum=(foodmoney+hobbymoney)/2;
    return sum;

}


class Derivedclass:public myBase
{
    int moneyforfood,moneyforhobby;
public:
    int setmydrived(int moneyforfood,int moneyforhobby)
    {
        cout<<"enter money for food(drived class)"<<endl;
        cin>>moneyforfood;
        cout<<"enter money for hobby(drived class)"<<endl;
        cin>>moneyforhobby;
                    /*THIS FUNCTION DOESNOT RETURN ANY INT VAL*/
    }

    int calculatedrived()
    {

        int sumd=(moneyforfood+moneyforhobby);
        cout<<"avg money on frived class "<<sumd<<endl;
        //return sumd;
                    /*THIS FUNCTION DOESNOT RETURN ANY INT VAL*/

    }
};

int main()
{
    myBase baseobj;
    baseobj.setdata();
    int caldata();
    Derivedclass obj;
    int setmydrived();
    obj.calculatedrived();
    system("pause");

}
于 2013-03-24T10:23:54.440 回答