C ++错误“对Class :: Function()的未定义引用" [英] C++ error 'Undefined reference to Class::Function()'

查看:160
本文介绍了C ++错误“对Class :: Function()的未定义引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我解决这个问题-我只是C ++的新手,这给我带来了很多麻烦.

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.

我正在尝试制作相对简单的Deck和Card类对象.

I'm trying to make relatively simple Deck and Card class objects.

错误显示在"Deck.cpp"中,声明了纸牌数组,然后当我尝试用纸牌对象填充数组时.它说有对Card::Card()Card::Card(Card::Rank, Card::Suit)Card::~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().

我的所有看似正确的东西,所以我不知道出了什么问题.

I've got all my includes seemingly right, so I don't know what's going wrong.

代码如下:

deck.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

#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

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

#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;
}

推荐答案

您使用什么来编译它?如果存在未定义的引用错误,通常是因为.o文件(从.cpp文件创建)不存在,并且您的编译器/生成系统无法链接该文件.

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.

此外,在card.cpp中,该功能应为Card::Card()而不是void Card. Card::作用域;这意味着您的Card()函数是Card类的成员(显然是Card类的成员,因为它是该类的构造函数).没有这个,空卡就只是一个免费功能.同样,

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,

void Card(Card::Rank rank, Card::Suit suit)

应该是

Card::Card(Card::Rank rank, Card::Suit suit)

此外,即使您将其称为deck.h,在deck.cpp中也要说#include "Deck.h".包含的内容区分大小写.

Also, in deck.cpp, you are saying #include "Deck.h" even though you referred to it as deck.h. The includes are case sensitive.

这篇关于C ++错误“对Class :: Function()的未定义引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