C++ woes

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C++ woes

Postby leke » Fri Mar 19, 2010 10:59 am

I'm finally giving C++ a whirl, but I'm having some problems.
How come the following return compile errors on Linux?
Code: Select all
// How to think like a computer scientist C++ Version
#include <iostream.h>
// main: generate some simple output
void main ()
{
  cout << "Hello, world." << endl;
  return 0
}


// Sams Teach Yourself C++ in 1 Hour a Day 6th Ed (from 2008)
#include <iostream>
int main()
{
  std::cout << “Hello World!\n”;
  return 0;
}

I know (thanks to youtube) how output Hello World without problems, but I was just wondering why the above fail?...and do you guys have any recommendations for a thorough C++ learning source for Linux?

Thanks.
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby Ram » Fri Mar 19, 2010 11:11 am

At a guess, I'd say the first one void main does not require the return 0 line.

Think there was a change/update to the iso std a good few years back.

I have a couple of sams books that are about 10 yrs apart if not more by the same author. The code is different, if memory serves me right for the Hello World program.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby M-Saunders » Fri Mar 19, 2010 11:29 am

leke, you need to state the actual compile errors! Otherwise it's all just guesswork! :-)

At a glance I see you're missing a semicolon after 'return 0' in main()

M
User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Postby leke » Fri Mar 19, 2010 12:42 pm

M-Saunders wrote:leke, you need to state the actual compile errors! Otherwise it's all just guesswork! :-)

At a glance I see you're missing a semicolon after 'return 0' in main()

M
You're right, what was I thinking?
Here is what I get back from the C++ Eclipse IDE...
Code: Select all
#include <iostream.h>
// main: generate some simple output
void main ()
{
  cout << "Hello, world." << endl;
  return 0
}

**** Build of configuration Debug for project TLCS ****

make all
Building file: ../TLCS.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"TLCS.d" -MT"TLCS.d" -o"TLCS.o" "../TLCS.cpp"
../TLCS.cpp:1:22: warning: iostream.h: No such file or directory
../TLCS.cpp:3: error: ‘::main’ must return ‘int’
../TLCS.cpp: In function ‘int main()’:
../TLCS.cpp:5: error: ‘cout’ was not declared in this scope
../TLCS.cpp:5: error: ‘endl’ was not declared in this scope
../TLCS.cpp:7: error: expected ‘;’ before ‘}’ token
make: *** [TLCS.o] Error 1
and
Code: Select all
#include <iostream>
int main()
{
  std::cout << “Hello World!\n”;
  return 0;
}

**** Build of configuration Debug for project TLCS ****

