ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have decided to try out a little bit of c++ programing so I went out and bought me a book "Sams Teach your self C++ in 21 Days"
Now I am at the part of the book where you write the hello world script. when I run
Code:
gcc hello.cpp
I get this
Quote:
james@james-desktop:~/Desktop$ gcc hello.cpp
/tmp/ccYHXH5l.o: In function `main':
hello.cpp:(.text+0xa): undefined reference to `std::cout'
hello.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccYHXH5l.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccYHXH5l.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Far as I can see the correct is correct by the book.
Okay I am moving on and and ran into another issue. I tried to compile this code with
g++ exer.cpp
Quote:
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}
and I got the error
Quote:
james@james-desktop:~/Desktop$ g++ exer.cpp
exer.cpp: In function ‘int main()’:
exer.cpp:6: error: ‘endl’ was not declared in this scope
exer.cpp:10: error: ‘end’ was not declared in this scope
What do you think end is supposed to be in your code?
I assume you just need that to be endl
Did you make a typo in copying code from the book? Or did the author of the book make the typo?
Either way, the idea of using a book or tutorial is to try to understand the code you are copying, not just copy it. If you try to understand the code you are copying, you should have no trouble recognizing end was a typo for endl.
No I am not just copying. The exercise is to determine what the code does then compiles it and see if I am right.
From the book word for word.
Quote:
1. Look at the following program and try to guess what it does without running it.
Quote:
#include <iostream>
int main()
{
int x = 5;
int y = 7;
std::cout << endl;
std::cout << x + y << " " << x * y;
std::cout << end;
return 0;
}
To get it compile using g++ I had to edit like this
Quote:
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int y = 7;
cout << endl;
cout << x + y << " " << x * y;
cout << endl;
return 0;
}
So I suppose it a possible typo in the book?
Like I said the book "Sams Teach Yourself C++ in 21 Days" is publish by Sams which is a well known for Education References.
How ever I notice now I seem to be editing ever tutorial to get them to work.
And just to be sure gcc is a ansi compliant compiler right? I think the bookk may be written for a microsoft ansi compliant compiler. Not sure if that makes a big difference as I am a newbie here in the area.
Thanks again. It starting to work out. I'll let you guys know how things work out.
1. Despite the best of intentions, many (most) books have typos.
But at least books have technical editors, and some kind of formal review process. My experience is that other resources (like net postings) are often worse.
2. Yes, you want "endl", not "end".
And yes, it's fully qualified name is "std::endl".
3. Yes, gcc is a C compiler, and g++ is a C++ compiler.
4. Yes, the snippet above builds and executes without error:
Code:
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int y = 7;
cout << endl;
cout << x + y << " " << x * y;
cout << endl;
return 0;
}
I have decided to try out a little bit of c++ programing so I went out and bought me a book "Sams Teach your self C++ in 21 Days"
if this is not for homework/work and its just for learning how to program, i would suggest not using C++. Languages like Python/Perl etc doesn't need to be compiled, and you don't have to work with things like pointers, memory management etc. You will make good use of your time learning how to write algorithms and stuff, instead of figuring out how to compile properly, why your pointers doesn't work etc....
Perl:
Code:
printf "Enter a number: "
chomp($number=<>);
print "Number entered: $number\n";
Python:
Code:
number=raw_input("Enter a number:")
print "Number is ", str(number)
Last edited by ghostdog74; 09-03-2010 at 01:40 AM.
I can do a little bit of perl like making calculations but I got a book on C++ so not I am going to learn it. But after I get the hang of C++ from what I understand I should practically be able to write alot of perl easier. So I have plans to jump into perl. The real reason of me wanting to learn some strong programming languages is so I can write some opensource programs.
Thanks for the help everyone.
Just a update on how things are going and for the people who have helped me and/or would like to know what I've done.
I am learning more about integers and stuff. So far I understands how functions work to. Also Learned how to figure out how much integers user in memory with
Quote:
#include <iostream>
int main()
{
using std::cout;
cout << "The size of an int is:\t\t"
<< sizeof(int) << " bytes.\n";
cout << "The size of a short int is:\t"
<< sizeof(short) << " bytes.\n";
cout << "The size of a short int is:\t"
<< sizeof(long) << " bytes.\n";
cout << "The size of char is:\t\t"
<< sizeof(char) << " bytes.\n";
cout << "The size of float is:\t\t"
<< sizeof(float) << " bytes.\n";
cout << "The size of a double is:\t"
<< sizeof(double) << " bytes.\n";
cout <<"The size of a bool is:\t"
<< sizeof(bool) << " bytes.\n";
return 0;
}
Hopefully in another week I might be able to start coding my own programs at the rate Im going.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.