Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ error 'Undefined reference to Class::Function()' [duplicate]

Tags:

c++

oop

undefined

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.

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

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.

The code is as follows:

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; } 
like image 256
Ben Harris Avatar asked Mar 29 '13 23:03

Ben Harris


People also ask

How do you fix undefined reference error in C?

Used the GCC compiler to compile the exp. c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call.

How do you fix a undefined reference to a class in C++?

So when we try to assign it a value in the main function, the linker doesn't find the symbol and may result in an “unresolved external symbol” or “undefined reference”. The way to fix this error is to explicitly scope the variable using '::' outside the main before using it.

How do you fix undefined symbol in C++?

You need to pass the values to the function Calculate. Variables x, y, z and function are not accessible outside the class and also u need a return type to the function so that you can get the output from the function Calculate.

What is undefined reference to WinMain?

The error means that the compiler try to find WinMain() but it didn't. Either you didn't declare int main() Or your project type is something other than Console. ( Normally Win32GUI)


1 Answers

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.

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)

should be

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.

like image 74
maditya Avatar answered Sep 23 '22 07:09

maditya