returning class and struct members

This is a discussion on returning class and struct members within the C++ Programming forums, part of the General Programming Boards category; Hi there; this question may be trivial to some, since I am new to C++, although I come from C. ...

  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.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    should have been assigned in mesh.ReadInput
    You have not a single reason to beleave this statement

    As a side not - this is C++ - you should use iostrem, std::string, new/delete

    Why do you have such a horrible mess of C/C++?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by vart View Post
    You have not a single reason to beleave this statement

    As a side not - this is C++ - you should use iostrem, std::string, new/delete

    Why do you have such a horrible mess of C/C++?
    Hi there, thanks for replying. It's actually a mixed of C/C++ because most of the functions I am using I had them already written in C. However, I used a new thing that I need to solve as an excuse to start finally switching into C++.

    Can you help with that?

    thanks again
    S.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by simone.marras View Post

    Can you help with that?
    Look at the function under question. Check what parameters it really uses (compiler should warn you about parameters that are not used)

    get rid of unused parameters. and check what function really does, not what you think it does
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by vart View Post
    Look at the function under question. Check what parameters it really uses (compiler should warn you about parameters that are not used)

    get rid of unused parameters. and check what function really does, not what you think it does
    Hi again; actually I left some variables in this forum although they are not used within this simplified version of the code. All those variables are actually used, and the problem that occurs is that the function mesh.ReadInput does NOT return the variables that are meant to be passed back to the calling routine (main.cpp in this case)

    S.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by simone.marras View Post
    Hi again; actually I left some variables in this forum although they are not used within this simplified version of the code. All those variables are actually used, and the problem that occurs is that the function mesh.ReadInput does NOT return the variables that are meant to be passed back to the calling routine (main.cpp in this case)

    S.
    I cannot know what you have in the hidden code. I only see the code been posted. in this code function does nothing with the supplied parameters, so I do not see why they should be filled with anything
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by vart View Post
    I cannot know what you have in the hidden code. I only see the code been posted. in this code function does nothing with the supplied parameters, so I do not see why they should be filled with anything
    It actually does; it reads it a string and assigns it to the variable msh_file:

    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));

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    no. it is not.


    it works with the member of meber variable Input
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by vart View Post
    no. it is not.


    it works with the member of meber variable Input
    Thanks about the hint. Then this was my initial question; how is it supposed to be done?
    That is why I am using this forum;

    Regards
    S.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by simone.marras View Post
    Thanks about the hint. Then this was my initial question; how is it supposed to be done?
    That is why I am using this forum;

    Regards
    S.
    I have answeres it already, but I can reapeate... I fill I'm reapeting myself too much in this thread

    1. get rid of unneded parameters in the function - pay attention to the compiler warnings
    2. switch to using C++ strings
    3. switch to using iostream
    4. switch to using new/delete
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Why do you think it should be &(Input.msh_file)? Do you know what that actually does?
    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.

  12. #12
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by Elysia View Post
    Why do you think it should be &(Input.msh_file)? Do you know what that actually does?
    Hi there Elysa; thanks for replying; I believe that fscanf(file_ID, "%s", &Input.msh_file) assigns to Input.msh_file the variable read inside file_ID in the order given by the file itself?

    From your question I think I am getting the meaning wrong, correct?

    Thank you
    S.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    Yes, that's right. So why are you trying to print the contents of msh_generator?
    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.

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Hi Elysia,

    Sorry about my distraction. I put a print for Input.msh_generator instead Input.msh_file because the same operation is actually done on Input.msh_generator; I just edited the code now uphere to correct this distraction. Again, in the original code they are both done.

    I want to print them as a first try to see if the are both returned from the function that assigns them with the proper quantity; the real operation though, would be passing them to a next function that is supposed to use those values for further operations.

    Simply, I am getting it wrong on how to have a class function operate on certain arguments and then pass them to another function that belongs to the same class and that uses the same (or other structs as well)

    Thank you again for helping

    Best
    S.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    But if you take a look at the function again, it reads into Input.msh_file, not the argument msh_file, so while named the same, you are reading or printing the wrong variable!
    And I am pretty sure you are getting some warnings and/or errors in that code. How about fixing them first, and changing to C++ code as vart said?
    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.

Page 1 of 2 12 LastLast
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, 01: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, 06: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


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