FreeBSD c++ programming

Hi there fellows!
I am absolutely new to programming and stuff. I've been said that it is worth the effort to lear how to use vi and program in linux systems, like FreeBSD named in the title of the post.-
I have so far managed to get a text file with vi, named it helloworld.cc nad am trying to compile it now.
Thing is that I get an error, which is:

error: use of undeclared identifier 'cout';

As I said, I am new, and do not know much about this, apology if question is quite too 'begginer-style'.-
Also, I'd like to ask if there is an specific tutorial for this purpose of writing and compilin(and running!) stuff in C++ I've been googling for quite some time, but find so many different things, advices and tutorials, that I get confused, so it would be great to get some info from someone that knows about it. It's not lazyness in search! HE he...

Thanks!

1
2
3
4
5
6
# include <iostream>
int main()
{
   cout << "Hello World!";
   return 0;
}
For tutorials cplusplus has some great tutorials for just starting out.

For the error you are getting, use this to declare cout.

1
2
3
4
5
6
7
8
9
# include <iostream>

using namespace std; //declares cout and cin. Allows you  to use them.

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


or you could do it this way

1
2
3
4
5
6
7
# include <iostream>

int main()
{
  std::cout << "Hello World!"; //works the same way as using namespace std.
   return 0;
}
Last edited on
Oh! Great!
Thanks for your reply, Hp of Legend. I really am thankful. It works! Does the Deitel book lie? =D In the Deitel book also states that we must use iostream.h, but I read here that it's deprecated.-
Thanks for your explanation, I have tried it and it compiles and works. Think I have a great long way ahead, uh?
I'll also follow your advice in concern to the tutorials section. I am really used to windows stuff, you know, it's 'easier', many things are done for one, and want to learn Linux/FreeBSD.-

Regards! And, thanks again, your help is highly appreciated.-
> Does the Deitel book lie?

If it asks you to use <iostream.h>, it is completely obsolete (pre-1998 C++).

Get one of these books:

Programming: Principles and Practice Using C++ - Stroustrup
http://www.amazon.com/dp/0321992784

C++ Primer (5th Edition) - Lippman, Lajoie, Moo
http://www.cplusplus.com/forum/beginner/148831/#msg779361

Accelerated C++: Practical Programming by Example - Koenig and Moo
http://www.amazon.com/dp/020170353X

And compile with: CC -std=c++11 -Wall -Wextra -pedantic-errors myprogram.cc
Does the Deitel book lie?
you are using a very old book. all books before C++99 uses <iostream.h>

C++ How to Program 9th ed was published in 2013.
http://www.deitel.com/Books/C/CHowtoProgram9e/tabid/3644/Default.aspx

example form the 8th edition, page38:
1
2
3
4
5
6
7
8
9
10
11
 // Fig. 2.1: fig02_01.cpp
 // Text-printing program.
 #include <iostream> // allows program to output data to the screen

 // function main begins program execution
 int main()
 {
 std::cout << "Welcome to C++!\n"; // display message

 return 0; // indicate that program ended successfully
 } // end function main 

Last edited on
Hey! I'm sorry for delay in response, apologize!
Thanks JLBorges. I'd LOVE to be able to buy those books you name, thing is I live in argentina, and it's not so easy to get your hands on them. This is the thing I've been able to find. WOW with the compile line, I just:
c++ myprogram.cc
=S

Thanks to you too, anup30. Yes it's old, not older than me nevertheless =P He he.-
Oh, I see that it has changed the cout thing, I must add std:: intead of just using it alone. I like theory, and it's really clear for me in that book, but I see that I have to update the coding part. I'll try my best =D

Thanks again!
JLBorges, my man, your are as good as reading The Aleph, when he finally gets to see it... Thanks indeed for your answer and the links, and specially your intention to share and help.-
Should I mention that the Deitel book I have is physical, paper, ink, pages =D That's the reason for I've found it quite old. But I also read from the pc, so this is highly appreciated.-
Great! I am really thankful.-

Reagards!
I see that it has changed the cout thing, I must add std:: intead of just using it alone.

three ways to write "std" :

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main () {
cout << "Welcome to C++!" << endl;	
return 0;	
}


1
2
3
4
5
6
#include <iostream>
//using namespace std
int main () {
std::cout << "Welcome to C++!" << std::endl;	
return 0;	
}


1
2
3
4
5
6
7
8
#include <iostream>
using std::cout;
using std::endl;

int main () {
cout << "Welcome to C++!" << endl;	
return 0;	
}
Yes! Thanks a lot anup30!
I see it was a good plan to ask here, great answers, and specially your concern to help.-
Great, I am very thankful, greetings to everyone. I guess I have a BIG way ahead, might I not come here very often, but I'll surelly will. Hope some day it can be to help too! =D

std::<<cout "Thanks! << std::endl;
Topic archived. No new replies allowed.