c++ - overloading operator<< not working -
im having trouble outputting data members of card object using overloaded operator<<
i'm getting error says
"js::operator<<(std::__1::basic_ostream >&, js::card const&)", referenced from: linker command failed exit code 1 (use -v see invocation)
//
//main.cpp
#include "card.h" #include "deck.h" #include <iostream> using std::cout; using namespace jasonsteindorf; int main(int argc, const char * argv[]) { //deck d; //d.shuffle(); card c(5,3); //5 of clubs card c2; //ace of diamonds cout << c; //not working ???????? should says 5 of clubs c.displaycard(); //works return 0; }
//
//deck.h
#ifndef js_deck_h #define js_deck_h #include <iostream> using std::cout; #include <vector> using std::vector; namespace js { //forward declaration class card; class deck { public: deck(); void shuffle(); private: vector<card>cards; }; } #endif
//
//deck.cpp
#include "deck.h" #include "card.h" #include <algorithm> using std::random_shuffle; #include <iostream> using std::cout; using namespace js; deck::deck() { cout << "a deck of cards!!!\n\n"; for(int suit = 0; suit < 4; suit++) { for(int rank = 1; rank < 14; rank++) { cards.push_back(card(rank, suit)); } cout << '\n'; } } void deck::shuffle() { random_shuffle(cards.begin(), cards.end()); //for(int s = 0; s < 52; s++) // cards.at(s)->displaycard(); for(vector<card>::iterator = cards.begin(); < cards.end(); it++) { card = *it; a.displaycard(); } }
//
//card.h
#ifndef js_card_h #define js_card_h #include <iostream> using std::ostream; #include <string> using std::string; #include <vector> using std::vector; namespace js { class card { public: enum suit { diamonds, hearts, spades, clubs }; enum rank { ace = 1, jack = 11, queen = 12, king = 13 }; card(); card(int rank, int suit); string getrank() const; string getsuit() const; int getrankvalue() const; int operator+(const card& rhs); void displaycard() const; private: int rank; int suit; }; //ostream &operator<<(ostream &out, const card &rhs); ostream& operator << (ostream& out, const card& c); } #endif
//
//card.cpp
#include "card.h" #include <iomanip> using std::setw; using std::right; #include <iostream> using std::cout; #include <sstream> using std::stringstream; using namespace js; card::card(): rank(1), suit(0){ displaycard(); } card::card(int rank, int suit) : rank(rank), suit(suit) { displaycard(); } string card::getsuit() const { switch (suit) { case spades: return "spades"; break; case hearts: return "hearts"; break; case diamonds: return "diamonds"; break; case clubs: return "clubs"; break; default: return ""; break; } } string card::getrank() const { switch (rank) { case ace: return "ace"; break; case jack: return "jack"; break; case queen: return "queen"; break; case king: return "king"; break; default: stringstream out; out << rank; return out.str(); break; } } int card::getrankvalue() const { switch (rank) { case ace: return 1; break; case jack: case queen: case king: return 10; break; default: return rank; break; } } int card::operator+(const card& rhs) { return (getrankvalue() + rhs.getrankvalue()); } ostream& operator <<(ostream& out, const card& c) { out << c.getrank() << " of " << c.getsuit(); return out; } /*ostream &operator<<(ostream &out, const card &rhs) { out << rhs.getrank() << " of " << rhs.getsuit(); return out; }*/ void card::displaycard() const { cout << right << setw(5) << getrank() << setw(3) << " of " << getsuit() << '\n'; }
card.h
declares operator in namespace:
namespace js { ostream& operator << (ostream& out, const card& c); }
but card.cpp
defines different operator in global namespace:
ostream& operator <<(ostream& out, const card& c) {...}
your choices are:
- put declaration in global namespace; or
- put definition in
js
namespace; or - qualify name
js::operator<<
in definition.
Comments
Post a Comment