Using Structs from header file in cpp file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • quophyie
    New Member
    • Mar 2008
    • 4

    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
    Code:
    #ifndef CARDS_H_
    #define CARDS_H_
    #include <iostream>
    #include <string>
    #include <vector>
    
    
    
    namespace pack{
    
    
    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
    
    
    #include "Cards.h"
    #include <iostream>
    #include <vector>
    
    using namespace std;
    using namespace pack;
    
    class Cards{
    
    
    
    public:
    
    Cards();
    ~Cards();
    
    Cards::deck getCard();
    Cards::deck dec;
    
    };
    
    
    
    Cards::Cards()
    {
    
    
    }
    
    Cards::~Cards()
    {
    }
    
    Cards::deck Cards::getCard(){
    
    
    }
    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.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Why u have defined the class cards in both the cpp and the h file?

    Raghuram

    Comment

    • quophyie
      New Member
      • Mar 2008
      • 4

      #3
      Sorry but I forgot to comment that ie.e. #include "Cards.h"ou t when I was posting the code. I still get errors when I compile even with the line commented out. Help!!

      Comment

      • quophyie
        New Member
        • Mar 2008
        • 4

        #4
        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
        Code:
        #ifndef CARDS_H_
        #define CARDS_H_
        #include <iostream>
        #include <string>
        #include <vector>
        
        using namespace std;
        
        namespace pack{
        
        
        class Cards
        {
        
        public:
        	Cards();
        	 ~Cards();
        	 
        	struct deck{
        
        		// this holds the description of the card e.g. "King Of"
        		std::string card_name;
        		
        		//card type e.g. "Hearts", Spades, etc
        		std::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


        Code:
        #include "Cards.h"
        #include <iostream>
        #include <vector>
        
        using namespace std;
        
        class Cards{	
        public:
        	Cards();
        	~Cards();
        	
        	/*defines a deck with a return type of deck i.e. Note that 
        	 * the return type deck is from the namespace pack which is declared and defined in
        	 * the the header file "Cards.h"
        	 */ 
            pack::Cards::deck dec;
        
        /*defines a getCard with a return type of deck i.e. Note that 
         * the return type deck is from the namespace pack which is declared and defined in
         * the the header file "Cards.h"
         */ 
        pack::Cards::deck getCard();
        };
        
        Cards::Cards()
        {
        	
        	
        }
        
        Cards::~Cards()
        {
        }
        
        pack::Cards::deck Cards::getCard(){
        	
        	return dec;
        }

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          Good to hear that u have solved the problem

          Raghuram

          Comment

          Working...