Author Topic: basic C++ question  (Read 1557 times)

slvadcjelli42

  • Member
  • **
  • Posts: 155
  • Kudos: 0
basic C++ question
« on: 27 September 2003, 22:16 »
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

  • VIP
  • Member
  • ***
  • Posts: 1,258
  • Kudos: 41
basic C++ question
« Reply #1 on: 27 September 2003, 22:47 »
What version of gcc are you using? for something that simple this is what i'd usually do:

Code: [Select]

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

slvadcjelli42

  • Member
  • **
  • Posts: 155
  • Kudos: 0
basic C++ question
« Reply #2 on: 27 September 2003, 23:10 »
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

  • VIP
  • Member
  • ***
  • Posts: 1,258
  • Kudos: 41
basic C++ question
« Reply #3 on: 27 September 2003, 23:16 »
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

  • Member
  • **
  • Posts: 155
  • Kudos: 0
basic C++ question
« Reply #4 on: 27 September 2003, 23:59 »
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.

slvadcjelli42

  • Member
  • **
  • Posts: 155
  • Kudos: 0
basic C++ question
« Reply #5 on: 28 September 2003, 21:33 »
OK, I've discovered more and also kicked myself for not noticing the thread below mine with the EXACT (well, pretty close) same problem as me. I'll move into that.

MikeU

  • Newbie
  • *
  • Posts: 15
  • Kudos: 0
basic C++ question
« Reply #6 on: 1 October 2003, 07:56 »
You should use g++ rather than gcc to compile C++ code. I believe g++ will be installed if gcc is installed.

g++ source.cpp

SAJChurchey

  • Member
  • **
  • Posts: 246
  • Kudos: 0
    • http://sajchurchey.htmlplanet.com
basic C++ question
« Reply #7 on: 2 October 2003, 01:37 »
actually, with newer versions of C++, you have to use the namespace feature of C++ like this.

Code: [Select]

It should work fine after u add that namespace and use g++.

Study up on namespaces.
SAJChurchey                    

Maniaman

  • Member
  • **
  • Posts: 188
  • Kudos: 0
basic C++ question
« Reply #8 on: 2 October 2003, 02:51 »
I had the same problem.

Code: [Select]

That fixed it for me, should work for you.
I use my hotmail email on here to keep from begin spammed by trolls. If you want my good email address for something then ask... :)


slvadcjelli42

  • Member
  • **
  • Posts: 155
  • Kudos: 0
basic C++ question
« Reply #9 on: 2 October 2003, 03:49 »
lol thanks for your help, although it's been pretty much fixed (in that other thread)... using namepace std made no difference either way, it turns out, g++ / c++ would compile it and gcc would not. Thanks anyway though, and I will still read up on namespaces.

xyle_one

  • VIP
  • Member
  • ***
  • Posts: 2,213
  • Kudos: 135
basic C++ question
« Reply #10 on: 30 October 2003, 04:21 »
itm