0

I have three files - Main.cpp, Functions.cpp and Header.h. I have declared a class in Header.h.

#pragma once
class CStack
{
private:

    int* topCardType;
    int* topCardValue;
    int cardType[52];
    int cardValue[52];
    int size;

public:

    CStack* CreateDeck();
};

I have defined the function CreateDeck() in Functions.cpp

#include "Header.h"

CStack* CStack::CreateDeck() {}

However when I call it in Main.cpp I'm getting Error: identifier CreateDeck is undefined

#include "Header.h"

int main()
{
    CStack* deck = CreateDeck();    //creates deck of 52 cards
}

Please help. I'm new to C++. This used to work when I use structure instead of classes in C

110f716c
  • 100
  • 1
  • 2
  • 8
  • 1
    As you don't get an error from the `Functions.cpp` file (you have errors in it) then I can only guess that you're not actually building with that source file. How *do* you build your program? – Some programmer dude Aug 28 '21 at 16:34
  • Or maybe you mean that you get an error when building the `Main.cpp` file, not when linking? Then the problem is that there's no such global non-member function as `CreateDeck`. There's only the non-static member function `CStack::CreateDeck`, which you need an instance of `CStack` to be able to call it. It seems to me you have skipped some early chapters of your beginners book, or don't have a good one. [Here's a list of good C++ books](https://stackoverflow.com/a/388282/440558), please invest in one or two. – Some programmer dude Aug 28 '21 at 16:36
  • And for future questions about build errors, please include information about how you build your program. And add a complete and full copy-paste (as text) of the build output. Lastly please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 28 '21 at 16:38
  • I don't understand what you mean. I build from main.cpp i guess – Fayas Mohamed Aug 28 '21 at 16:42
  • You omitted the most important part: how are you compiling and linking your program? And in main, it should be called `CStack* deck = CStack::CreateDeck();` ... except since it isn't a static class function, you can't do it that way. This code has lots of bad code smells. – Eljay Aug 28 '21 at 16:46
  • If you are using VSCode and and wrongly tagged this for Visual Stuidio 2019, you need to edit your tasks.json file to have VSCode compile using all of the source files instead of just the active one. – drescherjm Aug 28 '21 at 16:47
  • @Someprogrammerdude yeah forgot i needed to create an object first. Thanks but please don't be rude. – Fayas Mohamed Aug 28 '21 at 16:49

1 Answers1

0

The CreateDeck() method must be called on a member of the CStack class

Something like :

CStack stack;
CStack* deck = stack.CreateDeck();

It seems that the conception is wrong and you want to use CreateDesk() as a constructor

#pragma once
class CStack
{

private:

    int* topCardType;
    int* topCardValue;
    int cardType[52];
    int cardValue[52];
    int size;

public:

    CStack(); // Default constructor
};
#include "Header.h"

CStack::CStack() {
// Initialise member's variables here
}
#include "Header.h"

int main()
{
    CStack deck; //creates deck with default values
}
Tom Rivoire
  • 627
  • 5
  • 6