Using Struct and Class?

This is a discussion on Using Struct and Class? within the C++ Programming forums, part of the General Programming Boards category; Hi All, I'm trying to use a struct in conjuction with a class but am getting errors. Here's the code ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Using Struct and Class?

    Hi All,

    I'm trying to use a struct in conjuction with a class but am getting errors. Here's the code I'm trying to implement:

    Code:
    struct DataRec
    {
    	char 	 DataType;	
    	string        DataName;
    	string	 DataAddress;
    	int 	 DataClass;
    	string        DataSSN;
    	float	 DataPUB_GPA;
    	string        DataRank;
    };
    
    const int MAX_ITEMS = 6;
    typedef DataRec DataDB; 
    
    class PersonData
    {
    public:
    	void ReadInfo( DataDB data[], int& count );
    	//some functions
    	//
    private:
    	DataDB data[MAX_ITEMS];	
    	int count;					
    };
    I'm getting the following errors (just a few out of many):

    * 'data' : undeclared identifier
    * 'count' : undeclared identifier
    * subscript requires array or pointer type
    * left of '.DataType' must have class/struct/union type

    Can't figure this out. Any help would be appreciated.

    Thanks.

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    try:
    Code:
    typedef struct DataRec DataDB;

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Thanks, but still same errors -- good idea though.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    You typo'ed your declaration. Change to this:

    Code:
    
    class PersonData
    {
    public:
    	void ReadInfo( DataRec data[], int& count );
    	//some functions
    	//
    private:
    	DataRec data[MAX_ITEMS];	
    	int count;					
    };
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Nevermind, I see what you did. Change to this:

    Code:
    typedef struct DataRec
    {
    	char 	 DataType;	
    	string        DataName;
    	string	 DataAddress;
    	int 	 DataClass;
    	string        DataSSN;
    	float	 DataPUB_GPA;
    	string        DataRank;
    };
    
    const int MAX_ITEMS = 6;
    
    
    class PersonData
    {
    public:
    	void ReadInfo( DataRec data[], int& count );
    	//some functions
    	//
    private:
    	DataRec data[MAX_ITEMS];	
    	int count;					
    };



    Further, you might add this:




    Code:
    
    class DataRec  
    /*...much easier, right? Remember, a class and struct are equivalent, except that structs default to public, classes to private...*/
    {
    	char 	 DataType;	
    	string        DataName;
    	string	 DataAddress;
    	int 	 DataClass;
    	string        DataSSN;
    	float	 DataPUB_GPA;
    	string        DataRank;
    };
    
    
    
    class PersonData
    {
    public:
    
     PersonData(int max = 6) :max_items(max)
     {
      data = new DataRec[max_items];
     }
    
     ~PersonData()
     { 
      if(data) 
        delete[] data; 
     }
    
    void ReadInfo( DataRec data[], int& count );
    
    //...
    
    private:
    	DataRec *data;	
    	int count;
                    int max_items;					
    };
    This gives you more control over the size of the array, in a sense and also preserves encapsulation better...



    [edit]

    One more thing, you have a prototype argument that is identical to one of your classes internal variables:


    Code:
    void ReadInfo( DataRec data[], int& count );
    Change to something like:

    Code:
    void ReadInfo( DataRec _data[], int& count );

    Finally, you might consider simply storing a pointer to that parameter instead of having an entire copy within the object, thus saving yourself hundreds or thousands of bytes and also time spent copying.... If that suits you better then try:


    Code:
    
    class DataRec  
    {
    	char 	 DataType;	
    	string        DataName;
    	string	 DataAddress;
    	int 	 DataClass;
    	string        DataSSN;
    	float	 DataPUB_GPA;
    	string        DataRank;
    };
    
    
    
    class PersonData
    {
    public:
    
    
     PersonData() 
     {
      /*empty*/
     }
    
    
     PersonData(DataRec * pData, int& pCount) 
     {
       ReadInfo(pData, &pCount);
     }
    
    
     ~PersonData()
     { 
      /*empty*/
     }
    
    void ReadInfo( DataRec *pData, int& pCount ){
     data = pData;
     count = &pCount;
    }
    
    //...
    
    private:
    	DataRec *data;	
    	int * count; //..in case the count changes...
                    				
    };
    Last edited by Sebastiani; 10-01-2002 at 11:05 PM.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 4
    Last Post: 12-12-2002, 01:32 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21