GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Nov-2006, 00:59
littlewenwen littlewenwen is offline
New Member
 
Join Date: Nov 2006
Posts: 5
littlewenwen is on a distinguished road

C++ beginner


Dear All

I am a beginner to learn C++. I am following an on-line tutorial. When I try the first C++ program as described in the tutorial (which is supposed to work without any problem, as it is said in the tutorial), I got many error message. The folllowing is the program (I name it as test.cpp:



CPP / C++ / C Code:
#include <iostream.h>


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



I am using Suse 10.0 linux and the gcc compiler installed by Suse 10.0. After I gcc the programm above ( gcc test.cpp ) I got the error message. When I try to correct the error, and re-compile it, I got other error message!

Can some one point out what mistake I made? Or can you kindly supply me a workable, small program that will run under gcc compiler? I just want to get a rough idea about how C++ works.
Last edited by admin II : 11-Nov-2006 at 04:17. Reason: fixed broken [cpp] ... [/cpp] tag
  #2  
Old 11-Nov-2006, 01:39
alcoholic's Avatar
alcoholic alcoholic is offline
Member
 
Join Date: Nov 2005
Posts: 170
alcoholic is on a distinguished road

Re: C++ beginner


Try this code with g++ test.cpp
CPP / C++ / C Code:
#include <iostream>
int main();
{
std::cout << "Hello World!\n";
return 0;
}
__________________
Gravitation is not responsible for people falling in love.
-Albert Einstein
Last edited by admin II : 11-Nov-2006 at 04:17. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #3  
Old 11-Nov-2006, 06:41
amad1337's Avatar
amad1337 amad1337 is offline
Junior Member
 
Join Date: Dec 2004
Posts: 54
amad1337 will become famous soon enough

Re: C++ beginner


CPP / C++ / C Code:
#include <iostream.h>


int main(); /*semicolon shouldn't be here take that out*/
{
cout << "Hello World!\n";
     return 0;
}
  #4  
Old 11-Nov-2006, 08:38
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: C++ beginner


Also, iostream.h is deprecated. Use <iostream> and put
CPP / C++ / C Code:
using namespace std;
after the include statement. And if the tutorial uses iostream.h, a more up-to-date tutorial wouldn't hurt...
  #5  
Old 11-Nov-2006, 13:45
littlewenwen littlewenwen is offline
New Member
 
Join Date: Nov 2006
Posts: 5
littlewenwen is on a distinguished road

Re: C++ beginner


Thanks for all your kindly help. I tried all the suggestion, here is what I got:


#include <iostream>
int main();
{
std:: cout << "Hello World!\n";
return 0;
}


After I g++ above, the following error message appear:

test.cpp:3: error: expected unqualified-id before ‘{’ token



Next I delete the semicolon for line 2 (int main(), and the program became


#include <iostream>
int main()
{
std:: cout << "Hello World!\n";
return 0;
}


After I g++ again, I got the file a.out (what should I do next?); But If I use gcc, I got many error message (see following):


doudou66@linux:~/C++> gcc test.cpp
/tmp/ccvWewmo.o: In function `main':
test.cpp.text+0x25): undefined reference to `std::cout'
test.cpp.text+0x2a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccvWewmo.o: In function `__tcf_0':
test.cpp.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccvWewmo.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp.text+0x74): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccvWewmo.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status



The next thing I did is to add using namespace std, so the program became:



#include <iostream>
using namespace std;
int main()
{
std:: cout << "Hello World!\n";
return 0;
}



After I g++, I got the file a.out. But If I try to gcc, I still got the many error message.

What's the difference between g++ and gcc? If I assume g++ works in my case (as I can get the file a.out), what should I do next? To link? But how? It didn't specify in the tutorial?

Or is it possible I didn't set the gcc correct? (I just install the gcc and use it right away, didn't do anything else).
  #6  
Old 11-Nov-2006, 14:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ beginner


Quote:
Originally Posted by littlewenwen



After I g++ again, I got the file a.out (what should I do next?);

Enter ./a.out on the command line. Many (most?) distributions these days to not include the current directory on the PATH (it's a security issue). So you have to enter the "dot" to indicate current directory.

Quote:
Originally Posted by littlewenwen
But If I use gcc, I got many error message

gcc and g++ are both part of the GNU compiler suite.

To put it in simplest terms, if you have a C source file called "something.c" then you can compile it with the following:

gcc something.c

If there are no errors, then the default output file name (the executable) is called a.out. There are command-line switches that you can use to cause other things to happen, but this is the simplest.

If you have a C++ source file called "something.cpp" then you can compile it with the following:

g++ something.cpp

f there are no errors, then the default output file name (the executable) is called a.out. There are command-line switches that you can use to cause other things to happen, but this is the simplest.

Regards,

Dave
  #7  
Old 11-Nov-2006, 14:14
davis
 
Posts: n/a

Re: C++ beginner


Quote:
Originally Posted by littlewenwen
Thanks for all your kindly help. I tried all the suggestion, here is what I got:


#include <iostream>
int main();
{
std:: cout << "Hello World!\n";
return 0;
}


After I g++ above, the following error message appear:

test.cpp:3: error: expected unqualified-id before ‘{’ token



Next I delete the semicolon for line 2 (int main(), and the program became


#include <iostream>
int main()
{
std:: cout << "Hello World!\n";
return 0;
}


After I g++ again, I got the file a.out (what should I do next?); But If I use gcc, I got many error message (see following):


doudou66@linux:~/C++> gcc test.cpp
/tmp/ccvWewmo.o: In function `main':
test.cpp.text+0x25): undefined reference to `std::cout'
test.cpp.text+0x2a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccvWewmo.o: In function `__tcf_0':
test.cpp.text+0x47): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccvWewmo.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp.text+0x74): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccvWewmo.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status



The next thing I did is to add using namespace std, so the program became:



#include <iostream>
using namespace std;
int main()
{
std:: cout << "Hello World!\n";
return 0;
}



After I g++, I got the file a.out. But If I try to gcc, I still got the many error message.

What's the difference between g++ and gcc? If I assume g++ works in my case (as I can get the file a.out), what should I do next? To link? But how? It didn't specify in the tutorial?

Or is it possible I didn't set the gcc correct? (I just install the gcc and use it right away, didn't do anything else).

gcc is the C compiler invocation method. g++ is the C++ compiler invocation method.

Use:

g++ -g -Wall -W -o executabe-name-that-you-want-to-use your-cpp-file.cpp

-g means to include debugging symbols in the build. These will be necessary when debugging your applications. Use gdb in order to debug your applications.

-Wall means to include all warnings.

-W means to include "extra" warnings. It has been replaced by -Wextra, but the "old" version -W is still supported.

-o filename means to output the result of compilation and linkage to the named filename. If not specified, a.out is used.


:davis:
  #8  
Old 11-Nov-2006, 18:55
littlewenwen littlewenwen is offline
New Member
 
Join Date: Nov 2006
Posts: 5
littlewenwen is on a distinguished road

Re: C++ beginner


Thank you SO MUCH for the excellent guidance. I hope I can learn more from you in the future.
 
 

Recent GIDBlog2nd Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome CPP / C++ Forum 0 19-Aug-2005 09:25
Suggestions for beginner Propain CPP / C++ Forum 1 16-Mar-2005 20:46
RAID 0 -- beginner questions, need recommendation ldriskell Computer Hardware Forum 2 09-Jul-2004 19:47
Apache 1.3.28 >XP > Complete Beginner setting alias? thinnk Apache Web Server Forum 2 26-Mar-2004 00:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.