Public Group

# Using Structs from header file in cpp file

This topic is 3804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys I was hoping that someone out there will help me with this teething problem that I have. The thing is, I have a defined a struct i.e. "deck" in my header file. I want to use this struct as a type in my cpp file but when I build I get an error saying that a "type has not been named" as shown in my compiler errors. I have tried all different manner of fixes but all to no avail. I'm hoping one of you will point me in the correct direction. Also I have declared a namespace i.e. "cards" in my header file but when I try and use it in my cpp file, I get a message saying that the "namespace cards is not a namespace". I am using mingw 3.4.5. Please Help. Sorry about the code, I'd use tags but I dont know how to turn it on The Header file #ifndef CARDS_H_ #define CARDS_H_ #include <iostream> #include <string> #include <vector> namespace cards{ typedef struct sample { int a; char b, c; } SAMPLE_STRUCT; ////// } class Cards { public: Cards(); virtual ~Cards(); struct deck{ // this holds the description of the card e.g. "King Of" string card_name; //card type e.g. "Hearts", Spades, etc string card_type; /* * the value of the card e.g, 2,3 6 etc Note that Jack = 10 * Queen = 11, King = 12, Ace = 13 */ int card_value; }; deck getCard(); }; #endif /*CARDS_H_*/ the cpp file #ifndef CARDS_H_ #define CARDS_H_ #include "Cards.h" #include <iostream> #include <vector> using namespace std; using namespace cards; class Cards{ public: Cards(); ~Cards(); deck getCard(); decks dec; }; Cards::Cards() { } Cards::~Cards() { } deck Cards::getCard(){ } #endif//CARDS_H_// Comiler Errors: **** Build of configuration Debug for project Poker **** **** Internal Builder is used for build **** g++ -O0 -g3 -Wall -c -fmessage-length=0 -oPokerGameEngine.o ..\PokerGameEngine.cpp ..\PokerGameEngine.cpp: In function int main(int, std::string**)': ..\PokerGameEngine.cpp:22: warning: unused variable 'pl' ..\PokerGameEngine.cpp:31:2: warning: no newline at end of file g++ -O0 -g3 -Wall -c -fmessage-length=0 -oCards.o ..\Cards.cpp ..\Cards.cpp:8: error: cards' is not a namespace-name ..\Cards.cpp:8: error: expected namespace-name before ';' token ..\Cards.cpp:19: error: deck' does not name a type ..\Cards.cpp:20: error: decks' does not name a type ..\Cards.cpp:36: error: deck' does not name a type Build error occurred, build is stopped Time consumed: 4125 ms.

##### Share on other sites
To get at the type Deck you need to qualify the namespace it is in, which in this case is the class Cards:

Cards::Deck

You also have a typo with 'decks' on the line below.

##### Share on other sites
Thanks for your prompt reply Dave. I have tried your suggestion i.e. I have included the namespace hence I now have

Cards::deck getCard();
Cards::deck dec;

but now, compiler is throwing up new errors i.e.

**** Build of configuration Debug for project Poker ****

**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oCards.o ..\Cards.cpp
..\Cards.cpp:8: error: cards' is not a namespace-name
..\Cards.cpp:8: error: expected namespace-name before ';' token
..\Cards.cpp:19: error: expected ;' before "getCard"
..\Cards.cpp:20: error: expected ;' before "dec"
..\Cards.cpp:36: error: expected constructor, destructor, or type conversion before "Cards"
Build error occurred, build is stopped
Time consumed: 1656 ms.

Is there anything that I am doing wrong? HELP!!

##### Share on other sites
Your cpp file is defining CARDS_H_ which means the conpent of the header (where the cards namespace is defined) is not being included.

You only need an include lock in include files, not in cpp files.

##### Share on other sites
I thought the idea behind

#ifndef CARDS_H_
#define CARDS_H_

was so as to prevent getting "redefinition of class" errors when the class is is defined in the cpp file because when I removed it from the cpp file, I got "redefinition of class" errors in both the header and cpp files. Also I thought that if I include the header file i.e. "Cards.h" in in my cpp file, it should make available all the items in the header file hence I should be able to use the namespace "cards" in the cpp file . Maybe I've got it all wrong. If I have, please let me know. The one thing which is still bugging me are the compiler errors I am getting when I try to use the srcut "deck" from the header file although I have included the scope of struct deck in my cpp file i.e. Cards::Deck

##### Share on other sites
Quote:
 Original post by quophyieI thought the idea behind #ifndef CARDS_H_#define CARDS_H_was so as to prevent getting "redefinition of class" errors when the class is is defined in the cpp file

Yup, this is precisely why it's done. It's pretty simple, all it's saying is "if I've never been here before, define all this class stuff. Otherwise, don't define anything."

However where you went wrong is that your .cpp file doesn't need this, only your header file. What you accomplish when you do that is your .cpp file includes the header, and defines CARDS_H_. Then when the compiler gets to the code in your .cpp file it sees the #ifndef, sees that CARDS_H_ is already defines, and skips all the code. Your other problem is that you're defining the class "Cards" twice: once in the header and once in the .cpp. Just implement the member functions in your .cpp, there's no need to define the class again.

Also...try to use the "source" tags when posting long bits of code. It gives it proper formatting and syntax highlighting, which makes it much easier to read and help you out.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 12
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004466

×