make all
Building file: ../TLCS.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"TLCS.d" -MT"TLCS.d" -o"TLCS.o" "../TLCS.cpp"
../TLCS.cpp:4: error: stray ‘\342’ in program
../TLCS.cpp:4: error: stray ‘\200’ in program
../TLCS.cpp:4: error: stray ‘\234’ in program
../TLCS.cpp:4: error: stray ‘\’ in program
../TLCS.cpp:4: error: stray ‘\342’ in program
../TLCS.cpp:4: error: stray ‘\200’ in program
../TLCS.cpp:4: error: stray ‘\235’ in program
../TLCS.cpp: In function ‘int main()’:
../TLCS.cpp:4: error: ‘Hello’ was not declared in this scope
../TLCS.cpp:4: error: expected `;' before ‘World’
make: *** [TLCS.o] Error 1

Finally, here is what I've been using without problems...
Code: Select all
#include <iostream>
int main ()
{
using namespace std;
cout << "Hello, world." << endl;
return 0;
}

So the ISO make some changes and
iostream.h becomes iostream
std:: becomes using namespace std;
void main becomes int main
etc...?
If so, does this continue to happen a lot more?
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby Ram » Fri Mar 19, 2010 1:23 pm

In your first program.

It appears the complier can't find iostream.h - is it present on your system - eclipse may need pointing to it.

Also as Mike said your missing a ; at the end of the return 0 line.


In your second program.

What quotes are you using round Hello World - should be " I believe.

Your third program. try like this

Code: Select all
 #include <iostream>

 using namespace std;

 int main()
 {
   cout << "Hello, World!" << endl;
   return 0;
 }


As I said think there was a change/update to the iso std.

C++ was something I started to learn or at least read upon way back in about 94/95, then real life took over, another child, change of job from steel worker to IT, I ended up with no time to actually learn it. what I can say is that similar programs like Hello World are wrote with slight diffences now to back then.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby leke » Fri Mar 19, 2010 5:02 pm

well it's a relief to hear that this type of thing isn't experienced generally. I also didn't realise there is a difference between iostream and iostream.h (headers I think they're called). Eclipse can find iostream without problems.

The 3rd code example works fine. I just threw it in there to show what was working. I'll try to see if I can make eclipse find iostream.h then bash along with it.
User avatar
leke
LXF regular
 
Posts: 506
Joined: Mon Oct 22, 2007 5:45 pm
Location: Oulu, Finland

Postby Ram » Fri Mar 19, 2010 5:52 pm

I know the 3rd programe worked, just that putting the using statement after the #includes is better than having it in your main function.

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
User avatar
Ram
LXF regular
 
Posts: 1676
Joined: Thu Apr 07, 2005 9:44 pm
Location: Guisborough

Postby Bazza » Fri Mar 19, 2010 6:25 pm

Hi leke...

I`m pretty sure there is no such animal as "iostream.h" these
days...

It should be:-

/* The include file which is the library itself of */
/* which "iostream" as a (sub)class too. */
#include <iostream>

/* And this line with a semi-colon... */
using namespace std;

/* Continue with your code... */

But I could be wrong...

I havent coded in C(++) for ages...
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1476
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby shinso » Fri Mar 19, 2010 7:37 pm

The first program also doesn't refer to the std namespace. Shouldn't it be either:

Code: Select all
#include <iostream>

using namespace std;

int main()
{
  cout << "Hello, world." << endl;
  return 0;
}


Or:

Code: Select all
#include <iostream>

int main()
{
  std::cout << "Hello, world." << std::endl;
  return 0;
}


Edit - Forgot the semicolons, thanks Bazza!
Last edited by shinso on Sat Mar 20, 2010 4:03 pm, edited 1 time in total.
shinso
 
Posts: 13
Joined: Wed Mar 10, 2010 9:47 pm
Location: UK

Postby Bazza » Fri Mar 19, 2010 8:57 pm

Hi shinso...

Take a look at both your code segments again... ;o)

Neither will compile without a semi-colon after _return 0_";"

;)
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1476
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby shinso » Sat Mar 20, 2010 4:03 pm

Edited, thanks Bazza :oops:

That serves me right for being lazy and just copy and pasting the code from the first post :roll:
shinso
 
Posts: 13
Joined: Wed Mar 10, 2010 9:47 pm
Location: UK

Postby graemef » Wed Apr 07, 2010 9:16 am

Strictly speaking your code should be:

Code: Select all
std::cout << "hello world" << std::endl;


This is because cout and endl (which are defined in iostream) are defined within the std namespace. This will allow you to define your own cout variable in a different namespace. However for something as fundamental as cout that would be confusing, so many programs and tutorials introduce using namespace short cut.

Code: Select all
using namespace std;


This allows you to use all the variables and method defined in the std namespace without having to explicitly state the namespace, thus you can use cout rather than std::cout.

The iostream.h header file was provided for backwards compatibility for those days when C++ did make use of the std namespace, this header file essentially has a using namespace within it. The older header files are not bundled by default on all systems these days so if you do need it you may have to install it.
graemef
 
Posts: 8
Joined: Wed Apr 07, 2010 8:55 am


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron