<<演算子のオーバーロードに問題があります。すべてが印刷されて正常に入力されますが、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;
}
戻る前に、すべてが画面に正常に印刷されます。そして、これら2つの関数は私には同じように見えますが、C ++の経験が豊富な人は、そうでないことを教えてくれますか?