C++ 错误“对 Class::Function() 的未定义引用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15712821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ error 'Undefined reference to Class::Function()'
提问by Ben Harris
I was wondering if anyone could help me out with this - I'm only new to C++ and it's causing me a fair amount of troubles.
我想知道是否有人可以帮助我解决这个问题 - 我只是 C++ 新手,这给我带来了很多麻烦。
I'm trying to make relatively simple Deck and Card class objects.
我正在尝试制作相对简单的 Deck 和 Card 类对象。
The error is showing up in "Deck.cpp", declaration of an array of cards, and then when i try to fill the array with card objects. It says there's an undefined reference to Card::Card()
, Card::Card(Card::Rank, Card::Suit)
and Card::~Card()
.
错误出现在“Deck.cpp”中,卡片数组的声明,然后当我尝试用卡片对象填充数组时。它说有一个未定义的对Card::Card()
,Card::Card(Card::Rank, Card::Suit)
和 的引用Card::~Card()
。
I've got all my includes seemingly right, so I don't know what's going wrong.
我的所有包含看起来都是正确的,所以我不知道出了什么问题。
The code is as follows:
代码如下:
deck.h
甲板.h
#ifndef DECK_H
#define DECK_H
#include "card.h"
class Deck
{
public:
Deck();
~Deck();
Card DealNextCard();
void Shuffle();
void DisplayDeck();
protected:
private:
};
#endif // DECK_H
deck.cpp
甲板.cpp
#include "Deck.h"
#include "card.h"
using namespace std;
const int NUM_TOTAL_CARDS = 52;
const int NUM_SUITS = 4;
const int NUM_RANKS = 13;
Card* cardArray;
void Deck() {
cardArray = new Card[NUM_TOTAL_CARDS];
int cardCount = 0;
for (int i = 0; i > NUM_SUITS; i++) {
for (int j = 0; j > NUM_RANKS; j++) {
cardArray[cardCount] = Card(Card::Rank(i), Card::Suit(j) );
cardCount++;
}
}
}
Card DealNextCard();
void Shuffle();
void DisplayDeck();
card.h
卡.h
class Card
{
public:
enum Suit {D=0, H, C, S};
enum Rank {ONE=0, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, J, Q, K, A};
Card(Card::Rank, Card::Suit);
Card();
virtual ~Card();
Card::Suit suit;
Card::Rank rank;
Card::Rank GetRank();
Card::Suit GetSuit();
std::string CardName();
protected:
private:
};
#endif // CARD_H
card.cpp
卡.cpp
#include "card.h"
using namespace std;
Card::Suit cardSuit;
Card::Rank cardRank;
void Card() {
//nothing
}
void Card(Card::Rank rank, Card::Suit suit) {
cardRank = rank;
cardSuit = suit;
}
Card::Rank GetRank() {
return cardRank;
}
Card::Suit GetSuit() {
return cardSuit;
}
std::string CardName() {
string test;
test = "testing string";
return test;
}
采纳答案by maditya
What are you using to compile this? If there's an undefined reference error, usually it's because the .o file (which gets created from the .cpp file) doesn't exist and your compiler/build system is not able to link it.
你用什么来编译这个?如果存在未定义的引用错误,通常是因为 .o 文件(从 .cpp 文件创建)不存在并且您的编译器/构建系统无法链接它。
Also, in your card.cpp, the function should be Card::Card()
instead of void Card
. The Card::
is scoping; it means that your Card()
function is a member of the Card class (which it obviously is, since it's the constructor for that class). Without this, void Card is just a free function. Similarly,
此外,在你的 card.cpp 中,函数应该是Card::Card()
而不是void Card
. 的Card::
是范围界定; 这意味着您的Card()
函数是 Card 类的成员(显然是,因为它是该类的构造函数)。没有这个,void Card 只是一个免费的功能。相似地,
void Card(Card::Rank rank, Card::Suit suit)
void Card(Card::Rank rank, Card::Suit suit)
should be
应该
Card::Card(Card::Rank rank, Card::Suit suit)
Card::Card(Card::Rank rank, Card::Suit suit)
Also, in deck.cpp, you are saying #include "Deck.h"
even though you referred to it as deck.h. The includes are case sensitive.
此外,在deck.cpp 中,#include "Deck.h"
即使您将其称为deck.h ,您也是在说。包含区分大小写。
回答by Andy Prowl
In the definition of your Card
class, a declarationfor a default construction appears:
在您的Card
类的定义中,会出现一个默认构造的声明:
class Card
{
// ...
Card(); // <== Declaration of default constructor!
// ...
};
But no corresponding definitionis given. In fact, this function definition (from card.cpp
):
但没有给出相应的定义。事实上,这个函数定义(来自card.cpp
):
void Card() {
//nothing
}
Does notdefine a constructor, but rather a global function called Card
that returns void
. You probably meant to write this instead:
难道没有定义构造函数,而是一个全局函数叫做Card
回返void
。你可能打算写这个:
Card::Card() {
//nothing
}
Unless you do that, since the default constructor is declared but not defined, the linker will produce error about undefined references when a call to the default constructor is found.
除非你这样做,因为默认构造函数已声明但未定义,当找到对默认构造函数的调用时,链接器将产生有关未定义引用的错误。
The same applies to your constructor accepting two arguments. This:
这同样适用于接受两个参数的构造函数。这个:
void Card(Card::Rank rank, Card::Suit suit) {
cardRank = rank;
cardSuit = suit;
}
Should be rewritten into this:
应该改写成这样:
Card::Card(Card::Rank rank, Card::Suit suit) {
cardRank = rank;
cardSuit = suit;
}
And the same also applies for other member functions: it seems you did not add the Card::
qualifier before the member function names in their definitions. Without it, those functions are global functions rather than definitions of member functions.
这同样适用于其他成员函数:您似乎没有Card::
在定义中的成员函数名称之前添加限定符。没有它,这些函数就是全局函数而不是成员函数的定义。
Your destructor, on the other hand, is declared but never defined. Just provide a definition for it in card.cpp
:
另一方面,您的析构函数已声明但从未定义。只需为它提供一个定义card.cpp
:
Card::~Card() { }
回答by taocp
This part has problems:
这部分有问题:
Card* cardArray;
void Deck() {
cardArray = new Card[NUM_TOTAL_CARDS];
int cardCount = 0;
for (int i = 0; i > NUM_SUITS; i++) { //Error
for (int j = 0; j > NUM_RANKS; j++) { //Error
cardArray[cardCount] = Card(Card::Rank(i), Card::Suit(j) );
cardCount++;
}
}
}
cardArray
is a dynamic array, but not a member ofCard
class. It is strange if you would like to initialize a dynamic array which is not member of the classvoid Deck()
is not constructor of class Deck since you missed the scope resolution operator. You may be confused with defining the constructor and the function with nameDeck
and return typevoid
.- in your loops, you should use
<
not>
otherwise, loop will never be executed.
cardArray
是一个动态数组,但不是Card
类的成员。如果你想初始化一个不是类成员的动态数组,这很奇怪void Deck()
不是类 Deck 的构造函数,因为您错过了范围解析运算符。您可能会对使用 nameDeck
和 return type定义构造函数和函数感到困惑void
。- 在你的循环中,你应该使用
<
not>
否则循环将永远不会被执行。
回答by suspectus
Specify the Class Card for the constructor-:
为构造函数指定类卡-:
void Card::Card(Card::Rank rank, Card::Suit suit) {
And also define the default constructor and destructor.
并且还定义了默认的构造函数和析构函数。