我遇到了重载<<的问题运营商。一切都打印并输入正常,但当我尝试返回ostream时,我收到此错误:
表达式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)
我已经超载了另一个<<这个项目中的操作员已经很好地返回了一个ostream。以下代码中未使用此运算符。这是代码:
#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 ++经验告诉我吗?
答案 0 :(得分:1)
显示的代码中没有任何内容会导致您描述的问题。 “_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse)”错误表示堆在较早的时候已损坏,它在您的return语句中被检测到,但与运算符中的代码无关&lt;&lt;
答案 1 :(得分:0)