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(&v);
cout << v << " negated is " << result;
cout << "\n";
system("PAUSE");
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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;
}
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.cpp
negate' 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.cpp
int negate(const int*)' used prior to declarationC:\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
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
Yes this did compile differently with the desired result. Thankyou for the tip.
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!
That was going to be my next question to the OP - "Why does this work when I changed the function
name?"
:)
Wayne
I don't know why but it did work.
Charles Leng
... because "negate" is defined in namespace std::