简体   繁体   English

创建重载运算符时出错

[英]Error creating overloading operators

Attempting to create an overloaded operator for cout for a class (learning C++) and receiving the following errors: ..\\Vpet.h:17:14: error: 'ostream' in namespace 'std' does not name a type ..\\VPet.cpp:48:6: error: 'ostream' in namespace 'std' does not name a type 尝试为类的cout创建重载运算符(学习C ++)并收到以下错误: .. \\ Vpet.h:17:14:错误:名称空间“ std”中的“ ostream”未命名类型 .. \\ VPet.cpp:48:6:错误:名称空间'std'中的'ostream'没有命名类型

I have a feeling it's a syntax error, but i'm not sure. 我觉得这是语法错误,但我不确定。 It appears to be correct so it's plausible that it could be a compiler/IDE problem. 它似乎是正确的,因此可能是编译器/ IDE问题。 I'm using MinGW GCC compiler with Eclipse. 我在Eclipse中使用MinGW GCC编译器。 Code Below: 下面的代码:

Header File (IDE notifies of an error on the friend declaration 头文件 (IDE通知friend声明中的错误

* Vpet.h
 *
 *  Created on: May 18, 2016
 *      Author: TAmend
 */

#ifndef VPET_H_
#define VPET_H_


class VPet
{

    public:

    friend std::ostream& operator<<(std::ostream& os, const VPet& vp);

    // Constructors (Member Functions)
    VPet(int weight, bool hungry);
    //Default value in case the user creates a virtual pet without supplying parameters
    VPet();

    // Member functions
    void feedPet(int amountOfFood);
    bool getHungry();
    double getWeight();

    private:

    // Data Members
    double weight;
    bool hungry;

};


#endif /* VPET_H_ */

Class Source File (Error notification from IDE on std::ostream& operator<<(std::ostream& os, const VPet& vp) line 类源文件 (来自IDE的std::ostream& operator<<(std::ostream& os, const VPet& vp)错误通知std::ostream& operator<<(std::ostream& os, const VPet& vp)

#include "Vpet.h"
#include <cmath>


//Creation of our constructor (you can leave out the initializer list,
//but without it you're initializing to default and then overriding (operation twice))

VPet::VPet(int w, bool hun):weight(w),hungry(hun)
{



}

VPet::VPet():weight(100), hungry(true)
{

}

//Member Functions

void VPet::feedPet(int amt)
{

    if(amt >= (0.5 * weight))
    {
        hungry = false;
    }
    else
    {
        hungry = true;
    }

    weight = weight + (0.25 * amt);

}

double VPet::getWeight()
{
    return weight;
}

bool VPet::getHungry()
{
    return hungry;
}

std::ostream& operator<<(std::ostream& os, const VPet& vp)
{
    std::string hungerStatus = "";

    if(vp.hungry)
    {
        hungerStatus = "hungry";

    }
    else
    {
        hungerStatus = "not hungry";
    }

    return os << "weight: " << vp.weight << " hunger status: " << hungerStatus << std::endl;
}

You need to include header <iostream> in header Vpet.h 您需要在标题Vpet.h包含标题<iostream>

For example 例如

* Vpet.h
 *
 *  Created on: May 18, 2016
 *      Author: TAmend
 */

#ifndef VPET_H_
#define VPET_H_

#include <iostream>

//...

Also in the module that contains the definition of the operator you need to include header <string> . 同样,在包含运算符定义的模块中,您还需要包括标题<string>

Header <cmath> is redundant if you are not going to do some math with objects. 如果您不打算对对象做一些数学运算,则标题<cmath>是多余的。

Take into account that it is better to declare member functions that do not change the state of the object as constant. 考虑到最好将不将对象状态更改为常量的成员函数声明为常量。 For example 例如

bool getHungry() const;
double getWeight() const;

And the output operator can be declared without function specifier friend using the getters that are declared with the qualifier const as I showed. 就像我展示的那样,可以使用使用限定符const声明的getter来声明输出运算符,而无需使用函数说明符friend

For example 例如

std::ostream& operator<<(std::ostream& os, const VPet& vp)
{
    std::string hungerStatus;

    if(vp.getHungry())
    //    ^^^^^^^^^^^
    {
        hungerStatus += "hungry";

    }
    else
    {
        hungerStatus += "not hungry";
    }

    return os << "weight: " << vp.getWeight() << " hunger status: " << hungerStatus << std::endl;
    //                         ^^^^^^^^^^^^^
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM