having problem in compiling a simple c++ by gcc
Hi,
I have been away of c++ for 3 years but now I have to work on c++ again. To refresh my mind I had a look at my old, simple c++ codes which were written for DOS compiler. As the result I am not able to compile those codes under linux so I removed <conio.h> and <stdlib.h> and used <iostream> instead of them. My code looks like this: Code:
#include <iostream> Code:
g++ program.cpp I should be thankful if anyone tell me what's wrong with my new codes. |
You need to tell us what the errors are. One thing is that main()'s return type should be "int", rather than "void". You probably have errors saying that cout, cin and endl are undeclared. You can:
1. Put "using namespace std;" under your include directive, or 2. put 3 lines: "using std::cout;", "using std::cin;" and "using std::endl;" under your include directive, or 3. change "cout", "cin" and "endl" everywhere to "std::cout", "std::cin" and "std::endl" respectively. |
Well without the error message, I doubt anyone can help you fix the problem. I think you should also use the std namespace in your code e.g.
Code:
#include <iostream> |
Here is the error after adding "using namespace std" (the real error is much longer though):
In file included from /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31, from lab5.cpp:1: /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. lab5.cpp:19: error: ‘::main’ must return ‘int’ lab5.cpp: In function ‘int main()’: lab5.cpp:25: error: no match for ‘operator>>’ in ‘std::cin >> & n’ /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:131: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:137: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:169: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:172: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:175: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>] /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/istream:178: note: std::basic_istream<_CharT, _Traits>& |
Quote:
Edit: the above code compiles fine for me, but I'm using an older version of gcc (3.4.6). Also, change main's return type to int. |
Thank you. Everything works fine by removing & after "cin >>" and changing void main to int main, although in DOS compiler this program runs properly when it s void main.
Anyhow, DOS is dead and linux is the best platform for compiling c++ codes, so I have to become accustomed to its special tricks. |
All times are GMT -5. The time now is 12:47 AM. |