getting conversion error between int and char

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LemonHappy
    New Member
    • Nov 2016
    • 2

    getting conversion error between int and char

    #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;
    }
  • kiseitai2
    New Member
    • Jul 2007
    • 93

    #2
    Where is the error? Is it at compile time, linking time, or runtime? Please, post the error output!

    Comment

    • LemonHappy
      New Member
      • Nov 2016
      • 2

      #3
      cannot bind 'std::ostream {aka std::basic_ostr eam<char>}' lvalue to 'std::basic_ost ream<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 <<

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A Square is a class. When you code:

        Code:
        cout << Square3;
        you are really coding:

        Code:
        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:

        Code:
        cout << Square3.GetSide() << endl;
        where

        Code:
        double Square::GetSide()
        {
         return side;
        }
        That will return a double and the compiler knows how to cout a double.

        Comment

        Working...