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

    error: undefined reference to 'printSpecChar(int)

    I just purchased a laptop with Windows 8 and installed the portable Dev-C++ because the other one wasnt compatible with Win8.

    Now im getting an error when i compile that says:
    'undefined reference to 'printSpecChar(int)

    Let me also explain that when i was using Dev-C++ on Win7, i never had to use #include <stdlib.h> for system ("pause") but now I do.
    So these are two new things im encountering for the first time.

    Is there a setting that I need to change, or am I just missing something.....again?



    Code:
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    int getNumber(int, int);
    void printSpecChar(int);
    void pause (double);
    
    int main()
    {	
        int n;
        n = getNumber(1, 100);
        printSpecChar(n);
        return 0;
    }
    
    int getNumber 1, 100)
    	{
        		int num = 0;
                do
    	         {
    	             cout << "Enter a number between " << min
    		          << " and " << max << ": ";
    	             cin >> num; cin.clear(); cin.ignore(10, '\n';
    	         }
    	             while (num < min || num > max);
    	             cout << endl << endl;
    	             return num;
            }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: error: undefined reference to 'printSpecChar(int)

    Quote Originally Posted by psfign
    Now im getting an error when i compile that says:
    'undefined reference to 'printSpecChar(int)
    It looks like you are missing the definition of printSpecChar. Your code should not compile (and thus not link) on any C++ compiler worth its source. You seem to also be missing the definition of pause, but then you don't call pause in the code that you showed us.

    Quote Originally Posted by psfign
    Let me also explain that when i was using Dev-C++ on Win7, i never had to use #include <stdlib.h> for system ("pause") but now I do.
    The C++ header is actually <cstdlib>, but anyway, the fact that you did not include the correct header is a bug in your program. It so happens that your new installation of your IDE is such that the bug was exposed.

    Quote Originally Posted by psfign
    Is there a setting that I need to change, or am I just missing something.....again?
    You need to fix your code.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2013
    Posts
    71

    Re: error: undefined reference to 'printSpecChar(int)

    ok, i'll work on those, but why does stdlib.h work still if it's supposed to be cstdlib? And why didnt i have to use that on win7?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: error: undefined reference to 'printSpecChar(int)

    Quote Originally Posted by psfign
    why does stdlib.h work still if it's supposed to be cstdlib?
    A substantial part of the C standard library is included in the C++ standard library under the C standard-style headers. There are differences, however, e.g., the names are available in the global namespace even without a using directive.

    Quote Originally Posted by psfign
    And why didnt i have to use that on win7?
    The header could have been indirectly included by another one.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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

    Re: error: undefined reference to 'printSpecChar(int)

    Quote Originally Posted by psfign View Post
    ok, i'll work on those, but why does stdlib.h work still if it's supposed to be cstdlib? And why didnt i have to use that on win7?
    C++ knows nothing about the OS you're running on. All that matters is that the language adheres to the ANSI/ISO specification.

    <stdlib.h> and <cstdlib> are interchangeable. Whatever else is an installation/include path/settings issue which has nothing to do with the C++ language.

    Last, the compiler that you're using is not Dev-C++. The Dev-C++ is an Integrated Development Environment (IDE). Dev-C++ doesn't know how to compile a C++ file. All it does is under the hood, use the gcc C++ compiler. So compiler issues are with the version of gcc you're using.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jan 2013
    Posts
    71

    Re: error: undefined reference to 'printSpecChar(int)

    oh ok and i did have to upgrade bc g++ kept crashing.

    but i dont understand the void functions. can someone explain how to define them? do I place a value in the ( )?

    void printSpecChar(int); \\ do i change the int?
    printSpecChar(n); \\ do i change the n?

    and what goes in there? i dont mean tell me what to put per se, but how i determine WHAT to put there? we just learned void functions, and i have yet to successfully code one. prof gave this as an example lol and i cant even compile the example.
    Last edited by psfign; April 2nd, 2013 at 01:39 PM.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: error: undefined reference to 'printSpecChar(int)

    There are 3 things to writing and using a function (simplified).

    The first is the function declaration. These appear at the top of the program before main(..) and usually before any other function definiton. These inform the compiler regarding the name of the function, the type of its arguments and the type of its return. Function declarations have no body and the statement is terminated by a ;.

    Code:
    int getNumber(int, int);
    void printSpecChar(int);
    These are examples of function declarations. The first takes 2 argumnets of type integer and returns a value of type int. The second takes one argument of type int and does not return a value (void).

    The second is the function definition. This defines the actions of the function and provides the return value (if not void).

    Code:
    int getNumber(int min, int max)
    {
    int num = 0;
    
         do {
    	 cout << "Enter a number between " << min << " and " << max << ": ";
             cin >> num;
             cin.clear();
             cin.ignore(100, '\n');
         } while (num < min || num > max);
    
          cout << endl;
          return num;
    }
    This is the function definition for getNumber. It takes two int arguments called min and max and returns an int (num).

    The third is using the function. An example of using GetNumber is

    Code:
    int main()
    {	
    int n;
    
        n = getNumber(1, 100);
        cout << "The number entered is " << n << endl;
        return 0;
    }
    So from your code
    Code:
    void printSpecChar(int);
    This is a function declaration taking one int argument and does not return a value (ie a void function).

    The definition would be

    Code:
    void printSpecChar(int num)
    {
       ..........
       return;
    }
    note that as the function does not return a value, the return statement doesn't specify a value as none is being returned.

    An example of its use would be

    Code:
    printSpecChar(n);
    So in your code, you need to provide the statements for the body of the function printSpecChar(..) so that it does whatever its specification states.

    Please also see
    http://www.learncpp.com/cpp-tutorial...-at-functions/

    I hope this makes things a little clearer for you regading functions.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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