【发布时间】:2011-02-14 23:38:16
【问题描述】:
我在重载
表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
我还已经在这个项目中重载了另一个
#include "header1.h"
#include <iostream>
using namespace std;
class Car
{
public:
friend class Extras;
friend int main();
friend ostream& operator<< (ostream& os, const Car& in);
Car();
Car(string in_name, int in_year, string in_color, float in_cost);
private:
string name, color;
int year, extr_num;
float cost;
Extras *extr;
};
int main()
{
Car c1;
cout << c1;
return 0;
}
//Default Constructor
Car::Car()
{
name = "TEMP";
color = "BLUE";
year = 0;
cost = 0;
extr = new Extras[3];
extr_num = 0;
}
//Constructor
Car::Car(string in_name, int in_year, string in_color, float in_cost)
{
name = in_name;
color = in_color;
year = in_year;
cost = in_cost;
extr = new Extras[3];
extr_num = 0;
}
//Overloaded << operator for Car class
//This function is the one that fails.
ostream& operator<< (ostream& os, const Car& in)
{
os.precision(2);
os << in.name << ", " << in.year << ", "
<< in.color << ", $"<< in.cost << ", ";
os << "extras include: ";
os << endl;
return os; //Line of code in question
}
其他标头中的这段代码运行良好:
ostream& operator<< (ostream& os, Extras const &in)
{
os << in.ex_list;
return os;
}
一切都在返回前打印到屏幕上。这两个函数在我看来是一样的,有更多 C++ 经验的人可以告诉我吗?
【问题讨论】:
标签: c++ operator-overloading bitwise-operators return-type ostream