Miscellaneous > Programming & Networking

basic C++ question

(1/3) > >>

slvadcjelli42:
I have been learning C++ for a little while now but have only used any of it on Windows. I wanted to try it out on Linux, so I made the simplest thing I possibly could:

#include <iostream.h>

int main()
{
cout << "Hello\n";
return(0);
}

And tried "gcc test2.cpp" to compile it, test2.cpp of course being the filename. Well, this is what it said:

In file included from /usr/include/c++/3.2/backward/iostream.h:31,
                 from test2.cpp:1:
/usr/include/c++/3.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 <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
test2.cpp:7:2: warning: no newline at end of file
/tmp/ccA1TBCY.o(.text+0x19): In function `main':
: undefined reference to `std::cout'
/tmp/ccA1TBCY.o(.text+0x1e): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:  :o  perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccA1TBCY.o(.text+0x4a): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccA1TBCY.o(.text+0x79): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccA1TBCY.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

I then searched for iostream* and found that there is iostream and iostream.h. So I then tried

#include </usr/include/c++/3.2/iostream>

int main()
{
cout << "Hello\n";
return(0);
}

Then it said

test2.cpp: In function `int main()':
test2.cpp:5: `cout' undeclared (first use this function)
test2.cpp:5: (Each undeclared identifier is reported only once for each
   function it appears in.)

And that just kinda stumped me, so I did some reading and changed it to:

#include <istream>

int main()
{
   printf("Hello\n");
   return(0);
}


Which worked. My question is, why isn't iostream working right for me? Is the code really that different between Windows and Linux, and what I'm trying to do just doesn't make any sense (I sure hope not)? I also remember reading that printf is outdated, what's up with that? Any ideas will be appreciated.

(Edit: 1) disabled smilies. 2) fixed a problem with the filenames to reduce confusion. 3) that wasn't the best choice for a thread title, was it?)

[ September 27, 2003: Message edited by: Dirk Gently ]

Stryker:
What version of gcc are you using? for something that simple this is what i'd usually do:


--- Code: ---
--- End code ---

should work flawlessly, but if it doesn't... what version of gcc?

slvadcjelli42:
Oh, yeah, sorry, that should have been an obvious part to have. Here.

gcc --version
gcc (GCC) 3.2.3 20030415 (Debian prerelease)
Copyright (C) 2002 Free Software Foundation, Inc.

I'm glad that it "should" work, at least, although it doesn't (that specific piece gives me the same errors as before). "Prerelease"... could that be a problem? Or do you think there could be something wrong with the iostream.h file I have?

P.S. next time I'll use the "CODE" option for code.

Stryker:
i can't find anything about why iostream wouldn't work, perhaps it's corrupted... try reinstalling just the iostream.h file, if that doesn't work i'd reinstall gcc, and if that still doesn't work... i'd get different version.

i dont have any problems with printf, i love it... much quicker and easier to format stuff for me. not to mention i dont have to have << everywhere. but, cout should still work   :(  .

[ September 27, 2003: Message edited by: Stryker ]

slvadcjelli42:
Yeah, I messed up with that post above. Anyway, I tried using KDevelop for some stuff, and "cout" worked there but only under certain conditions. I guess I can figure this out now that I have that to work with, thanks. I think reinstalling iostream.h probably isn't necessary since it seems to work sometimes (I'm a little confused about that, I'll have to look at what KDevelop is doing), but who knows. So thanks again, I'll see what I can do.

Navigation

[0] Message Index

[#] Next page

Go to full version