CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2005
    Posts
    8

    Post Errors from [testing...]

    Below is the main part of my program that I think I am having some diffculties, please give me some of your advice as to how I can fix the problem...
    Millions of thanks to you...

    Code:
    int func(){
    	int i;
    	char c;
    	...
    	struct s{
    		int funci(){
    			return ::i;}
    		//some others
    	};
    	//do something with struct;
    	
    	return somenumber;
    }
    
    int main(){
    	//testing...
    	s* ps;
    	int geti	=	 ps->funci();
    	int getii	=	 ps->i;
    	assert(geti,getii);
    	//do other things...
    	return 0;
    }
    ......

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Errors from [testing...]

    Your code is really very strange.
    Code:
    	s* ps;
    	int geti	=	 ps->funci();
    	int getii	=	 ps->i;
    You should allocate the structure before using the pointer.
    Even, if the first statement "could" work, the second statement, is almost sure to crash.
    Code:
    	struct s{
    		int funci(){
    			return ::i;}
    		//some others
    	};
    That structure is defined in the scope of the func function.
    If it is not defined outside, the main function will not be able to access it.
    That structure does not contain any member, so ps->i will surely do a compile-time error, except if you declared a different structure outside func.
    Code:
    			return ::i;}
    Did you defined a global variable named i?
    You should choose better names for global variables.

    You should probably read a C++ tutorial, or read a good C++ book.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #3
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Errors from [testing...]

    Code:
    assert(geti,getii);
    Finally, the assert macro takes only one argument.

    Do you mean?
    Code:
    assert(geti==getii);
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  4. #4
    Join Date
    Oct 2005
    Posts
    8

    Re: Errors from [testing...]

    Quote Originally Posted by SuperKoko
    You should allocate the structure before using the pointer.
    Even, if the first statement "could" work, the second statement, is almost sure to crash.
    Yes,I am sorry I made a mistake when using that pointer to call the function's return value, somenumber is the one relating to autovariable i, after some computations are performed on i...
    Quote Originally Posted by SuperKoko
    ...except if you declared a different structure outside func...You should probably read a C++ tutorial, or read a good C++ book.
    You guess correctly that I have another class to wrap up that function. And that I am reading a book too, but still don't know where the mistake is.
    Also, I found the error using assert(), which should have a condition inside rather than just parameters...
    Thanks SuperKoko.
    ......

  5. #5
    Join Date
    Oct 2005
    Posts
    8

    Re: Errors from [testing...]

    Here is a program I actually got, and don't know where to go on since some problems I would like to understand more are still there, as you once mentioned, right in the following source code...

    PHP Code:
    struct ss{
        
    int func(){
            
    int i;
            
    char c;
            ...
            
    struct s{    
                
    int funci(){
                    return ::
    i;}
                
    //some others
            
    };
            
    //do something with struct;
            
    return somenumber;
        }
        
    int funcContinue(){
            
    //code to call recursively this function
        
    }
        
    int i;    
    };
            
    int main(){
        
    //testing...
        
    sspss=new struct ss;
        
    sps;//Could you tell me how to initialize ps here ? since I want to use it.
        //pss was mistaken in place of ps and now has been already corrected.
        
    int geti    =     pss->func();
        
    int getii    =     pss->i;
        
    //using ps to get i variable 
        
    int getiii    =    pss->ps->funci();
        
        
    assert(geti==getii);

        
    //do other things...

        
    return 0;

    Thanks a lot
    Last edited by Cungtrang; October 23rd, 2005 at 09:22 AM. Reason: Clearify the source code
    ......

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Errors from [testing...]

    Please post compilable code (I mentioned this in the other thread you started).

    Code:
    	ss* pss=new struct ss;
    This is not necessary.

    1) You do not need to dynamically allocate anything here. Just create an instance of ss.

    2) You don't need "struct ss" -- just "ss" is necessary.

    3) Your program has a memory leak. You never called "delete" at the end of your program to release the memory that you allocated dynamically.

    4) You cannot access a local struct from outside the function ss:func(), therefore "s" cannot be initialized in main().

    5) funci() cannot return the "i" declared in the outer struct ss. It is a local struct with no knowledge of anything outside of it. Unless the "i" in ss is a static member, it cannot be returned.

    So what you posted is an impossibility, to put it in more simpler terms.

    This is as close as I could get in compiling something that is actually compilable:
    Code:
     struct ss
     {
        int func()
        {
            int i;
            char c;
            struct s
            {    
                int funci()
                {
                    return 0;
                }
                //some others
            };
            //do something with struct;
            return 0;
        }
        int i;     
     };
    
    int main()
    {
        ss pss;
        pss.i = 1;
        int geti   = pss.func();
        return 0;
    }
    No pointers are necessary here at all. You mentioned that you are reading a C++ book. What is the name of the book?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured