VS 2010 c++ LNK2005使用#pragma一次和#ifndef时的错误
VS 2010 C++ LNK2005 errors while using #pragma once and #ifndef
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:DevPokerDebugPoker.exe : fatal error LNK1169: one or more multiply defined symbols found
我已经知道为什么这些错误发生的谷歌,但我不知道为什么他们仍然发生,当我尝试#pragma和#ifndef保护的东西。
这是我的名片。
#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
这可能很明显,但我就是想不明白…
,这里是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;
}
,下面是我用来测试它们的代码:
#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;
}
对象card
和deck
在头中定义。当您将头文件包含到翻译单元中时,它将创建该对象的另一个定义。您可能应该从类定义中删除card
和deck
。如果确实需要定义这些对象,可以使用
extern Card card;
它们是链接器错误,它们与c++代码本身无关。
问题出在Card和Deck定义的末尾。为什么有:
} card;
:
} deck;
?
1)如果多次定义"class Card",您将得到编译错误,而不是链接错误。
2)在大多数情况下,您的头文件看起来很好。事实上,您并不需要两者都需要:#pragma once或#ifndef中的一个或另一个应该足够了。
3)问题:从你的类定义中去掉最后的"card"answers"deck"!
相关文章:
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在C++中一次将矢量值写入多个文件
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么 zlib 放气初始化调用一次不起作用?
- 在一次迭代中从 txt 文件中读取多行
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 高级选择排序 - 在一次迭代中搜索两个元素
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何仅使用一次固定<<设置精度(2)?或者至少恢复到默认行为?
- 什么是仅调用一次并调用参数的控制台应用
- 如何确保用户在一行上提示输入一次时输入名字和姓氏?
- (C++)虽然循环一次不起作用,但我引入了多个变量
- 为什么我的信号处理程序只执行一次?
- 如何使用C++一次读取整个二进制文件
- 花括号使循环运行一次?
- 使用 #pragma 一次,#ifndef 在同一文件中包含保护
- VS 2010 c++ LNK2005使用#pragma一次和#ifndef时的错误