Thread: card game

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    9

    card game

    im writing generic code for card games and i can seem to fix this one error when i compile PileOfCards.cpp it says there is no 'operator=' what am i doing wrong
    PileOfCards.h:
    Code:
    #ifndef PILEOFCARDS
    #define PILEOFCARDS
    
    
    #include <iostream>
    #include "Card.h"
    using namespace std;
    
    class PileOfCards
    {
      public:
    	PileOfCards();
    	void MakeDeck();
    	void Shuffle();
    	Card TakeTopCard();
    	void AddCardToPile(Card c);
    	const PileOfCards & operator = (const PileOfCards & 
    
    original);
      private:
    	Card * Pile;
    	int size;
    	int back;
    };
    
    #endif
    PileOfCards.cpp:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include "PileOfCards.h"
    using namespace std;
    	
      PileOfCards::PileOfCards() {
    	size = 52;
    	Pile = new Card[size];
    	back = 0;
    	}
    
      void PileOfCards::MakeDeck() {
    	size = 52;
    	Pile = new Card[52];
    	int back = 0;
    	for(suit i = HEART; i <= SPADE; i + 1){
    		for(value j = ACE; j <= KING; j + 1)
    		Pile[back] = new Card(i, j, DOWN);
    		back+1;
    		}			
    	}
    
      void PileOfCards::Shuffle() {
    	for(int l = 0; l < 1000; l+1) {
    		Card c = TakeTopCard();
    		int x = rand();
    		for(int a = back; a > x; a-1) {
    			Pile[a] = Pile[a-1];
    			}
    		Pile[x] = c;
    		}
    	}
    
      Card PileOfCards::TakeTopCard() {
    	Card d = Pile[0];
    	for(int g = 0; g < back; g+1) {
    		Pile[g] = Pile[g + 1];
    		}
    	back - 1;
    	return d;
    	}
    
      void PileOfCards::AddCardToPile(Card c) {
    	for(int f = back; f > 0; f-1) {
    		Pile[f+1] = Pile[f];
    		}
    	Pile[0] = c;
    	}
    
      const PileOfCards & PileOfCards::operator = (const PileOfCards 
    
    & original) {
    	if(this != &original){
    		size = original.size;
    		delete Pile;
    		Pile = new Card[size];
    		for(int k = 0; k < size; k + 1)
    			Pile[k] = original.Pile[k];
    		}
    	}
    Card.h:
    Code:
    #ifndef CARD
    #define CARD
      
    #include <iostream>
    using namespace std;
    
    enum status {DOWN, UP};
      enum value {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
      enum suit {HEART, CLUB, DIAMOND, SPADE};
    
    class Card
    {  
      public:
    	Card();
    	Card(suit s, value v, status f);
    	Card(const Card & original);
    	void FaceUp();
    	void FaceDown();
    	status CardStatus() const;
    	value CardValue() const;
    	suit CardSuit() const;
    	const Card & operator = (const Card & original);
    
      private:
    	suit mySuit;
    	value myValue;
    	status myStatus;
    };
    
    #endif
    card.cpp:
    Code:
    #include <iostream>
    #include "Card.h"
    using namespace std;
    
      Card::Card()
      {}
    
      Card::Card(suit s, value v, status f){
    	mySuit = s;
    	myValue = v;
    	myStatus = f;
      }
    
      Card::Card(const Card & original){
    	mySuit = original.mySuit;
    	myValue = original.myValue;
    	myStatus = original.myStatus;
      }
    
      void Card::FaceUp(){
    	myStatus = UP;  
      }
    
      void Card::FaceDown(){
    	myStatus = DOWN;
      }
    
      status Card::CardStatus() const {
    	return myStatus;
      }
    
      value Card::CardValue() const {
    	return myValue;
      }
    
      suit Card::CardSuit() const {
    	return mySuit;
      }
    
      const Card & Card::operator = (const Card & original) {
    	if(this != &original) {
    		mySuit = original.mySuit;
    		myValue = original.myValue;
    		myStatus = original.myStatus;
    		}
    	return * this;
      }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Have you tried removing the space between operator and =?
    Dunno if it matters at all but I've never seen the operator keyword followed by a space before the operator symbol.

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    It looks like your problem is here.
    Code:
    const PileOfCards & operator = (const PileOfCards & 
    
    original);
    Sorry, I cannot help you because I haven't learned about that part of C++ yet.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I can't see the problem on initial inspection, but if you could post the entire error, including file and line number, that would be helpful. It usually tells you what requires an operator=.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Mark the line in your code (don't repost) where you get the error, and an exact error message would help. My guess is that the problem lies in your operator= overload prototype. Are you sure you want to return an address to a const? My guess it would be better to return a bool to show success or failure. The code inside the function should clear out *this and set it to original.

    Also, on another note:
    Code:
      PileOfCards::PileOfCards() {
    	size = 52;
    	Pile = new Card[size];
    	back = 0;
    	}
    
      void PileOfCards::MakeDeck() {
    	size = 52;
    	Pile = new Card[52];
    	int back = 0;
    	for(suit i = HEART; i <= SPADE; i + 1){
    		for(value j = ACE; j <= KING; j + 1)
    		Pile[back] = new Card(i, j, DOWN);
    		back+1;
    		}			
    	}
    Are you sure you need to new 52 cards twice? I'm pretty sure you shouldn't be doing that. You also shouldn't have new on that last line. Could get a bunch of memory leaks this way. You don't have a constructor, so you don't delete [] Pile. Also, for that last bold line, try changing to this:

    Code:
    for(suit i = HEART; i <= SPADE; i++){
    		for(value j = ACE; j <= KING; j++)
                    Card temp(i,j,DOWN);
    		Pile[back++] = temp;
    		}
    At least, I'm pretty it should be this way. In any way, please make a destructor.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 PM
  2. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  4. Video card mystery
    By Leeman_s in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-30-2003, 02:56 PM
  5. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM