0

Board.cpp:

#include "Board.h"

Tetris_::Board::Board(int b, int h) {
  width_ = b;
  height_ = h;
}

ostream& operator<<(ostream& out, const Tetris_::Board& brd) {
  out << "(" << brd.width_ << ")";
  return out;
}

Tetris_::Board::~Board() {}

Board.h:

#include <iostream>
#include <stdlib.h>

using std::ostream;

namespace Tetris_ {
  class Board {
    public:
      Board(int b, int h);
      friend ostream& operator<<(ostream& out, const Board& brd);
      ~Board();
      int width_;
    private:
      int height_;
  };
}

main.cpp

#include "Board.h"
#include <stdlib.h>

using Tetris_::Board;
using std::cout;
//using std::endl;

int main(int argc, char* argv[]) {
  Board* brd = new Board(20,20);
  cout << brd;
  return 0;
}

All I get after executing the file is the address of the brd object, and not its width! I think it doesn't "find" the "overloading" and doesn't execute it, but just uses the standart "cout <<" procedure! Where am I wrong?!

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Narue
0

You're printing a pointer, of course the result will be an address. Try dereferencing brd:

cout << *brd;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.