Menu

Small piece of code compilation problem

2008-07-13
2012-09-26
  • Charles  Leng

    Charles Leng - 2008-07-13

    I have been having a problem compiling a short simple piece of code. The code is not my own but taken from 'C++: A Beginner's Guide' by Herbert Schildt 2nd Edition, Chapter 7 Listing 2 p245. I have just added a few lines necessary to see the program output in a console window. The listing is as follows and is identical to the code presented in the book bar the few minor alterations. I don't think the alterations would be the cause of the problem at all. Here is the code in question:

    include <cstdlib>

    include <iostream>

    using namespace std;

    int negate(const int *val);

    int main(int argc, char *argv[])
    {
    int result;
    int v = 10;

    result = negate(&amp;v);
    
    cout &lt;&lt; v &lt;&lt; &quot; negated is &quot; &lt;&lt; result; 
    cout &lt;&lt; &quot;\n&quot;;
    
    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
    

    }

    int negate(const int val)
    {
    return -
    val;
    }

    // End of code

    Here is the compiler output:

    Line File Message
    C:\Dev-Cpp\Templates\Listing_2.cpp In function int main(int, char**)': 13 C:\Dev-Cpp\Templates\Listing_2.cppnegate' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)
    C:\Dev-Cpp\Templates\Listing_2.cpp In function int negate(const int*)': 23 C:\Dev-Cpp\Templates\Listing_2.cppint negate(const int*)' used prior to declaration
    C:\Dev-Cpp\Makefile.win [Build Error] [Templates/Listing_2.o] Error 1

    I don't know what is the cause of this. I would have thought such simple example code as this would compile with a standard C++ compiler. I am using Dev-C++ 4.9.9.2 on Windows Xp Professional SP2. I also encountered the same problem when using Dev-C++ 4.9.9.2 on Windows 98 SE. Perhaps others can copy and paste this sample code into their Dev-C++ IDE and try and compile to see if they get the same result. I would be most interested to know what happens.

    Thanks in advance

     
    • Wayne Keen

      Wayne Keen - 2008-07-13

      Does this compile any different for you?

      include <cstdlib>

      include <iostream>

      using namespace std;

      int my_negate(const int *val);

      int main(int argc, char *argv[])
      {
      int result;
      int v = 10;

      result = my_negate(&v);

      cout << v << " negated is " << result;
      cout << "\n";

      system("PAUSE");
      return EXIT_SUCCESS;
      }

      int my_negate(const int val)
      {
      return -
      val;
      }

      // End of code

      It compiles and runs for me under MinGW/MSYS (GCC-4.2.1) - I don't have Dev on this machine

      Wayne

       
      • Charles  Leng

        Charles Leng - 2008-07-16

        Yes this did compile differently with the desired result. Thankyou for the tip.

         
    • cpns

      cpns - 2008-07-14

      I am willing to bet that negate is defined somewhere in the std:: namespace.

      There's a reason for namespaces, and "using namespace std ;" undoes all that it attempts to solve!

       
      • Wayne Keen

        Wayne Keen - 2008-07-14

        That was going to be my next question to the OP - "Why does this work when I changed the function
        name?"

        :)

        Wayne

         
        • Charles  Leng

          Charles Leng - 2008-07-16

          I don't know why but it did work.

          Charles Leng

           
          • cpns

            cpns - 2008-07-16

            ... because "negate" is defined in namespace std::

             

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.