sg2018
sg2018

Reputation: 13

Problems returning an object in C++

I'm just starting out in C++ and I'm having problems with one part of my assignment:

class Something {
     public:
         Random& random(); // should access a data member of type Random
     private:
         Random test(int r, int c);
}

Random& Something::random() {
         return (Random&) test;
}

And now there's an error with "test" in the function definition of random(), because "the expression must be an lvalue" and I built the solution and the error message given says "'&' : illegal operation on bound member function expression "

I have to keep the function declaration the way it is, because it's listed that way in the specs.

How do I fix this?

Upvotes: 1

Views: 99

Answers (1)

R Sahu
R Sahu

Reputation: 206747

You said in a comment: "test" is supposed to be a member variable.

Then, you need to change your class to:

class Something {
     public:
         Random& random(); // should access a data member of type Random
     private:

         // Not this. This declares test to be member function.
         // Random test(int r, int c);

         // Use this. This declares test to be member variable.
         Random test;
}

Random& Something::random() {
         return test;
}

Upvotes: 3

Related Questions