一切都是正确的,但无法运行:错误预期{或;

时间:2013-03-24 10:19:26

标签: c++ class

我无法找到我的错误在哪里可以帮助我?错误是预期的;或者}但我已经把它放在那里我无法运行它。你知道为什么吗?被驱逐的阶级被关闭;虽然程序仍无法编译。请指导我谢谢

  #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");

         }

3 个答案:

答案 0 :(得分:4)

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

如果你使用理智的缩进风格,那么更容易发现。

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

答案 1 :(得分:1)

您应该在课程声明后添加;。例如:

class Whatever {

};

答案 2 :(得分:0)

您的代码中出现的错误很少:

  • 您错过了}的结束括号(class Derivedclass:public myBase

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

  • int setmydrivedint 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");

}