Newbie with basic c++ question

J

jamlen

Trying to teach myself c++. Using Visual Studio.Net I don't have any
compiler issues, but using Borland TCL or DJGPP, or GPP on my Linux
machine, I can't compile anything.

//hello.cpp
#include <iostream.h>

// main: generate some simple output

void main ()
{
cout << "Hello, world." << endl;
}


Using DJGPP, gpp hello.cpp doesn't like the iostream.h.
If I change it to <iostream>, then the compiler returns:

hello.cpp:6: error: 'main' must return 'int'
hello.cpp: In function 'int main(...)':
hello.cpp:7: error: 'cout' undeclared (first use this function)
hello.cpp:7: error: 'endl' undeclared (first use this function)

If I change line 6 to "int main()", I still get the errors with "cout"
and "endl" being undeclared.
I get the "cout" problem on my Linux machine as well. I've tried
changing the "cout" to "stdout", but that doesn't work either.

Since every initial program I've found examples of are variations of
this, I figure that I must be missing something obvious.

Any ideas of what I'm doing wrong, or what I'm not seeing?
 
W

WW

jamlen said:
//hello.cpp
#include <iostream.h>

There is no iostream.h header in standard C++. Use <iostream>.

#include said:
void main ()

The main function *must* return int.

void main ()
{
cout << "Hello, world." << endl;

cout and endl are in the std namespace:

std::cout << "Hello, world." << std::endl;
Any ideas of what I'm doing wrong, or what I'm not seeing?

You have a crap textbook. Get Accelerated C++ from Koenig&Moo, or Essential
C++, or The C++ Programming Language Special Edition from Stroustrup, or
Francis Glassborow's new book if you have no pogramming experiences (does
not seem so). But forget that book (or teacher) you were using. It will
cause you more trouble later than you can imagine.
 
G

Gianni Mariani

jamlen said:
Trying to teach myself c++. Using Visual Studio.Net I don't have any
compiler issues, but using Borland TCL or DJGPP, or GPP on my Linux
machine, I can't compile anything.

//hello.cpp
#include <iostream.h>
nuke the .h

and use namespace std

either "using namespace std;

or scope the cout and endl

like:

std::cout
// main: generate some simple output

void main ()
{
cout << "Hello, world." << endl;

loose the "," :)
}


Using DJGPP, gpp hello.cpp doesn't like the iostream.h.
If I change it to <iostream>, then the compiler returns:

hello.cpp:6: error: 'main' must return 'int'
hello.cpp: In function 'int main(...)':
hello.cpp:7: error: 'cout' undeclared (first use this function)
hello.cpp:7: error: 'endl' undeclared (first use this function)

If I change line 6 to "int main()", I still get the errors with "cout"
and "endl" being undeclared.
I get the "cout" problem on my Linux machine as well. I've tried
changing the "cout" to "stdout", but that doesn't work either.

Since every initial program I've found examples of are variations of
this, I figure that I must be missing something obvious.

Any ideas of what I'm doing wrong, or what I'm not seeing?

You've got an old book - pre the standard.

this should work ...

#include <iostream>

// main: generate some simple output

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

Kevin Goodsell

jamlen said:
Trying to teach myself c++. Using Visual Studio.Net I don't have any
compiler issues, but using Borland TCL or DJGPP, or GPP on my Linux
machine, I can't compile anything.

First you need to be aware that C++ is not "whatever your compiler
accepts". Most compilers accept a lot of junk that is *not* C++ (but
different compilers accept different junk). Your code has a number of
examples.
//hello.cpp
#include <iostream.h>

// main: generate some simple output

void main ()
{
cout << "Hello, world." << endl;
}


Using DJGPP, gpp hello.cpp doesn't like the iostream.h.

iostream.h is not part of C++.
If I change it to <iostream>, then the compiler returns:

hello.cpp:6: error: 'main' must return 'int'

A very accurate error message, describing an error in your code.
hello.cpp: In function 'int main(...)':
hello.cpp:7: error: 'cout' undeclared (first use this function)
hello.cpp:7: error: 'endl' undeclared (first use this function)

Also accurate messages. The objects in question are called std::cout and
std::endl. Use those names (along with the correct header and return
type for main) and your program will be correct.

Read up on namespaces. You can't use the C++ standard library without them.
If I change line 6 to "int main()", I still get the errors with "cout"
and "endl" being undeclared.
I get the "cout" problem on my Linux machine as well. I've tried
changing the "cout" to "stdout", but that doesn't work either.

That's nonsense. stdout is a completely different kind of object.
Since every initial program I've found examples of are variations of
this, I figure that I must be missing something obvious.

Mainly that compilers accept things that are not C++, but different
compilers allow different kinds of non-C++. If you stick to standard
C++, all C++ compilers will accept the code.

-Kevin
 
W

WW

Kevin said:
First you need to be aware that C++ is not "whatever your compiler
accepts". Most compilers accept a lot of junk that is *not* C++ (but
different compilers accept different junk). Your code has a number of
examples.

