0
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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
AlTrain
  • 15
  • 5

2 Answers2

1

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.

gluk47
  • 1,812
  • 20
  • 31
  • Thank you for your answer. I splitted the code like u said, unfortunaletely it does not fix the error. When i hold my mouse above the shift operator it shows me: Error no '<<' operator matches these operands. Operand types are: std::ostream << Vehicle . – AlTrain Oct 08 '15 at 15:37
  • @AlTrain but did you actually try compiling it? When you hold your mouse over a source line it's likely that internal IDE heuristics work, not a real compiler. They can easily get confused by C++. I checked this source with g++ 4.8 and clang 3.4, both are pretty old. As for the splitting the source, it should just make it easier to locate the real error line, but it cannot solve the problem in no way. – gluk47 Oct 08 '15 at 15:41
  • I posted the parts of my programm which are important for the question, so I compiled it in my program (with all the derived classes initialized variables etc., which I tested already, there was no error/mistake) and it still gives me the same error. I use Visual Studio 2012. By the way are the const declarations correct? – AlTrain Oct 08 '15 at 15:50
  • I think, you isolated the error reason not quite accurately. Try compiling this source exactly as it is as a single file in a new project. Does it work or does it produce an error? Which error precisely? Also may it be so that you define `operator<<` only in a cpp file and actually use this operator in another file where its declaration is not visible? Or do you try using this operator for an inherited class without redefining this operator for that inherited class? – gluk47 Oct 08 '15 at 15:56
  • Okay, I will try to describe it better. I put the ostreamOutput function and the operator<< function in the vehicle-class cpp. (this is the base class) I havent implemented anything in the derived classes (in regard to overloading this operator) . But I also havent used the overloaded operator with any only class than the vehicle class, until now. The vehicle class has been included to main.cpp, so its declaration should be visible. I will now try to compile your code in a seperate project. – AlTrain Oct 08 '15 at 16:06
  • But do you put the `operator<<` prototype to the Vehicle.h that is included into main.cpp? It looks like in the file 'main.cpp' this operator is not visible. – gluk47 Oct 08 '15 at 16:07
  • I think thats the mistake. I didnt. Give me a second please i will try it again now. I tried to declare this as public method: std::ostream& operator<<(std::ostream&, const Vehicle&); The compiler says that there are too many parameters, can you tell me why this is wrong? – AlTrain Oct 08 '15 at 16:12
  • Sure. Just make the prototype out of the operator declaration: replace the function body and its curly braces with a semicolon. The result is this one: `std::ostream& operator<<(std::ostream& os, const Vehicle& x);`. Put this line after a class declaration in its header, but not inside a class. You could put this operator inside a class only if its first argument was of type `Vehicle*` which then would be passed implicitly as `this`, like that: `std::ostream& operator>>(std::ostream& os) {ostreamOutput(os);}`. But that's not what is needed in your case. – gluk47 Oct 08 '15 at 16:18
  • Also, here is a short reference on whether you should put your overloaded operator inside a class: http://stackoverflow.com/questions/4421706/operator-overloading/4421729#4421729 – gluk47 Oct 08 '15 at 16:20
  • 1
    Ok, thank you very much for your help, it finally works :) The link is also very helpful. – AlTrain Oct 08 '15 at 16:27
0

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;
}
Gandzy
  • 13
  • 4
  • Something has to be wrong with the types of the operator, I corrected my mistakes above, but the error still remains. Thank you for your answer. – AlTrain Oct 08 '15 at 14:16