Error while trying to pass a struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SilverJester
    New Member
    • Feb 2007
    • 8

    Error while trying to pass a struct

    I'm getting the following error when trying to compile my code:
    error C2248: 'std::basic_ios <_Elem,_Traits> ::basic_ios' : cannot access private member declared in class 'std::basic_ios <_Elem,_Traits> '
    Not sure what it means, but it didn't start until after I added the "read_file" function (btw I know the function is mostly commented out, I'm still working on it). I think it has something to do with the fact that I am trying to pass a structure (acnt) to a function and is giving me an error for some reason. My code is below (parts have been removed to conserve space, and replaced with "..." but it is still in the same order as the original code).

    Code:
    struct acnt
    {
    	int number;
    	name *fname;
    	name *lname;
    	float balance;
    	char *next;
    };
    
    .
    .
    .
    
    void read_file(fstream, acnt);
    .
    .
    .
    
    void read_file(fstream in_file, acnt records)
    {
    	while (! in_file.eof() )	//read until end of file
        {
    		//in_file >> records.number;
    		//in_file >> records.fname;
    		//in_file >> records.lname;
    		//in_file >> records.balance;
    		cout<<"file read okay :)" <<endl;
        }
    
    }
  • SilverJester
    New Member
    • Feb 2007
    • 8

    #2
    I forgot the function call (which is the main function):

    Code:
    void main()
    {
            fstream file;
    	acnt records;
    	int choice;
    
    	file.open("banks.txt", ios::app | ios::out);
    	
    	if(!file)	// if file could not be opened
    	{
    	cerr << "Error: File could not be opened!" << endl;
    	exit(1);
    	}
    	read_file(file, records);	//populate records by reading from file
    .
    .
    .
    } //end main
    Oh and if it helps, I'm using microsoft visual studio 2005.

    Comment

    • hirak1984
      Contributor
      • Jan 2007
      • 316

      #3
      [font=Verdana][size=2]you didnt post the definition of the variable "name" [font=Arial][size=3]you have used in the struct.[/size][/font][/size][/font]
      Originally posted by SilverJester
      I forgot the function call (which is the main function):

      Code:
      void main()
      {
      fstream file;
      	acnt records;
      	int choice;
       
      	file.open("banks.txt", ios::app | ios::out);
       
      	if(!file)	// if file could not be opened
      	{
      	cerr << "Error: File could not be opened!" << endl;
      	exit(1);
      	}
      	read_file(file, records);	//populate records by reading from file
      .
      .
      .
      } //end main
      Oh and if it helps, I'm using microsoft visual studio 2005.

      Comment

      • SilverJester
        New Member
        • Feb 2007
        • 8

        #4
        Sorry forgot too post it. "name" is not a variable but rather another struct:

        Code:
        struct name
        {
        	char node;
        	name *next;
        
        	void new_name(char string[])		//Functoins copies a char array into a name.
        	{
        		node = string[0];
        		name *temp = this;
        
        		for(int i=0; i < strlen(string); i++)
        		{
        			temp->node = string[i];
        
        			temp->next = new name;
        			temp = temp->next;
        
        			temp->next = NULL;
        		}
        	}
        };

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          As of right now, you are passing your struct by value to your read function. This means that the function will create its own copy of that struct, put the data into the new struct, but not touch the original. When the function ends, the new struct is deallocated, and nothing has changed.

          To fix this, add an '&' after the struct type name. This will have the struct a reference variable, meaning that anything you do to it in the function will happen to the original struct.

          See if that will help.

          Comment

          • SilverJester
            New Member
            • Feb 2007
            • 8

            #6
            Thanks for the reply. Yes you are right I noticed that as well and already tried to change it but I still get the same error.

            Comment

            • AdrianH
              Recognized Expert Top Contributor
              • Feb 2007
              • 1251

              #7
              Originally posted by SilverJester
              Thanks for the reply. Yes you are right I noticed that as well and already tried to change it but I still get the same error.
              Don't forget to change the prototype.

              Code:
              void read_file(fstream&, acnt);
              .
              .
              .
              
              void read_file(fstream& in_file, acnt records)
              {
              ...
              }

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                Originally posted by AdrianH
                Don't forget to change the prototype.
                Code:
                void read_file(fstream&, acnt&);
                .
                .
                .
                
                void read_file(fstream& in_file, acnt& records)
                {
                ...
                }
                ...and also the acnt&.

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by Ganon11
                  Code:
                  void read_file(fstream&, acnt&);
                  .
                  .
                  .
                  
                  void read_file(fstream& in_file, acnt& records)
                  {
                  ...
                  }
                  ...and also the acnt&.
                  Definatly, if you want to see any changes in records. Actualy, you would have seen the changes in the c-string fields but not the int fields. That would have probably really messed you up. :)


                  Adrian

                  Comment

                  Working...