当重载的操作符不是朋友时,它会返回垃圾

时间:2014-10-03 05:17:54

标签: c++

我的重载插入运算符在被声明为我的Coord类的朋友时工作正常。然而,当它不是朋友时,cout<< myCoord似乎回归了GARBAGE。考虑到我没有使用任何私人会员数据,我不明白为什么。

//in my coord.cpp
ostream& operator <<(ostream& outputStream, const Coord &coord_in) {

    outputStream << "( " << coord_in.getRoom() << ", " << 
    coord_in.getRow() << ", " << coord_in.getCol() << " )";   

    return outputStream;
}

//coord.h
#include <iostream>
using namespace std;

#ifndef COORD_H
#define COORD_H

class Coord {
private:
int room;
int row;
int col;
public:
Coord();
Coord(int room_in, int row_in, int col_in);
/* accessor functions */
int getRoom() const;
int getRow() const;
int getCol() const;

/* mutator functions */
void setRoom(Coord coord_in, int newRoom);
void setRow(Coord coord_in, int newRow);
void setCol(Coord coord_in, int newCol);

friend ostream& operator <<(ostream& outputStream, const Coord &coord_in);
};

#endif

0 个答案:

没有答案
相关问题