473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using Structs from header file in cpp file

Hi guys

I''m a new C++ programmer and I am having a few problems with some structs that I have defined in my header file and want to use in my CPP file. The struct called "deck" is defined in a file called "Cards.h". In the corresponding CPP file called "Cards.cpp" when I try to use the struct "deck", I get a lot of errors from the compiler as displayed in my compiler errors below. I have tried all manner of things including defing variables without the scope ie.. deck dec; in the cpp file but nothing seems to work. I was hoping one of you guys will point out to me if there is something that I'm doing wrong cos I cannot see it. Also, one other thing, If I define a namespace in my header file, am I able to use it in my cpp file? I'm only asking this cos I have defined a name called "pack" in my header file but when I try to use it in my cpp file, get an error saying that "pack is not s namespace name" as is illustrated in my compiler errors. I am using mingw 3.4.5. Please Help!!!

The Header file
Expand|Select|Wrap|Line Numbers
  1. #ifndef CARDS_H_
  2. #define CARDS_H_
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6.  
  7.  
  8.  
  9. namespace pack{
  10.  
  11.  
  12. typedef struct sample
  13. {
  14. int a;
  15. char b, c;
  16. }
  17. SAMPLE_STRUCT;
  18. //////
  19. }
  20.  
  21.  
  22. class Cards
  23. {
  24.  
  25. public:
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. Cards();
  33. virtual ~Cards();
  34.  
  35.  
  36. struct deck{
  37.  
  38. // this holds the description of the card e.g. "King Of"
  39. string card_name;
  40.  
  41. //card type e.g. "Hearts", Spades, etc
  42. string card_type;
  43. /*
  44. * the value of the card e.g, 2,3 6 etc Note that Jack = 10
  45. * Queen = 11, King = 12, Ace = 13
  46. */
  47. int card_value;
  48. };
  49.  
  50. deck getCard();
  51. };
  52.  
  53.  
  54.  
  55.  
  56.  
  57. #endif /*CARDS_H_*/
  58.  
  59.  
  60.  
  61. the cpp file
  62.  
  63.  
  64. #include "Cards.h"
  65. #include <iostream>
  66. #include <vector>
  67.  
  68. using namespace std;
  69. using namespace pack;
  70.  
  71. class Cards{
  72.  
  73.  
  74.  
  75. public:
  76.  
  77. Cards();
  78. ~Cards();
  79.  
  80. Cards::deck getCard();
  81. Cards::deck dec;
  82.  
  83. };
  84.  
  85.  
  86.  
  87. Cards::Cards()
  88. {
  89.  
  90.  
  91. }
  92.  
  93. Cards::~Cards()
  94. {
  95. }
  96.  
  97. Cards::deck Cards::getCard(){
  98.  
  99.  
  100. }
  101.  
  102.  
Compiler Errors:



**** 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:9: error: `pack' is not a namespace-name
..\Cards.cpp:9: error: expected namespace-name before ';' token
..\Cards.cpp:20:2: warning: "/*" within comment
..\Cards.cpp:46: error: expected `;' before "getCard"
..\Cards.cpp:47: error: expected `;' before "dec"
..\Cards.cpp:63: error: expected constructor, destructor, or type conversion before "Cards"
Build error occurred, build is stopped
Time consumed: 14359 ms.
Mar 21 '08 #1
4 5126
gpraghuram
1,275 Expert 1GB
Why u have defined the class cards in both the cpp and the h file?

Raghuram
Mar 22 '08 #2
Sorry but I forgot to comment that ie.e. #include "Cards.h"out when I was posting the code. I still get errors when I compile even with the line commented out. Help!!
Mar 22 '08 #3
I finally overcame this problem by defining the struct and class in a namespace in the header file and to get access to the struct and methods in the namespace from the cpp, I used the scope operator. I know that for some experts, this may be obvious but for us newbies, it can sometimes be mind boggling to get our heads around these things. Anyway here is the code.

the header
Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef CARDS_H_
  3. #define CARDS_H_
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. namespace pack{
  11.  
  12.  
  13. class Cards
  14. {
  15.  
  16. public:
  17.     Cards();
  18.      ~Cards();
  19.  
  20.     struct deck{
  21.  
  22.         // this holds the description of the card e.g. "King Of"
  23.         std::string card_name;
  24.  
  25.         //card type e.g. "Hearts", Spades, etc
  26.         std::string card_type;
  27.         /*
  28.          * the value of the card e.g, 2,3 6 etc Note that Jack = 10
  29.          * Queen = 11, King = 12, Ace = 13
  30.          */
  31.         int card_value;
  32.         };
  33.  
  34.     deck getCard();
  35.  
  36.     };
  37.  
  38. }
  39. #endif /*CARDS_H_*/
  40.  
  41.  

the cpp file


Expand|Select|Wrap|Line Numbers
  1. #include "Cards.h"
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Cards{    
  8. public:
  9.     Cards();
  10.     ~Cards();
  11.  
  12.     /*defines a deck with a return type of deck i.e. Note that 
  13.      * the return type deck is from the namespace pack which is declared and defined in
  14.      * the the header file "Cards.h"
  15.      */ 
  16.     pack::Cards::deck dec;
  17.  
  18. /*defines a getCard with a return type of deck i.e. Note that 
  19.  * the return type deck is from the namespace pack which is declared and defined in
  20.  * the the header file "Cards.h"
  21.  */ 
  22. pack::Cards::deck getCard();
  23. };
  24.  
  25. Cards::Cards()
  26. {
  27.  
  28.  
  29. }
  30.  
  31. Cards::~Cards()
  32. {
  33. }
  34.  
  35. pack::Cards::deck Cards::getCard(){
  36.  
  37.     return dec;
  38. }
  39.  
  40.  
Mar 23 '08 #4
gpraghuram
1,275 Expert 1GB
Good to hear that u have solved the problem

Raghuram
Mar 24 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Nafai | last post by:
Hi. Have a look at this program: struct tmp { short a; int b; short c; }; int main() {
3
by: Christian F | last post by:
Hi, I'm a C-newbie and I would like to know if I am doing something wrong in the code below. It is working, but I'm afraid it might not be correct because I don't really understand everything of...
5
by: Grant Austin | last post by:
What would be the correct syntax for setting up a dynamic array of structs? Suppose you have a struct declared: struct relation { FILE * binFile; unsigned int numAttrs; struct attrList *...
2
by: spleen | last post by:
Hiya, Im a newbie to C so please excuse my ignorance... Ok, so I have several C files, with my functions in, one has main in of course! and I have 2 structs as outside of the funtions...
0
by: Arthur Autenzio | last post by:
I have written a managed C++ wrapper class around an old C++ DLL that I have to use in my application. My problem is I need to marshal a bunch of structures from C# to the C++ class. These...
3
by: MSDousti | last post by:
Hello I want to write a C# (or VB.NET) program which reads the structure of a PE (odinary win32 executable) file. These files have a long header (512 bytes or so). The definition of the header...
4
by: MSDousti | last post by:
Hi all, I read some Q&As in the Net, discussing Marshalization of nested structs in C# (or VB.NET). Some guys stated that .NET framework does not support this feature yet. Are they right? ...
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
4
by: KioKrofov | last post by:
Currently I have (a much bigger version of) the code below in my main cpp file: struct myData { int dataCodeNum; char* dataName; } myData arrayOfData1={ { 1, ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.