0

< < 연산자에 오버로드 문제가 있습니다. 모든 인쇄 및 잘 들어갑니다, 그러나 나는 시도하고 ostream에를 반환 할 때이 오류를 얻을 :오버로드 <<, ostream을 반환하면 오류가 발생합니다. C++

표현 : _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++에 대한 더 많은 경험을 할 수 있습니까?

답변

1

설명 된 코드에는 설명하는 문제가 발생하지 않습니다. "_BLOCK_TYPE_IS_VALID (pHead-> nBlockUse)"오류는 힙이 이전 시점에서 손상되었다는 표시입니다. 반환 문에서 감지되지만 연산자와 다른 코드는 아닙니다. <

+0

감사합니다. 답을 고맙게 생각합니다. 의미심장한데, 나는 하루 종일 다른 프로젝트에서 일해 왔으며 충격도 없었습니다. – zmarine

+0

보조 노트로, Car 클래스에 소멸자, 복사 생성자 및 operator = 연산자가 필요합니다. 그렇지 않으면 예를 들어, 메모리 누수 및/또는 재미있는 오류가 발생합니다. 값으로 Car 객체를 전달하거나보다 명확한 방식으로 Car 객체를 복사합니다. – Erik

0

힙을 묶었습니다. 현재 실행중인 코드와 관련이있을 수도 있고 없을 수도 있습니다. 당신이 원시 포인터를 사용하여 시작 하겠지만 우리에게 보여 주기로 결정한 것을 즉시 보지 마십시오.

관련 문제