The best I loved when one of them accepted (bug, was fixed in the 6.2)
passing a class-type instead of a pointer. The class type had absolutely no
conversion functions. :)
 
D

David White

Gianni Mariani said:
loose the "," :)

Actually, I believe that "Hello, world." is the grammatical form, even if
it's not the traditional C & C++ first-program output :)

DW
 
G

Gianni Mariani

David said:
Actually, I believe that "Hello, world." is the grammatical form, even if
it's not the traditional C & C++ first-program output :)

Don't tell me you've got a ISO standard :->
 
K

Kwan Ting

"If you stick to standard C++, all C++ compilers will accept the code."

Let me write an example involving the use of export keyword or something
like that and try and compile it under all the popular compiler and see how
many of them accepts it ;-)
 
K

Kevin Goodsell

Kwan said:
"If you stick to standard C++, all C++ compilers will accept the code."

Let me write an example involving the use of export keyword or something
like that and try and compile it under all the popular compiler and see how
many of them accepts it ;-)

The "you" in my message referred to the OP. I'm quite confident that he
doesn't know about "export", or those other yet-to-be-implemented (in
most compilers) features. ;)

....OK, point taken. "If you stick to standard C++, all conforming C++
implementations will accept the code. But there aren't any of those
(except maybe Comeau) yet." Doesn't sound so great, does it? :-/

-Kevin
 
D

David White

Kwan Ting said:
"If you stick to standard C++, all C++ compilers will accept the code."

Let me write an example involving the use of export keyword or something
like that and try and compile it under all the popular compiler and see how
many of them accepts it ;-)

"All C++ compilers" includes _unpopular_ compilers, as well as C++ compilers
that don't yet exist. Even if every existing compiler implemented the same
non-standard extension, it would still not be standard and might not compile
on all existing C++ compilers tomorrow.

DW
 
K

Keroppi

"Kevin Goodsell"
...OK, point taken. "If you stick to standard C++, all conforming C++
implementations will accept the code. But there aren't any of those
(except maybe Comeau) yet." Doesn't sound so great, does it? :-/

No, not really does it :-S

Mind you, having only being half serious when I posted that just remember
the case of for loop scope under MSVC++ :-o
(No I'm not starting on VC++ being bad etc, bearing in mind that it's the
main compiler I am using...)
 
K

Kevin Goodsell

Keroppi said:
"Kevin Goodsell"




No, not really does it :-S

Mind you, having only being half serious when I posted that just remember
the case of for loop scope under MSVC++ :-o
(No I'm not starting on VC++ being bad etc, bearing in mind that it's the
main compiler I am using...)

My understanding is that version 7 & up get that right (but you have to
set an option, I think - the default is some kind of "compatibility
mode" that accepts code that uses either the old or the new rule).

-Kevin
 
J

jamlen

Thanks, this helped quite a bit. I've been using two books and
viewing quite a few websites, but they all seemed to give similar
examples and none used "std::cout".
I'll look into some of the books that you mentioned.
 
J

jamlen

Thanks for the advice. The whole reason I was trying different
compilers was to develop "good" code habits, not just use a forgiving
compiler.
I'll read up on namespaces and the standard library.
 
S

Stuart Golodetz

Keroppi said:
"Kevin Goodsell"


No, not really does it :-S

Mind you, having only being half serious when I posted that just remember
the case of for loop scope under MSVC++ :-o

FWIW, putting this at the top of every translation unit (annoying, I know,
but better than nothing) will fix the scoping issue:

#if defined(_MSC_VER) && _MSC_VER <= 1200
#define for if(0); else for
#endif

HTH,

Stuart.
 
A

Arquebus257WeaMag

Any ideas of what I'm doing wrong, or what I'm not seeing?
You've got an old book - pre the standard.

this should work ...

#include <iostream>

// main: generate some simple output

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

Well Ive tried running the "Hello World" program (identical to the
above) from Bruce Eckel's book "Thinking in C++ on Dev-C++ and Koenig
and Moo's book Accelerated C++ on Borland C++Builder6, and in both
cases the command window just flashes and you cant see the results of
the program. Ive tried using the #include <conio> heading with
getch(); placed before the return main statement, but that doesnt
always work. What do I do?
 
W

WW

Arquebus257WeaMag said:
Well Ive tried running the "Hello World" program (identical to the
above) from Bruce Eckel's book "Thinking in C++ on Dev-C++ and Koenig
and Moo's book Accelerated C++ on Borland C++Builder6, and in both
cases the command window just flashes and you cant see the results of
the program. Ive tried using the #include <conio> heading with
getch(); placed before the return main statement, but that doesnt
always work. What do I do?

OFF-TOPIC, ONLY WINDOWS (or compatible)

A.) Start it from a console window

B.) Do this:

#include <iostream>
#include <cstdlib>

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,823
Latest member
Nadia88

Latest Threads

Top