3

I have recently started working with C++ classes and had just started when I reached an error. I have a "resource.h" file that contains the class definition of two classes: 'deck' and 'card'. I #included this file in another file, "card.cpp". In the card.cpp file I described all the methods/functions of the 'card' class. However on compilation I am getting the following the errors (fyi I am using the MinGW compiler for command-line):

card.cpp:3:29: error: ISO C++ forbids declaration of 'setCard' with no type [-fp ermissive] card.cpp:3:1: error: prototype for 'int Card::setCard(char, char)' does not matc h any in class 'Card' resource.h:9:8: error: candidate is: void Card::setCard(char, char)

The "card.cpp" file:

#include "resource.h"

Card::setCard(char f, char s) {
    face = f;
    suit = s;
}

Card::Card (char face, char suit) {
    setCard(face, suit);
}

Card::~Card () {}

The "resource.h" file:

typedef unsigned short int UINT;
class Card;
class Deck;

class Card {
    public:
        Card(char face, char suit);
        ~Card();        
        void setCard(char face, char suit);
        char getFace() const { return face; }
        char getSuit() const { return suit; }
    private:
        char face;
        char suit;
};

class Deck {
    public:
        Deck();
        ~Deck();
        Card getCard(UINT x);

    private:
        Card myCards[54];
};

What is causing this issue, and why in the world does the compiler think that "Card::setChard()" is an int

Nigh7Sh4de
  • 395
  • 2
  • 7
  • 20

1 Answers1

8
Card::setCard(char f, char s) {
    face = f;
    suit = s;
}

should be

void Card::setCard(char f, char s) {
    face = f;
    suit = s;
}

Some hints that helped me get to this amazing conclusion:

  • C++ forbids declaration of 'setCard' with no type
  • candidate is: void Card::setCard(char, char)

If you thought this was cryptic, hold on tight for when you get to templates. Compilers have a history of generating great error messages for them.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Great thanks it worked! But why do I need to add the "void" keyword? In the class definition I had already specified the "setCard" is going to return void? – Nigh7Sh4de Oct 29 '12 at 15:00
  • Here is a bonus question: I can return int, void, string, etc... But can I return self-defined class? I mean now that I have declared the class 'Card' could I write a function that returns Card? – Nigh7Sh4de Oct 29 '12 at 15:03
  • @Guitarroka yes :)) This isn't meant to be condescending - but you should read a good C++ book - http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Luchian Grigore Oct 29 '12 at 15:04
  • @Guitarroka see the link in my previous comment. – Luchian Grigore Oct 29 '12 at 15:07
  • In your class "declaration" (**NOT** definition) you declared that the class will have methods with given signatures. This declaration can be used by **ANY** (other) class to communicate with **your** class. So if you declare your class to have method `void setCard(char, char);` this is what you should define (and implement) in your class code. Your `.h` files is declaration [for everybody] and your `.cpp` files is the actual implementation/definition. – Germann Arlington Oct 29 '12 at 15:16
  • @GermannArlington what you call class declaration is actually a class definition. A class declaration is `class X;` (identical to forward declaration). A class definition is `class X{};`. – Luchian Grigore Oct 29 '12 at 15:31
  • @LuchianGrigore I always considered anything does not provide the code/behaviour definition/implementation to be declaration/description/explanation - I may have forgotten the correct wording though. From what I remember we used `.h` files to describe/declare (mainly) common information that is 'of interest' to other classes and then define/implement the actual behaviour in code in `.c` and `.cpp` files. I must admit that it has been some time since I actually used C++ though. – Germann Arlington Oct 29 '12 at 15:42