User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,686 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,209 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 123 | Replies: 1
Reply
Join Date: Jul 2008
Posts: 1
Reputation: fittipaldi is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fittipaldi fittipaldi is offline Offline
Newbie Poster

Operator Overloading in Namespace, not working, printing address

  #1  
32 Days Ago
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?!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Operator Overloading in Namespace, not working, printing address

  #2  
32 Days Ago
You're printing a pointer, of course the result will be an address. Try dereferencing brd:
  1. cout << *brd;
Member of: Beautiful Code Club.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 2:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC