By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,254 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,254 IT Pros & Developers. It's quick & easy.

getting conversion error between int and char

P: 2
#include <iostream>
#include <string>
using namespace std;

class Square {

private:
double side;


public:

double getArea() {
return side * side;
}

void setSide(double s) {
side = s;
}




// Overload + operator to add two Square objects.
Square operator+(const Square& b) {
Square square;
square.side = side + b.side;
return square;
}

Square operator-(const Square& b) {
Square square;
square.side = side - b.side;
return square;
}


Square operator*(const Square& b) {
Square square;
square.side = side * b.side;
return square;

}

Square operator/(const Square& b) {
Square square;
square.side = side / b.side;
return square;
}
};

// Main function
int main() {
Square Square1;
Square Square2;
Square Square3;

double area = 0.0;


Square1.setSide(2.0);

Square2.setSide(5.0);


// Square1 area
area = Square1.getArea();
cout << "Area of Square1 : " << area << endl;

// Square2 area
area = Square2.getArea();

cout << "Area of Square2 : " << area << endl;

char op;

cout << "Enter the operator: ";
cin >> op;
switch (op)
{
case '+':
Square3 = Square1 + Square2;
cout << Square3;
break;
case '-':
Square3 = Square1 - Square2;
cout << Square3;
break;
case '*':
Square3 = Square1 * Square2;
cout << Square3;
break;
default:
cout << "No a valid operator";
}
return 0;
}
Nov 2 '16 #1
Share this Question
Share on Google+
3 Replies


kiseitai2
P: 93
Where is the error? Is it at compile time, linking time, or runtime? Please, post the error output!
Nov 2 '16 #2

P: 2
cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'

It's the error where "cout << Square3;" It's ether a definition or conversion error, because it tell me there's no operator for <<
Nov 2 '16 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
A Square is a class. When you code:

Expand|Select|Wrap|Line Numbers
  1. cout << Square3;
you are really coding:

Expand|Select|Wrap|Line Numbers
  1. ostream& operator<<(ostream& os, Square& arg);
and the compiler can't find this function. You will need to write this as a friend function to class Square so the function can access the private members of Square.

Or, you write member functions to Square to fetch the data:

Expand|Select|Wrap|Line Numbers
  1. cout << Square3.GetSide() << endl;
where

Expand|Select|Wrap|Line Numbers
  1. double Square::GetSide()
  2. {
  3.  return side;
  4. }
That will return a double and the compiler knows how to cout a double.
Nov 3 '16 #4

Post your reply

Sign in to post your reply or Sign up for a free account.