2

I am trying to make a program with a class called "Deck". However, I am unable to define the functions outside of the header file.

The header Deck.h file has the following:

#ifndef DECK_H
#define DECK_H

#include <vector>
//#include "Card.h"

class Deck{
public:
   vector<Card>* deck = new vector<Card>;
   bool visibility; //Whether you are allowed to view the deck

   Deck();
   Deck(bool visibility);

   void shuffle();



};



#endif

And the Deck.cpp file has the following code:

#include "Deck.h"
#include <vector>
#include "stdafx.h"

Deck::Deck(){
   this.visibilty = false;
}

Deck::Deck(bool visibility){
   this.visibility = visibility;
}

void Deck::shuffle(){
   vector<Card>* shuffleHolder = new vector<Card>;

   shuffleHolder = this.deck;
}

For some reason, I always receive the errors:

1>Compiling...
1>Deck.cpp
1>.\Deck.cpp(5) : error C2653: 'Deck' : is not a class or namespace name
1>.\Deck.cpp(5) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Deck.cpp(6) : error C2673: 'Deck' : global functions do not have 'this' pointers
1>.\Deck.cpp(6) : error C2228: left of '.visibilty' must have class/struct/union
1>.\Deck.cpp(7) : warning C4508: 'Deck' : function should return a value; 'void' return type assumed
1>.\Deck.cpp(9) : error C2653: 'Deck' : is not a class or namespace name
1>.\Deck.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Deck.cpp(10) : error C2673: 'Deck' : global functions do not have 'this' pointers
1>.\Deck.cpp(10) : error C2228: left of '.visibility' must have class/struct/union
1>.\Deck.cpp(11) : warning C4508: 'Deck' : function should return a value; 'void' return type assumed
1>.\Deck.cpp(13) : error C2653: 'Deck' : is not a class or namespace name
1>.\Deck.cpp(14) : error C2065: 'vector' : undeclared identifier
1>.\Deck.cpp(14) : error C2065: 'Card' : undeclared identifier
1>.\Deck.cpp(14) : error C2065: 'shuffleHolder' : undeclared identifier
1>.\Deck.cpp(14) : error C2061: syntax error : identifier 'vector'
1>.\Deck.cpp(16) : error C2673: 'shuffle' : global functions do not have 'this' pointers
1>.\Deck.cpp(16) : error C2228: left of '.deck' must have class/struct/union

I am trying to figure out what the problem is, otherwise I might have to define all the functions inside of the Deck.h file and get rid of the .cpp file altogether.

6
  • 6
    Visual Studio can be pretty picky about the precompiled header, often if it doesn't find it first, it will stop processing include statements. Try moving #include "stdafx.h" to be the first include in Deck.cpp
    – lcs
    Jan 2, 2018 at 15:27
  • 1
    Visual C++ will not include header files included above #include "stdafx". Jan 2, 2018 at 15:29
  • 1
    Do not use new for the vector Jan 2, 2018 at 15:31
  • vector<Card>* shuffleHolder = new vector<Card>; shuffleHolder = this.deck;and gone is the new vector.
    – user2672107
    Jan 2, 2018 at 15:41
  • 1
    Be careful, as it is now, the shuffle method has a memory leak: you dynamically allocate a new vector, and you immediately lose the only existing pointer to this object, thus creating a non-reachable memory zone.
    – SirDarius
    Jan 2, 2018 at 15:41

1 Answer 1

0

When using precompiled headers #include "stdafx.h" must appear before every #include in every file. Otherwise the preprocessor/compiler will ignore the other files. You can read more about how to use precompiled headers here. The other option is just not use them if you don't need the improved compilation time.

Some unrelated notes about your code: this is pointer to the current object, so you cannot access properties from it with the dot . operator(which is used for objects with class type, not pointer). You have to use ->. For example this->visibility or if it is unambiguous just visibility. Also since you have dynamic memory allocation with new you have to have a destructor that calls delete. But in this particular case new is not needed and you can just let the vector manage the memory itself by making it a vector variable and not a pointer and not defining it with new.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.