Thread: returning class and struct members

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    61

    returning class and struct members

    Hi there; this question may be trivial to some, since I am new to C++, although I come from C.

    I have a struct that defines a type (typeInput), and a class with a funtion that uses some of the members of that struct (see the code below).

    What I have not been able to do is the following:
    once I use the class function mesh.ReadInput to assign two strings from an input file, I cannot pass them into an other function of the same class.
    Specifically, if you look at the code reported hereafter (I divided it in [1],[2], and [3] for the sake of clarity), after assigning correctly two strings to what are called Input.msh_file, when I print its value in main after the call to ReadInput, I get an empty output.

    Can anyone help?
    Thank you very much in advance,
    S.

    1] Struct and Class definitions (Mesh.H):
    Code:
    typedef struct InputTag{
    
    	char msh_file[32];
    	int nsd;
    	
    } typeInput;
    
    class Mesh{
    
    	private:
    		int 	nsd;		
    		int	nelem;
    		int 	nnodes;			// number of nodes (of the mesh).
    
    	public:
    
    	typeInput 	Input;
    		
    	Mesh(void);			// constructor
    	
    	//Functions:
    	void Mesh::ReadInput(char *input_file, char *msh_generator, char *mesh_file, int *nsd);
    
    
    };
    //END Class and Struct definitions
    2] Mesh.cpp
    Code:
    void Mesh::void Mesh::ReadInput(char *input_file, char *msh_generator, char *mesh_file, int *nsd)
    {
    	FILE *input_file_ID;
    	
    	/*********************************************************/
    	//Start reading:
    	
    		input_file_ID = fopen(input_file, "r");
    		
    		//Read Entries of input file start now:
    			fscanf(input_file_ID, "%s \n", &(Input.msh_file));
    
    }; //END Mesh.cpp where Mesh::ReadInput is defined.
    3] main.cpp
    Code:
    int main(int argc, char *argv[])
    {
    	
    	/********************************/
    
    	Mesh	mesh = Mesh();
    
    int nsd, nnodes, nelem, nbdy_nodes, max_elnodes;
    	
    	char *msh_file, *msh_generator;
    	
    	/*********************************/
    	
    	msh_file = (char*) malloc(32 * sizeof(char));
    	msh_generator = (char*) malloc(32 * sizeof(char));
    
    //Read input file:
    		mesh.ReadInput( argv[1], msh_file, msh_generator, &nsd);
    
                  
      //!!! HERE IS THE PROBLEM!!! When I print out the strings msh_file and msh_generator that
                      should have been assigned in mesh.ReadInput, I get an empy  output:
    		printf("msh_file :: %s \n", msh_file);
    
    
       //Free dyn. memory
       free(msh_file);
       free(msh_generator);
    
    return 0;
    }
    Last edited by simone.marras; 03-16-2009 at 10:09 AM.

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. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 02:11 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread