AlTrain
AlTrain

Reputation: 15

Overloading operator <<

class Vehicle
{
public:
//[...]
    virtual std::ostream& ostreamOutput(std::ostream&) const; // virtual in order to use it for subclasse like cars, busses etc. 

    virtual double Speed() const; //returns the speed of a vehicle, is implemented in derived classes

private: 
    int Number
    std::string Name
//[...]

protected:
    int MaxSpeed; //these variables were also needed in the derived classes
};

std::ostream& Vehicle::ostreamOutput(std::ostream& os) const
{
    os << std::resetiosflags(std::ios::right) << std::setiosflags(std::ios::left) <<       std::setfill(' ') << ""
        << std::setw(4) << Number
        << std::setw(9) << Name
        << std::setw(15) << Speed()
        << std::setw(5) << MaxSpeed

    return os;
}

std::ostream& operator<<(std::ostream& os, const Vehicle& x)
{
    x.ostreamOutput(os);
    return os; 
}

main() //I wanted to overload the "<<"-Operator in order to print the vehicle information without //a seperate function
{
    Vehicle Vehicle1("Vehicle1", 80); 

    std::cout << Vehicle1 << std::endl;//the first shift-operator contains the error
}

I tried to overload the Shiftoperator but I get the error named: "error c2679 binary ' ' no operator found which takes a right-hand operand of type".

The error occured in the first Shift Operator in the main function. I want to print Vehicle and its derived classes with the overloaded operator.

Can you explain the error to me? I really do not know how to correct this.

Upvotes: 0

Views: 478

Answers (2)

gluk47
gluk47

Reputation: 1820

I fixed all the typos (missed semicolons) in your source, and here is a complete working example:

#include <iostream>
#include <iomanip>

using namespace std;

class Vehicle
{
public:
//[...]
    Vehicle (const char* Name, int Number)
        : Name (Name), Number (Number)
    {}
    virtual std::ostream& ostreamOutput(std::ostream&) const; // virtual in order to use it for subclasse like cars, busses etc. 

    virtual double Speed() const {return 0.;} //returns the speed of a vehicle, is implemented in derived classes

private: 
    // remove in-class initializers below if you need to avoid C++11
    int Number = -1;
    std::string Name = "not set";
//[...]

protected:
    int MaxSpeed = 200; //these variables were also needed in the derived classes
};

std::ostream& Vehicle::ostreamOutput(std::ostream& os) const
{
    os << std::resetiosflags(std::ios::right) << std::setiosflags(std::ios::left) <<       std::setfill(' ') << ""
        << std::setw(4) << Number
        << std::setw(9) << Name
        << std::setw(15) << Speed()
        << std::setw(5) << MaxSpeed;

    return os;
}

std::ostream& operator<<(std::ostream& os, const Vehicle& x)
{
    x.ostreamOutput(os);
    return os; 
}

int main() //I wanted to overload the "<<"-Operator in order to print the vehicle information without //a seperate function
{
    Vehicle Vehicle1("Vehicle1", 80); 

    std::cout << Vehicle1 << std::endl;//the first shift-operator contains the error
}

Maybe you output some other variables for which operator<< is not defined. To debug this case, split the code from e.g. this:

    os << std::resetiosflags(std::ios::right) << std::setiosflags(std::ios::left) <<       std::setfill(' ') << ""
        << std::setw(4) << Number
        << std::setw(9) << Name
        << std::setw(15) << Speed()
        << std::setw(5) << MaxSpeed;

to this:

    os << std::resetiosflags(std::ios::right) << std::setiosflags(std::ios::left) <<       std::setfill(' ') << ""
        << std::setw(4) << Number;
    os  << std::setw(9) << Name;
    os  << std::setw(15) << Speed();
    os  << std::setw(5) << MaxSpeed;

This way you'll get the error message for the real line that is causing trouble. Otherwise you'll get the error message only for the first line, the compiler you use apparently does not distinguish the lines in this case.

Upvotes: 1

Gandzy
Gandzy

Reputation: 13

Your code example contains only typos (Vehicle <-> Fahrzeug, ostreamAusgabe <-> ostreamOutput, semicolon after Speed() in ostreamOutput()). Overloaded operator<< should work fine.

Try to compile and launch this code:

class Vehicle
{
public:
    Vehicle(const std::string& name, int num)
        : Name(Name)
        , Number(num)
        , MaxSpeed(100)
    {}

    virtual std::ostream& ostreamOutput(std::ostream&) const;
    virtual double Speed() const;

private: 
    int Number;
    std::string Name;

protected:
    int MaxSpeed;
};

double Vehicle::Speed() const
{
    return 0.0;
}

std::ostream& Vehicle::ostreamOutput(std::ostream& os) const
{
    os << std::resetiosflags(std::ios::right) <<  std::setiosflags(std::ios::left) <<       std::setfill(' ') << ""
        << std::setw(4) << Number
        << std::setw(9) << Name
        << std::setw(15) << Speed()
        << std::setw(5) << MaxSpeed;

    return os;
}

std::ostream& operator<<(std::ostream& os, const Vehicle& x)
{
    x.ostreamOutput(os);
    return os; 
}

int main()
{
    Vehicle Vehicle1("Vehicle1", 80); 

    std::cout << Vehicle1 << std::endl;
    return 0;
}

Upvotes: 0

Related Questions