重载运算符 <<

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
}

我试图重载 Shiftoperator 但我收到名为的错误: "error c2679 binary ' ' no operator found which takes a right-hand operand of type"。

错误发生在 main 函数的第一个移位运算符中。我想用重载运算符打印 Vehicle 及其派生的 类。

你能给我解释一下错误吗?我真的不知道如何纠正这个问题。

您的代码示例仅包含拼写错误(Vehicle <-> Fahrzeug、ostreamAusgabe <-> ostreamOutput、ostreamOutput() 中 Speed() 之后的分号)。重载 operator<< 应该可以正常工作。

尝试编译并启动这段代码:

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;
}

我修正了您源代码中的所有拼写错误(遗漏的分号),下面是一个完整的工作示例:

#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
}

也许您输出了一些未定义 operator<< 的其他变量。要调试这种情况,请将代码从例如这个:

    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;

对此:

    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;

这样您将获得导致问题的实际线路的错误消息。否则你只会得到第一行的错误信息,在这种情况下你使用的编译器显然不区分这些行。