C++ 创建重载运算符时出错
尝试为类的cout创建重载运算符(学习C++),并收到以下错误: ..\Vpet.h:17:14:错误:命名空间“std”中的“ostream”未命名类型 ..\VPet.cpp:48:6:错误:命名空间“std”中的“ostream”未命名类型 我觉得这是个语法错误,但我不确定。它似乎是正确的,因此可能是编译器/IDE问题。我正在将mingwgcc编译器与Eclipse一起使用。代码如下: 头文件(IDE通知C++ 创建重载运算符时出错,c++,eclipse,c++11,syntax,compiler-errors,C++,Eclipse,C++11,Syntax,Compiler Errors,尝试为类的cout创建重载运算符(学习C++),并收到以下错误: ..\Vpet.h:17:14:错误:命名空间“std”中的“ostream”未命名类型 ..\VPet.cpp:48:6:错误:命名空间“std”中的“ostream”未命名类型 我觉得这是个语法错误,但我不确定。它似乎是正确的,因此可能是编译器/IDE问题。我正在将mingwgcc编译器与Eclipse一起使用。代码如下: 头文件(IDE通知友元声明中的错误 * Vpet.h * * Created on: May 18
友元
声明中的错误
* 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_ */
*Vpet.h
*
*创建日期:2016年5月18日
*作者:塔蒙德
*/
#ifndef VPET_H_
#定义VPET_H_
类VPet
{
公众:
friend std::ostream&operator您需要在headerVpet.h
比如说
* Vpet.h
*
* Created on: May 18, 2016
* Author: TAmend
*/
#ifndef VPET_H_
#define VPET_H_
#include <iostream>
//...
bool getHungry() const;
double getWeight() const;
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;
// ^^^^^^^^^^^^^
}
输出操作符可以在不使用函数说明符friend
的情况下使用使用限定符const声明的getter来声明,如我所示
比如说
* Vpet.h
*
* Created on: May 18, 2016
* Author: TAmend
*/
#ifndef VPET_H_
#define VPET_H_
#include <iostream>
//...
bool getHungry() const;
double getWeight() const;
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;
// ^^^^^^^^^^^^^
}
std::ostream&operator您需要在headerVpet.h
比如说
* Vpet.h
*
* Created on: May 18, 2016
* Author: TAmend
*/
#ifndef VPET_H_
#define VPET_H_
#include <iostream>
//...
bool getHungry() const;
double getWeight() const;
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;
// ^^^^^^^^^^^^^
}
输出操作符可以在不使用函数说明符friend
的情况下使用使用限定符const声明的getter来声明,如我所示
比如说
* Vpet.h
*
* Created on: May 18, 2016
* Author: TAmend
*/
#ifndef VPET_H_
#define VPET_H_
#include <iostream>
//...
bool getHungry() const;
double getWeight() const;
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;
// ^^^^^^^^^^^^^
}
std::ostream&operator噢!这就解决了问题。谢谢Vlad。应该有点尴尬。只要允许,我会选择你的答案作为解决方案。@StormsEdge任何程序员都可能犯错误。:
应该足够了(至少在标题中)doh!这解决了问题。谢谢Vlad。有点尴尬,我应该知道。只要SO允许,我会选择你的答案作为解决方案。@StormsEdge任何程序员都可能犯错误。:
应该足够了(至少在标题中)