Thread: Visual C++ Help!!

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    16

    Visual C++ Help!!

    For some reason everytime I try to compile a program that has a function in it, I get the following error:

    "error C2601: 'larger' : local function definitions are illegal
    Error executing cl.exe."

    In that particular case the function was named larger. Does anyone know what I might be doing wrong? I've followed code from my text books, and also tried to create my own functions to no avail.

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    Can you post some code? The error looks like you're trying to nest functions (which is illegal in C/C++).
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    Here's a very simple function that I tried.

    Code:
    	double larger(double x, double y)
    		{
    			if (x >= y)
    				return x;
    			else 
    				return y;		
    
    		}

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    11

    hey sorry im anewb but maybe

    maybe you need to prototype it maybe? sorry if im wrong but im just guessing but im a newb. Something this?

    Code:
    double larger(double x, double y);
    
    double larger(double x, double y)
    	{
    		if (x >= y)
    			return x;
    		else 
    			return y;		
    	}

    Like i said i know barely nothing so maybe someone else might know something better. good luck

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    Here's my entire program:

    Code:
    #include<iostream>
    using namespace std;
    
    double larger(double, double);
    
    void main()
    {
    	double one, two;
    	
    	cout<<"Enter two numbers: ";
    	cin>>one>>two;
    	cout<<endl;
    
    	cout<<"Ther larger number is:  "<<larger(one, two);
    
    	
    	
    	
    	
    	//Function Definition
    	double larger(double x, double y)
    		{
    			if (x >= y)
    				return x;
    			else 
    				return y;		
    
    		}
    
    }
    As you can see the prototype is before the main function.

  6. #6
    root
    Join Date
    Sep 2003
    Posts
    232
    What did I tell you before? Functions inside functions are illegal.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    6
    In case you don't understand twm, main is a function. Therefore you need to cut your function definition and paste it after the close brace of main.

  8. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    Thanks a lot twm! I wasn't thinking straight. I didn't see main as a function. That helps a whole bunch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM