在devc ++中编译代码时,为什么会出现此错误?

时间:2020-05-24 16:00:41

标签: c++

我想传递对象e的地址,但出现此错误:

D:\Timecal1\timecal.cpp In function 'int main()':
101 9   D:\Timecal1\timecal.cpp [Error] invalid initialization of non-const reference of type 'entry&' from an rvalue of type 'entry*'
97  7   D:\Timecal1\timecal.cpp [Note] in passing argument 1 of 'void menu(entry&)

出什么问题了?

这是我的代码:

#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>

//definitions
#define s ' '

using namespace std;

class date{
    private :
        int day;
        string month;
        int year;

        public:
            date()
            {
                day=0;
                month="NULL";
                year=0;
            }

            void vwrite(ostream &o)
            {
                o<<day<<'/'<<month<<'/'<<year;
            }
};

// overloading << operator for class date
ostream& operator<<(ostream &o,date &d)
{
    d.vwrite(o);
    return o;
}

class time{
    private:
        int hours;
        int mins;

    public:
        time()
        {
            hours=0;
            mins=0;
        }

        void vwrite(ostream &o)
        {
            o<<hours<<':'<<mins;
        }
};

// overloading operator << for time
ostream& operator<<(ostream &o, time &t)
{
    t.vwrite(o);
    return o;
}

class entry
{
    private:
        int sno;
        date d;
        time t;
        int mowd;

    public:
        entry() //constructor
        {
            sno=0;      
            mowd=0;
        }


        void vwrite(ostream &o)
        {
            o<<sno<<". "<<d<<s<<t<<s<<mowd;
        }
};


// overloading operator << for entry
ostream& operator<<(ostream &o, entry &e)
{
    e.vwrite(o);
    return o;
}



int main()
{
    void menu(entry&);
    entry e;
    cout<<sizeof(entry)<<endl;
    cout<<sizeof(e)<<endl;
    menu(&e);
    return 0;
}


void menu(entry &e)
{
    cout<<"\nWhat would you like to do? : ";
    cout<<"\n1. Read entries\n";
    cout<<"\n2. Enter time entries\n";
    cout<<"\n3. Modify a time sheet\n";
    cout<<"\n4. Calculate total\n";
    cout<<"\n5. Delete a time sheet\n";
    cout<<"\n6. Exit\n";
    cout<<endl;
    int x=0;
    cin>>x;
    switch(x)
    {
        case 1 :
            system("cls");
            cout<<"\nRead entries\n";
            cout<<e;
            break;

        case 2 :
            system("cls");
            cout<<"\nEnter time entries\n";
            break;

        case 3 :
            system("cls");
            cout<<"\nModify a time sheet\n";
            break;

        case 4 :
            system("cls");
            cout<<"\nCalculate total\n";
            break;

        case 5 :
            system("cls");
            cout<<"\nDelete a time sheet\n";
            break;

        case 6 :
            system("cls");
            cout<<"\nExiting\n";
            break;

        default :
            system("cls");
            cout<<"\nInvalid input\n";
            break;      
    }
}

3 个答案:

答案 0 :(得分:2)

当您拨打这样的电话时:

menu(&e);

您正在将e地址传递到menu。看来您认为此调用传递了 reference ,但事实并非如此。

但是menu需要一个entry&,因此您需要这样称呼它:

menu(e);

答案 1 :(得分:1)

您的代码的另一个问题是您拥有:

using namespace std;

但是您的一个类的名称是time,可能与std::time冲突。

答案 2 :(得分:1)

1 -原型声明

void menu(entry&);

必须在main函数之外。

2 -menu有一个引用参数,这意味着无论您传递什么变量,它都会引用该变量,而不是像往常一样复制它,因此您只需传递变量。如果使用地址操作符&传递变量,就好像要将引用传递给变量的引用一样,因此必须使用变量本身:

menu(e);
如果您的参数是指针,则使用

menu(&e)

void menu(entry*);

表示您必须传递变量的地址。