Click to See Complete Forum and Search --> : Errors in code (was: does this make sense to you?)
aj2004
03-08-2004, 04:01 AM
okay, i wrote this extremely simple program:
#include <iostream>
#include <stdio.h>
int main()
{
int number;
cout << "enter a number: ";
cin >> number;
cout << "you entered: ";
return 0;
}
as you can see, it's very simple and basic. user enters number, program displays that number. but, i get the following errors:
--------------------------
~# gcc blah.cpp
blah.cpp: In function 'int main()':
blah.cpp:7: 'cout' undeclared (first use this function)
blah.cpp:7: (Each undeclared identifier is reported only once for each function it appears in.)
blah.cpp:8: 'cin' undeclared (first use this function)
--------------------------
as you can imagine, i feel very retarded... cout and cin are supposed to work as they are basic 'commands'.
deathadder
03-08-2004, 04:48 AM
Shouldnt it be #include <iostream.h> ?
aj2004
03-08-2004, 04:56 AM
now, i get this instead:
:~# gcc blah.cpp
In file included from /usr/include/c++/3.2.3/backward/iostream.h:31,
from blah.cpp:1:
/usr/include/c++/3.2.3/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 <sstream> instead of the deprecated header <strstream.h>. To disable this warning use
-Wno-deprecated.
/tmp/ccNpP5ib.o(.text+0x19): In function `main':
: undefined reference to `std::cout'
/tmp/ccNpP5ib.o(.text+0x1e): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccNpP5ib.o(.text+0x2d): In function `main':
: undefined reference to `std::cin'
/tmp/ccNpP5ib.o(.text+0x32): In function `main':
: undefined reference to `std::basic_istream<char, std::char_traints<char> >::operator>>(int&)'
/tmp/ccNpP5ib.o(.text+0x42): In function `main':
: undefined reference to `std::cout'
/tmp/ccNpP5ib.o(.text+0x47): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >&
std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccNpP5ib.o(.text+0x74): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccNpP5ib.o(.text+0xa3): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccNpP5ib.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
kinda weird. it makes the .exe file (a.out) fine if i remove cout and cin from the code, but that obviously removes almost all of the functionality of the program.
psi42
03-08-2004, 04:58 AM
Shouldn't you be using g++?
deathadder
03-08-2004, 05:00 AM
You can use gcc I believe, though I only use g++, but have to tried
gcc blah.cpp -Wno-deprecated
aj2004
03-08-2004, 05:18 AM
i feel less retarded now... it's nice that i can come to the JustLinux forums and get an answer to my problem without being laughed at...
the program works, now that i used g++.
myshkin
03-08-2004, 05:30 AM
*laughs at aj2004*
sorry couldnt resist. :p
dont worry, i know no better.
aj2004
03-08-2004, 06:13 AM
this has nothing to do with my programming problem, but i had to mention it. i just saw an advertisement at the top of this page saying that "in 4 out of 5 workload scenarios, Linux was 11%-20% more expensive than Windows." WOW, a windows installation with all the stuff i have right now would cost me several hundred dollars. but, i am running slack 9.1 for free (except for the cost of cd's). of course, i would never pay some outrageous price to install windows, and it's software, on my computer... in fact, i wouldn't pay at all... and i didn't. (for my mom's and bro's computers)
mindcooler
03-08-2004, 07:13 AM
Do NOT use iostream.h! That header has been deprecated for approximately six years now. The correct version of your program is:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
/* or just using namespace std; but that's bad practice
or qualify each symbol explicitly when they are used. */
int main()
{
int number;
cout << "enter a number: ";
cin >> number;
cout << "you entered: " << number << endl;
return 0;
}
Note that I have removed the header file you weren't using, and had you been using it you should include it's c++ counterpart, namely <cstdio>
Use g++ when building this program, not gcc.
bwkaz
03-08-2004, 08:33 PM
(Code snippet manually line-wrapped so the forum isn't widened nearly as much... ;))
Yes, you should use g++, but you can use gcc if you specify -lstdc++ manually (that's dash, ell, stdc++). At least in this case -- I'm not sure whether that works all the time or not.