newbie question regarding usage of structures in class

This is a discussion on newbie question regarding usage of structures in class within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I have an issue thats befuddling me. I am attaching the code below. My problem is in getArrayStruct ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    newbie question regarding usage of structures in class

    Hi Guys,

    I have an issue thats befuddling me. I am attaching the code below. My problem is in getArrayStruct method, I am not able see the contents of my structure. I think the problem may be a simple mistake but I am not able to pinpoint it. The program compiles and runs okay but the contents of structure returned by getArrayStruct print gibberish, like they have not been initialized. Any pointers/guidance on this is very much appreciated.

    Thanks in advance
    -sam


    Code:
    structure.h
        struct structForArray {
    	        int size;
    	        int position;
    	        int mark;
    	        int *byteArray;
        };
    
    
    
    
    And a class that uses it.. thus
    
    abcd.h
    #include "structure.h"
       class abcd {
    
    	      public :	
    		               structForArray *source;
    ....
    	      public :
                                   abcd(char *filename ) {
                                    //call a function to populate the struct
                                    populate (filename);
    
                                   ~abcd() {}
    
                  private:
                                  void populate(char *inputfilename);
                                  struct structForArray *getArrayStruct();
    };
    
    
    
    
    
    abcd.cpp
    #include "abcd.h"
               void abcd::populate(char *inputfilename) {
    
                     //read from file
    
                    //populate structure 
    
            structForArray *inputStream;
            structForArray inputStr = {0, 0, 0, NULL};
            inputStream = & inputStr;
            ...
    
                   //for loop ...populating the struct
    
                 
                  this->source = inputstream;
    
                  return;
    }
    
    
    struct structForArray *abcd::getArrayStruct() {
           //here I am trying to check source  structure and return it
           //but it is empty
    
           return this->source;
    }
    Last edited by samual_van; 04-27-2005 at 05:46 PM. Reason: title change

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>this->source = inputstream;

    Now source is pointing to an object that goes out of scope at the end of the function. You might as well just remove those temporary variables and set source's members directly. And there's no need to use the this pointer in this case either.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Hi JawIB,
    Thanks for your help. I changed the populate function, But still in the getArrayStruct function I am doing something I am not supposed to. It is still printing gibberish. Here is the function:

    Code:
        struct structForArray *abcd::getArrayStruct() {
    
            if (this->source != NULL) {
    
                cout << "Size...... " << this->source->size << "\n\n\n";
                cout << "position...... " << this->source->position << "\n\n\n";
                cout << "mark...... " << this->source->mark << "\n\n\n";
                int x = 0;
                while (x < 32) {
                    //inputStream->byteArray[x] = x + 1;
                    cout << "Byte=== " << x << ": " << source->byteArray[x] <<"\n";
                    x++;
                }
    
                return this->source;
            } else {
                return NULL;
            }
        }
    Can you tell me what I am doing wrong??

    Thanks a lot for your help.

    -Sam

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Either don't declare source as a pointer or allocate/deallocate it with new and delete. And again, there's no need to use the this pointer here.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By vastgoten in forum C++ Programming
    Replies: 4
    Last Post: 07-31-2008, 02:43 AM
  2. class and pointer question
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2006, 11:00 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

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