-2

I'm kinda of new at c++, so I need some help. I have to make a function tha books ticket for an event.

  void Event::book(int row, int seat, Date date, char *name, char *note)
{
    if(this->date==date && strcmp(this->name,name)==0)
    {
        if(hall.seatsInHall[row-1][seat-1].isFree()==true)
        {
            hall.seatsInHall[row-1][seat-1].isFree()=false; // here it gives me non-lvalue in assignment
            hall.seatsInHall[row-1][seat-1].getNote()=note; // here it gives me non-lvalue in assignment
            hall.seatsInHall[row-1][seat-1].getNote()=note; // here it gives me non-lvalue in assignment
            cout<<"Seat Booked.";
        }
        else cout<<"This seat is already taken or bought.";
    }
    else cout<<"Error.";
}

What can I do to not show this mistake?

  • i suspect you mean isFree = false, but since you dont show the definition its hard to say. or maybe isFree(false), you gotta show the def of isFree – pm100 Jun 12 '20 at 23:27
  • isFree() is a bool function in class Seat, which returns true if a seat is free – Priss Bliznakova Jun 12 '20 at 23:30
  • If you return the `bool` by value you get a new object that's a copy that's not around long enough to be worth changing the value. Since this is certainly a programming mistake, the compiler slaps the code down. See [What are rvalues, lvalues, xvalues, glvalues, and prvalues?](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) for a quick rundown of the terminology. – user4581301 Jun 12 '20 at 23:50
  • 1
    show the definiton of the class please – pm100 Jun 13 '20 at 00:05

1 Answers1

0

The problem arises because the stuff on the left side of the assignment statements are not lvalues i.e. you cannot assign values to them. The most likely cause is that getNote() and isFree() are returning-by-value. A function can return an lvalue if (and only if) it returns a reference.

You need to make sure that isFree() and getNote() return non-const references.

For the two functions having problems in your post, they could look like:

bool& isFree()

char*& getNote()

While this should fix your errors, in the case of getNote, it's not very pretty. Furthermore, it's not clear which object has ownership of the char* data and that could lead to memory leaks later. A nicer solution would be to change note to std::string instead of char*.

Your original function signature would then look like:

void Event::book(int row, int seat, Date date, char *name, std::string note)

And the signature of getNote() would be:

std::string& getNote()
  • While you could fix it by returning a reference to a pointer i.e. `char*& getNote()`, a nicer and more robust C++ option would be to use a std::string instead of char* for the note. You benefit from the features and relative security of std::string, and it makes the code more understandable. – SuperKingLol Jun 13 '20 at 13:04
  • Yes, it's much easier with string. Thank you! – Priss Bliznakova Jun 13 '20 at 19:33
  • Great! I'm glad you got it working. If this helped you, would you be able to accept my answer? – SuperKingLol Jun 13 '20 at 22:27