Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 11-25-2003, 04:30 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
overloading the << operator....

please see:

http://www.programmersheaven.com/c/M...ing=A9999F0001

TIA
GSXdan is offline   Reply With Quote
Old 11-26-2003, 08:08 AM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Okay, here we go...

First, the brackets in Deck::dealcard are all funky again. I have know idea what you are trying to do, so I probably didn't put the brackets in the right place, but I do know that I needed to put one more } bracket in there to get it to compile.

Second, you have to change the -> in the operator>> overload to a period.

You also have to change the declaration of the getMessage method by adding const at the end. Otherwise, the operator>> overload won't work, beause you say that the Card &X is const, and therefore all the member functions used in that method must be const.

You shouldn't need that "friend ostream..." anywhere, so I took it out.

The following code will compile, but you have to figure out if it works:

card.h
Code:
#include <iostream>
#include <string>   
#ifndef CARD_H
#define CARD_H
using namespace std;

class Card {
public:
    Card(int, int, string);
    int getType() {return Ctype;} // < 0 pay, > 0 collect, = 0 GOOJF
    int getFlag() {return flag;}
    void setFlag(int x) {flag = x;}
    string getMessage() const {return message;}


private:

    int Ctype;
    int amount;
    string message;
    static int flag;
};

class Deck {

public:
    Deck();
    Card* dealcard();
    void discard(Card*);

private:
    Card* data[10];
    int top, bottom;
};
#endif
card.cpp
Code:
#include <stdlib.h>
#include "card.h"
#include <iostream>

using namespace std;

Card::Card(int type, int money, string M) {
    Ctype = type;
    amount = money;
    message = M;
}
// static int intializer

int Card::flag = 1;

Deck::Deck() {
    top = 9;
    bottom = 8;
    data[0] = new Card(0, 0, "Get out of BG Police Free");
    data[1] = new Card(-1, 100, "Pay Health Center $100");
    data[2] = new Card(-1, 50, "Pay Parking Fees $50");
    data[3] = new Card(1, 200, "Collect $200 for winning BG lottery");
    data[4] = new Card(-1, 550, "Pay Tuition $550");
    data[5] = new Card(1, 600, "Collect $600 for scholarship");
    data[6] = new Card(-1, 250, "Pay $250 for text books");
    data[7] = new Card(-1, 640, "Pay Residence Hall fee $640");
    data[8] = new Card(1, 350, "Collect job money $350");
    data[9] = new Card(-1, 250, "Pay $250 for car maintenance");
}

Card* Deck::dealcard() {
    Card* tmp;
    if(data[top]->getType() == 0) {
        if(data[top]->getFlag() == 0) {
            if(top == 9) {
                top = 0;
                bottom++;
            }
            else {
                top++;
                if(bottom == 9) {
                    bottom = 0;
                }
                else {
                    bottom++;
                }
            }
            tmp = data[top];
            data[top]->setFlag(0);
        }
        else {
            tmp = data[top];
        }
        if(top == 9) {
            top = 0;
            bottom++;
        }
        else {
            top++;
            if(bottom == 9) {
                bottom = 0;
            }
            else {
                bottom++;
            }
        }
    return tmp;
    }
}


void Deck::discard(Card* card) {
    if(card->getType() == 0) {
        data[0]->setFlag(1);
    }
}





ostream& operator<<(ostream& out, const Card& X)  {
    out << X.getMessage() << endl;
    return out;
}
doctorgonzo is offline   Reply With Quote
Old 11-30-2003, 09:54 PM   #3
Member (7 bit)
 
Join Date: Nov 2003
Location: LA
Posts: 101
Send a message via AIM to HNPFL
Boy do I hate those details.
HNPFL is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:42 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2