Accessing Private Variables in Structures

This is a discussion on Accessing Private Variables in Structures within the C++ Programming forums, part of the General Programming Boards category; Hello, this is my first C++ assignment so please bear with me. I need to translate a C program to ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    166

    Accessing Private Variables in Structures

    Hello,
    this is my first C++ assignment so please bear with me. I need to translate a C program to C by making variables in structures private(no classes yet!) and putting public inline functions. There's a good chance that I have much more problems with my code than I'm asking right now, but I have 4 spots that I'm currently stuck in and can't access properly.

    My structures:
    Code:
    struct Container 
    {
    private:
        int count;
        char** lines;    
        int nlines;    
    public:
        int getCount(){ return count;}
    
    
        void setCount( ){ count = 0;}
        
        int getLineCount(){ return nlines;}
        
        char* getLine(int count){ return *(lines+count);}
        
        int addLine(char* line){
            *(lines + count++) = makeString(line);
            return nlines;
        }
        void deleteFile()
        {
            int x = 0;
            if (lines)
            {
                for(x = 0; x < nlines; x++) 
                    free(*(lines + x));
                free(lines);
            }
            //free(container); DON'T KNOW IF I NEED THIS!!
        }
    };
    Code:
    struct FileHandler
    {
    private:
        FILE* fpointer;
        struct Container* container;
    public:
        int countLines(FILE* fpointer){
            int count = 0;
            char line[ONEK];
            if (fpointer != NULL)
            {
                while (!feof(fpointer))
                    if (fgets(line, ONEK, fpointer)) count++;
                rewind(fpointer);    
            }
            return count;
            }
    
    
        void initContainer(){
        int count = countLines(fpointer);
        container = (struct Container*)malloc(sizeof(struct Container));
        container->setCount();
        container->getLineCount.nlines = count; // Problem #1 (both this problem and the one below throws error "left of must contain     class/struct/union
        container->getLine.lines = (char**)malloc( sizeof(char*)*container->addLine->nlines ); // Problem #2
        }
    }
    Code:
    struct Inventory 
    {
    private:
        Book* books;        //an array of books
        int nbooks;        //num of books in inventory
    public:
    int fillInventory(struct Container* cp){		int current = 0;
    		initInventory(cp->getLineCount());
    		if (cp != NULL) 
    		{
    		for (current = 0; current < cp->getLineCount(); current++)
    			books->initBook( (char*)(books + current), *(cp)->getLine(current)); //NO IDEA MOST LIKELY WRONG!
    		}
    		return nbooks;
    	}
    void deleteInventory(){
            if (books) 
            {
                int x;
                for (x = 0; x < nbooks; x++)
                    books->deleteBook((int)books + x);  // DON'T KNOW
                free(books);
            }
        }
    };
    And in my main:
    Code:
    void main()
    {   
            struct FileHandler fhandle;        
            struct Inventory inventory;
            printf("Processing File from: %s\n", FILE_NAME);        
            if (fhandle.processFile(FILE_NAME))
            {
                printf("Filling Inventory....\n");    
                inventory.fillInventory();        //Problem #3 (it needs to take struct Container cp, should I just add it in main?)
                inventory.showInventory();        
                printf("Cleaning up....\n");
                inventory.deleteInventory();    
                deleteFile(fhandle.container); //Problem #4 (no idea how to access)
            }
    }



    -Thank you for your time. Any tips or suggestions are greatly appreciated!
    Last edited by hotshotennis; 04-18-2013 at 02:17 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by hotshotennis View Post
    ...I need to translate a C program to C...
    Just confirming C -> C++?
    If so, you could start by getting rid of all C stuff, fgets (std::getline), printf (std::cout), malloc and free (std::vector, std::array), stop using heap allocation (use stack allocation), get rid of "struct" before struct types (eg struct FileHandler -> FileHandler), char arrays (std::string).

    It will make your program easier. Less to code, easier to read, harder to get wrong, and faster. Plus you will learn how to do things the C++ way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    166
    Quote Originally Posted by Elysia View Post
    Just confirming C -> C++?
    If so, you could start by getting rid of all C stuff, fgets (std::getline), printf (std::cout), malloc and free (std::vector, std::array), stop using heap allocation (use stack allocation), get rid of "struct" before struct types (eg struct FileHandler -> FileHandler), char arrays (std::string).

    It will make your program easier. Less to code, easier to read, harder to get wrong, and faster. Plus you will learn how to do things the C++ way.
    Thank you for your input! It is a C->C++ conversion. However, we are to leave the allocations, printf's, etc alone for now. (That will be the next assignment). The structures shouldn't be turned into classes yet either.
    Anyway, if I get rid of the "struct" before it, it throws an error. The assignment does specify to get rid of any typedefs, but I don't see any.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,640
    Quote Originally Posted by hotshotennis View Post
    The structures shouldn't be turned into classes yet either.
    Anyway, if I get rid of the "struct" before it, it throws an error. The assignment does specify to get rid of any typedefs, but I don't see any.
    You misunderstood Elysia's point (or one of them, anyway).

    Given a type definition like
    Code:
    struct SomeType
    {
         /*   members here */
    };
    the declaration of a variable of that type in C is;
    Code:
    struct SomeType x;
    In C++, the struct keyword is not needed when declaring a variable. It still required in the type definition though.


    By the way, main() returns int, not void. In both C and C++.

    It is a bad habit to explicit type conversions (aka type casts) to shut up the C++ compiler. Several of the places where you use type conversions are INVALID - the compiler might accept them, but any attempt to use the result causes undefined behaviour.

    For many reasons, using malloc() to dynamically allocate struct/class types in C++ is a REALLY BAD IDEA. There are lots of scenarios where it simply does not work
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,640
    Quote Originally Posted by hotshotennis View Post
    The structures shouldn't be turned into classes yet either.
    Anyway, if I get rid of the "struct" before it, it throws an error. The assignment does specify to get rid of any typedefs, but I don't see any.
    You misunderstood Elysia's point (or one of them, anyway).

    Given a type definition like
    Code:
    struct SomeType
    {
         /*   members here */
    };
    the declaration of a variable of that type in C is;
    Code:
    struct SomeType x;
    In C++, the struct keyword is not needed when declaring a variable. It still required in the type definition though.


    By the way, main() returns int, not void. In both C and C++.

    It is a VERY bad habit to use explicit type conversions (aka type casts) to shut up the C++ compiler. Several of the places where you use type conversions are INVALID - the compiler might accept them, but any attempt to use the result causes undefined behaviour. If the compiler is complaining where you have used a type conversion (and I can see, on a quick look) one instance where it will, it means you have converted to the wrong type.

    For many reasons, using malloc() to dynamically allocate struct/class types in C++ is a REALLY BAD IDEA. There are lots of scenarios where it simply does not work
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested Class Accessing Classes Private Variables
    By SterlingM in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2011, 01:06 AM
  2. Accessing private string
    By swappo in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2009, 01:53 PM
  3. Accessing private Data members
    By Emeighty in forum C++ Programming
    Replies: 17
    Last Post: 08-15-2008, 12:16 AM
  4. Question with accessing private data
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2008, 03:14 AM
  5. Setting And Accessing Private Member Variables
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2006, 07:00 PM

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