Pergunta

1>Deck.obj : error LNK2005: "class Card card" (?card@@3VCard@@A) already defined in Card.obj
1>PokerTester.obj : error LNK2005: "class Card card" (?card@@3VCard@@A) already defined in Card.obj
1>PokerTester.obj : error LNK2005: "class Deck deck" (?deck@@3VDeck@@A) already defined in Deck.obj
1>C:\Dev\Poker\Debug\Poker.exe : fatal error LNK1169: one or more multiply defined symbols found

I've learned why these errors occur by googling, but I don't know why they're still happening when I've tried #pragma once and the #ifndef protection thing.

Here's my Card.h

#pragma once

#ifndef CARD_H
#define CARD_H

#include <iostream>
#include <string>
using namespace std;

class Card
{
public:
    Card(int cardSuit = 0, int cardValue = 2); //constructor will create a two of hearts by default
    ~Card(void);
    int getSuit(); //returns the suit of the Card
    int getValue(); //returns the value of the Card
    int getColor(); //returns the color of the Card
    friend ostream& operator<< (ostream &out, Card &cCard);

private:
    int suit; //card suit
    int value; //card value
    int color; //card color
} card;

#endif 

and my Deck.h

#pragma once

#ifndef DECK_H
#define DECK_H

#include "Card.h"
#include <vector>
using namespace std;

class Deck
{
public:
     Deck(void);
    ~Deck(void);
    void newDeck(); //regenerates the full 52 card deck (e.g. cards are missing)
    void shuffle(); //shuffles the deck
    int cardsInDeck(); //returns the number of cards remaining in the deck
    Card takeTopCard(); //returns the top card and removes it from the deck
private:
    vector<Card> myDeck; //vector of 52 Card objects that make up the deck
} deck;

#endif

This is probably pretty obvious, but I just can't figure it out...

as requested, here's Card.cpp:

#include "Card.h"

Card::Card(int cardSuit, int cardValue)
{
card.suit = cardSuit;
card.value = cardValue;
if(cardSuit == 0 || cardSuit == 1) card.color = 0;
if(cardSuit == 2 || cardSuit == 3) card.color = 1;
}

//returns the card's color
int Card::getColor() 
{
return card.color;
}

//returns the card's suit
int Card::getSuit()
{
return card.suit;
}

//returns the card's value
int Card::getValue()
{
return card.value;
}

and here's the thing I wrote to test them:

#include "Deck.h"

int main() 
{
Deck testDeck = *new Deck();
Card testCardCreation = *new Card();
Card testCard = testDeck.takeTopCard();
testDeck.shuffle();
Card testShuf = testDeck.takeTopCard();
cout << testCard << endl << testShuf << endl;

return 0;
}
Foi útil?

Solução

The objects card and deck are defined in the header. When you include the header into a translation unit it will create another definition of this object. You should probably just remove card and deck from the class definition. If you actually need these objects to be defined, you'd use

extern Card card;

Outras dicas

They are linker errors, they're not related to C++ code per se.

The problem is at the end of the definitions of Card and Deck. Why do you have:

} card;

And:

} deck;

?

1) If you defined "class Card" multiple times, you'd get a compile error, not a link error.

2) For the most part, your header files look fine. In fact, you don't really need both: one or the other of #pragma once or #ifndef should suffice.

3) PROBLEM: leave off the final "card" and "deck" from your class definitions!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top