undefined reference to function names

This is a discussion on undefined reference to function names within the C Programming forums, part of the General Programming Boards category; Ok I've already spent hours agonising of why my program didn't work and it turned out Switch should have been ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    21

    undefined reference to function names

    Ok I've already spent hours agonising of why my program didn't work and it turned out Switch should have been switch, but the error message "should have ; before {" was not very helpful.

    Anyway now I am stuck again, I've made so many errors on this program that have been obvious, only it took me hours to figure out, but I think if this problem is figured out it hopefully will work.

    Error message is:
    /tmp/cc4nHzG5.o: In function `main':
    test.c.text+0x106): undefined reference to `qadd'
    test.c.text+0x118): undefined reference to `qadd'
    test.c.text+0x150): undefined reference to `qcheck'
    The error applies to the other math functions to, I had to cut the quote short because it printed the smiley faces and there is a smiley face limit.

    My program is not finished yet, but still...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int qadd(int a, int b);
    int qsubtract (int a, int b);
    int qmultiply (int a, int b);
    int qdivide (int a, int b);
    int qcheck (int holdans);
    
    
    
    	
    
    main()
    
    {
    
    	int score = 0;
    	int rs;			/* Random math operator */
    	int ran1;		/* Random number 1 */
    	int ran2;		/* Random number 2 */
    	int holdans;		/* Get answer from function and insert in another function to check if user guesses correctly */
    
    
    	
    	
    
    	int i;
    	for (i=0; i<=9; ++i)	/* Ask 10 questions */
    	{
    		rs = rand() % 4 + 1;	/* Random math operator (+,-,*, /) */
    		ran1 = rand() % 10 + 1;	/* Random number between 1 and 10 */
    		ran2 = rand() % 10 + 1;
    	
    
    		switch (rs)
    		{
    			case 1:
    				qadd(ran1, ran2);   
    				holdans = qadd(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'+',ran2,'=');   
    				qcheck (holdans);
    				break;
    			case 2:
    				qsubtract(ran1, ran2);
    				holdans = qsubtract(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'-',ran2,'='); 
    				qcheck(holdans);
    				break;
    
    			case 3:
    				qmultiply(ran1, ran2);
    				holdans = qmultiply(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'*',ran2,'='); 
    				qcheck (holdans);
    				break;
    
    			case 4:
    				qdivide(ran1, ran2);
    				holdans = qdivide(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'+',ran2,'='); 
    				qcheck (holdans);
    				break;
    		}
    
    	}
    
    	int qcheck (holdans)
    	{
    		int guess;
    		
    		scanf(" %d", &guess);
    		if (guess == holdans) 
    		{
    			printf("Correct");
    		}
    		else
    		{
    			printf("Wrong");
    		}
    	}
    
    
    	int qadd (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 + ran2;
    		return ans;
    	}
    
    	int qsubtract (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 - ran2;
    		return ans;
    	}
    
    	int qmultiply (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 * ran2;
    		return ans;
    	}
    	
    	int qdivide (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 / ran2;
    		return ans;
    	}
    
    
    
    
    }
    If it is obvious, please give a clue.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't put qadd inside main. It is its own function.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by hellogamesmaste View Post
    Ok I've already spent hours agonising of why my program didn't work and it turned out Switch should have been switch, but the error message "should have ; before {" was not very helpful.

    Anyway now I am stuck again, I've made so many errors on this program that have been obvious, only it took me hours to figure out, but I think if this problem is figured out it hopefully will work.

    Error message is:


    The error applies to the other math functions to, I had to cut the quote short because it printed the smiley faces and there is a smiley face limit.

    My program is not finished yet, but still...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int qadd(int a, int b);
    int qsubtract (int a, int b);
    int qmultiply (int a, int b);
    int qdivide (int a, int b);
    int qcheck (int holdans);
    
    
    
    	
    
    main()
    
    {
    
    	int score = 0;
    	int rs;			/* Random math operator */
    	int ran1;		/* Random number 1 */
    	int ran2;		/* Random number 2 */
    	int holdans;		/* Get answer from function and insert in another function to check if user guesses correctly */
    
    
    	
    	
    
    	int i;
    	for (i=0; i<=9; ++i)	/* Ask 10 questions */
    	{
    		rs = rand() % 4 + 1;	/* Random math operator (+,-,*, /) */
    		ran1 = rand() % 10 + 1;	/* Random number between 1 and 10 */
    		ran2 = rand() % 10 + 1;
    	
    
    		switch (rs)
    		{
    			case 1:
    				qadd(ran1, ran2);   
    				holdans = qadd(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'+',ran2,'=');   
    				qcheck (holdans);
    				break;
    			case 2:
    				qsubtract(ran1, ran2);
    				holdans = qsubtract(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'-',ran2,'='); 
    				qcheck(holdans);
    				break;
    
    			case 3:
    				qmultiply(ran1, ran2);
    				holdans = qmultiply(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'*',ran2,'='); 
    				qcheck (holdans);
    				break;
    
    			case 4:
    				qdivide(ran1, ran2);
    				holdans = qdivide(ran1,ran2);
    				printf("\n \n: %d %c %d %c",ran1,'+',ran2,'='); 
    				qcheck (holdans);
    				break;
    		}
    
    	}
    
    	int qcheck (holdans)
    	{
    		int guess;
    		
    		scanf(" %d", &guess);
    		if (guess == holdans) 
    		{
    			printf("Correct");
    		}
    		else
    		{
    			printf("Wrong");
    		}
    	}
    
    
    	int qadd (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 + ran2;
    		return ans;
    	}
    
    	int qsubtract (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 - ran2;
    		return ans;
    	}
    
    	int qmultiply (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 * ran2;
    		return ans;
    	}
    	
    	int qdivide (ran1, ran2)
    	{
    
    		int ans;
    		ans = ran1 / ran2;
    		return ans;
    	}
    
    
    
    
    }
    If it is obvious, please give a clue.
    Also in the function qcheck you should make sure that you return somethng an integer value.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    All parameters should have a type, and main should return int.
    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.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by hellogamesmaste View Post
    Ok I've already spent hours agonising of why my program didn't work and it turned out Switch should have been switch, but the error message "should have ; before {" was not very helpful.

    If it is obvious, please give a clue.
    Be thankful the compiler gives you any explanation at all Quite often finding a mistake is a matter of inferring what the problem may be based on what error has been generated; C compilers AFAIK make at least two different kinds of passes thru the code, the second one (focussed on semantics) using information from the first one (focussed on syntax). They will spot different sorts of errors. This is why in a 100 line program, you will get thrown a syntax error on line 85 and not find out about a semantic error on line 22 until you fix line 85; the first pass fails.

    Your example is kind of like that -- it would be nice if the compiler would recognize "Switch" as wrong but what first happens is the syntax does not make sense to it.

    Getting use to deciphering error messages is just a matter of experience, but I promise it does get better, eg, stupid little things that waste hours will become stupid little things that waste 30 seconds.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    21
    Ah simple. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Question about OpenGL/Linux
    By Ideswa in forum Linux Programming
    Replies: 12
    Last Post: 09-10-2006, 05:56 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. undefined reference to function in class error
    By bladerunner627 in forum C++ Programming
    Replies: 12
    Last Post: 10-18-2005, 09:06 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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