I get this error in the following code

//Error...
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

void Menu::readDates(istream& in, Date& dates)
{
	in >> dates.getDay() >> dates.getMonth() >> dates.getYear(); 
}

Thought '>>' can deal with integers... Surely there' no need to overload here... All three functions return integers, confused???

Recommended Answers

All 7 Replies

cin wants a reference to an integer, and getDay() just returns an int. If you change getDay() to return a reference then readDate() should work correctly. Example:

class Date
{
...
    int& getDay() {return m_day;}
};

cin wants a reference to an integer, and getDay() just returns an int. If you change getDay() to return a reference then readDate() should work correctly. Example:

class Date
{
...
    int& getDay() {return m_day;}
};

Emh... No Ancient, i've been stupid here cause i shouldn't even use the getters on istreams, rather ostreams... Thnx!

But wait, doesn't this work?

int x;
cin >> x;

x is an int, not a reference to one. Is it different when you're dealing with function return types and just the data type of a variable?

But wait, doesn't this work?

int x;
cin >> x;

x is an int, not a reference to one. Is it different when you're dealing with function return types and just the data type of a variable?

x is technically a placeholder for an int, therefore it can be treated as a reference variable.

Also the statements provided above were supplying input for a non-lvalue (a copy of an int, not a reference).

For example--

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;
using std::flush;

int x, y;

int &getRef(){
    return x;
}

int getInt(){
   return y;
}

int main(){

   x = 5;
   y = 2;

   cin >> getRef(); // doable because getRef is an lvalue

   cout << x << endl; // should print out the input

   cin >> getInt(); // illegal!, trying to assign a copy of an int which is NOT an lvalue

   return 0;
}

x is technically a placeholder for an int, therefore it can be treated as a reference variable.

Also the statements provided above were supplying input for a non-lvalue (a copy of an int, not a reference).

For example--

#include <iostream>
#include <cstdlib>

using std::cout;
using std::cin;
using std::endl;
using std::flush;

int x, y;

int &getRef(){
    return x;
}

int getInt(){
   return y;
}

int main(){

   x = 5;
   y = 2;

   cin >> getRef(); // doable because getRef is an lvalue

   cout << x << endl; // should print out the input

   cin >> getInt(); // illegal!, trying to assign a copy of an int which is NOT an lvalue

   return 0;
}

Oh right, I see. So this would work, correct?

cout << getInt();

Oh right, I see. So this would work, correct?

cout << getInt();

I don't see why that would be a problem, then again I don't know all of the arguments passed to cout by heart.

What I do know is that a string is just a const char * and that cout can accept an argument like that. I'm fairly sure cout can also take constant values such as ints, etc, without a necessary ostream flush/endl object.

Again I could be wrong, I'm not in a room with a compiler nor can I view the header files since I don't have access to them. I'd suggest playing around with cout and determining what it can or can't take.

However, it would not make sense for cout to not be capable of taking something like an int, even if a stream object isn't being returned. If possible check the header files to see what is overloaded in the << and >> operators of cout/cin.

This works:

#include <iostream>
using namespace std;

int x;

int& GetRef()
{
     return x;
}

int GetInt()
{
    return x;
}

int main()
{
    x = 5;
    
    cout << GetInt() << endl << GetRef() << endl;
    system("PAUSE");
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.