Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ woes

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
leke
LXF regular


Joined: Mon Oct 22, 2007 6:45 pm
Posts: 503
Location: Oulu, Finland

PostPosted: Fri Mar 19, 2010 10:59 am    Post subject: C++ woes Reply with quote

I'm finally giving C++ a whirl, but I'm having some problems.
How come the following return compile errors on Linux?
Code:

// 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.
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1671
Location: Guisborough

PostPosted: Fri Mar 19, 2010 11:11 am    Post subject: Reply with quote

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.....
Back to top
View user's profile Send private message MSN Messenger
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Fri Mar 19, 2010 11:29 am    Post subject: Reply with quote

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

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

M
Back to top
View user's profile Send private message Visit poster's website
leke
LXF regular


Joined: Mon Oct 22, 2007 6:45 pm
Posts: 503
Location: Oulu, Finland

PostPosted: Fri Mar 19, 2010 12:42 pm    Post subject: Reply with quote

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

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:
#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:

#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:

#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?
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1671
Location: Guisborough

PostPosted: Fri Mar 19, 2010 1:23 pm    Post subject: Reply with quote

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:

 #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.....
Back to top
View user's profile Send private message MSN Messenger
leke
LXF regular


Joined: Mon Oct 22, 2007 6:45 pm
Posts: 503
Location: Oulu, Finland

PostPosted: Fri Mar 19, 2010 5:02 pm    Post subject: Reply with quote

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.
_________________
http://truecenterpublishing.com/zenstory/maybe.html
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1671
Location: Guisborough

PostPosted: Fri Mar 19, 2010 5:52 pm    Post subject: Reply with quote

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.....
Back to top
View user's profile Send private message MSN Messenger
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1474
Location: Loughborough

PostPosted: Fri Mar 19, 2010 6:25 pm    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
shinso



Joined: Wed Mar 10, 2010 9:47 pm
Posts: 13
Location: UK

PostPosted: Fri Mar 19, 2010 7:37 pm    Post subject: Reply with quote

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

Code:

#include <iostream>

using namespace std;

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


Or:

Code:

#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
Back to top
View user's profile Send private message
Bazza
LXF regular


Joined: Sat Mar 21, 2009 11:16 am
Posts: 1474
Location: Loughborough

PostPosted: Fri Mar 19, 2010 8:57 pm    Post subject: Reply with quote

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...
Back to top
View user's profile Send private message
shinso



Joined: Wed Mar 10, 2010 9:47 pm
Posts: 13
Location: UK

PostPosted: Sat Mar 20, 2010 4:03 pm    Post subject: Reply with quote

Edited, thanks Bazza Embarassed

That serves me right for being lazy and just copy and pasting the code from the first post Rolling Eyes
Back to top
View user's profile Send private message
graemef



Joined: Wed Apr 07, 2010 9:55 am
Posts: 8

PostPosted: Wed Apr 07, 2010 10:16 am    Post subject: Reply with quote

Strictly speaking your code should be:

Code:
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:
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.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast