A class to contain structs

This is a discussion on A class to contain structs within the C++ Programming forums, part of the General Programming Boards category; Hello, I want to try and make something like this: Code: class FSAD //Fixed/Selectable Analog Data { public: char Name[25]; ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    A class to contain structs

    Hello,

    I want to try and make something like this:
    Code:
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    
    		void MakeSec()
    		{
    			DataLine *pDataSec;
    
    			pDataSec = new DataLine[nExpected*2+1];
    		}
    	};
    Where the DataLine is:
    Code:
    struct DataLine
    {
    	char Param[20];
    	int Label, SFactor, SigBits;
    	int Bit30, Bit31, Max, Min;
    } ;
    If not sure if I call the constructor, How can I later access the variables in the array of structs inside the class?

    The class is supposed to have two variables (char Name[25], int nExpected) and an array of structs (DataLine). I can't get to the individual variables inside the struct.
    I tried using this and the compiler yelled at me:
    Code:
     Section.pDataSec[i].Param
    Ideas?

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Your code has a memory leak in it as you allocate a DataLine locally in MakeSec() (which, btw, cannot be accessed from outside MakeSec()), then fail to free it when it exits. Perhaps you want something like this:

    Code:
    struct DataLine{//everything public by default
    	char Param[20];
    	int Label, SFactor, SigBits;
    	int Bit30, Bit31, Max, Min;
    	DataLine(){//a REALLY GOOD IDEA to have a constructor initialize variables
    		Label=SFactor=SigBits=Bit30=Bit31=Max=Min=0;
    		memset(Param, 0, sizeof(Param));
    	};
    } ;
    
    class FSAD{        //Fixed/Selectable Analog Data  
    public:
    	char Name[25];
    	int nExpected;  // size = nExected*2+1
    	DataLine dl;
    
    	void MakeSec(){
    		;//use dl here
    	};
    };
    
    FSDA Section;
    
    strcncpy(Section.dl.Param, "something", 20);

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I've realised that my array of structs was being destroyd once the program finishes with the constructor. I've fixed that same way you did.
    Code:
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    		DataLine *pDataSec;
    
    		void MakeSec(DataLine *pDataSec)
    		{
    			pDataSec = new DataLine[nExpected*2+1];
    		}
    	};
    I think the problem comes from the way I pass it to a function. I'm writing a function to fill the array of structs. and I'm not sure how to pass the class to it correctly:
    Code:
    void ReadDataSec(FILE * inp, FSAD Section , int nExpected)
    {
    	char line[100];
    	int  i=0;
    
    	while ( fgets(line, sizeof line, inp) != NULL ) // reads in a line of text from file
    	{
    		//if ( strncmp(line, "ENDL", 4) == 0 ) // Exit if at the end of a section
    		//{
    			sscanf(line, "%s", Section.pDataSec[i].Param);
    			printf("%s \n", Section.pDataSec[i].Param);
    
                break;
    	}
             //}
    
    }
    If I don't use a function, and reference the array of stucts like this:
    Code:
    strncpy(FAD.pDataSec[0].Param, "something", 20);
    	printf("%s\n", FAD.pDataSec[0].Param);
    I get an run-time error that the memory could not be written.
    Last edited by earth_angel; 06-27-2005 at 08:59 AM.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    First, you must provide a destructor to deallocate the memory you allocate for the struct array! Second, why not just use a vector instead, so you don't have to worry about deallocating the memory?

    example:

    Code:
    struct DataLine{//everything public by default
    	char Param[20];
    	int Label, SFactor, SigBits;
    	int Bit30, Bit31, Max, Min;
    	DataLine(){//a REALLY GOOD IDEA to have a constructor initialize variables
    		Label=SFactor=SigBits=Bit30=Bit31=Max=Min=0;
    		memset(Param, 0, sizeof(Param));
    	};
    } ;
    
    class FSAD{        //Fixed/Selectable Analog Data  
    public:
    	char Name[25];
    	int nExpected;  // size = nExected*2+1
    	vector < DataLine > vctDL;
    
    	//void MakeSec() no need for this now
    };
    
    void ReadDataSec(FILE * inp, FSAD &Section , int nExpected)//note that Section MUST be passed in by reference!
    {
    	char line[100];
    	int  i=0;
    	DataLine dl;
    
    	while ( fgets(line, sizeof line, inp) != NULL ) // reads in a line of text from file
    	{
    		//if ( strncmp(line, "ENDL", 4) == 0 ) // Exit if at the end of a section
    		//{
    			sscanf(line, "%s",dl.Param);
    			printf("%s \n", dl.Param);
    			Section.vectDL.push_back(dl);//this does a copy, this could be more efficient with references, but I wouldn't worry about that now.
    
                break;
    	}
             //}
    
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    If it's just one of the structs it's fine, But I need an array of them the size of which is determined at run-time.

    and I never learned ho wto work with Vectors, I'll take a look at that.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I've read a few things about vectors,but I don't understand it enough to start playing with them.
    After I've implemented all the suggestions to the best of my ability, this is what I came up with:

    Function to work on the class:
    Code:
    void ReadDataSec(FILE * inp,class FSAD &Section , int nExpected)
    {
    	char line[100];
    	int  i=0;
    
    	GetWord(inp, &Section.pDataSec[0].Param);
    	printf("%s\n\n", *Section.pDataSec[0].Param);
    
    }
    Class definition:
    Code:
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    		DataLine pDataSec;
    
    		void MakeSec(DataLine *pDataSec)
    		{
    			pDataSec = new DataLine[nExpected*2+1];
    		};
    
    		~FSAD()
    		{
    			delete[] pDataSec;
    		};
    	};
    and the function call in main:
    Code:
    ReadDataSec(inp, FAD, FAD.nExpected);
    and the problems, oh I hate them:
    Code:
    error C2440: 'delete' : cannot convert from 'struct DataLine' to ''
    ....
     error C2676: binary '[' : 'struct DataLine' does not define this operator or a conversion to a type acceptable to the predefined operator
    H:\Code\EDFParse3\ReadDataSec.cpp(8) : error C2228: left of '.Param' must have class/struct/union type

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>DataLine pDataSec;

    Did you forget a * to make pDataSec a pointer to type DataLine???
    You're only born perfect.

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Its good practice, and maybe a "standard" for companies to have the variables in a class be private and have special get and set functions for the variables if needed.

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I did forget a *. That's because I was trying all kinds of things to make it work, but nothing seems to.

    Now a static array works, but a dynamic one through MakeSec call gives a run-time error, that memory could not be written.

    And I'm not really worried about the public and private thing right now. I will look into that later if need be. thanks though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 05:30 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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