-2

I'm trying to write my first code on ubuntu terminal using c++ .I created a new cpp file named aaa by

"nano aaa.cpp"

then inside I wrote

#include<iostream>
using std::cout;
using std::endl;

int main(int argc, car** argv)
{
   cout << "hello" << endl;
   return 0; 
}

i saved and got out but when i tried typing

g++ aaa.cpp

I got the error

error: ‘endl’ was not declared in this scope cout << "hello" << endl;

where did I go wrong I tried

$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib

but it was no good

any help?

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
ganam
  • 53
  • 2
  • 6

2 Answers2

2

Stylistically, I prefer to be explicit: std::cout and std::endl.

#include <iostream>

int main(int argc, char** argv) {
  std::cout << "hello" << std::endl;
  return 0; 
}

This also fixes a tyo of yours: char, not car and repairs the #include.

This works as expected:

$ g++ -Wall -pedantic -o foo2 foo2.cpp
$ ./foo2
hello
$ 

If you wanted to, you could also use

using namespace std;

but as stated, I prefer to more explicit form.

Edit: Nothing as much fun as debating the beancounters. OP question is likely having _another error he is not sharing. His code, repaired for char actually builds:

$ cat foo3.cpp 
#include <iostream>
using std::cout;
using std::endl;

int main(int argc, char** argv) {
  cout << "hello" << endl;
  return 0; 
}
$ g++ -Wall -pedantic -o foo3 foo3.cpp
$ ./foo3
hello
$ 

Ubuntu 16.04, g++ 5.4.0

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

First, make sure you have the tools you need to be able to compile a C++ code on Ubuntu. For that run the following code in the command line : This line will install all the basic stuff you need for compiling a C++ code, it will install C, C++, and make.

 sudo apt-get install build-essential

Now that you have all you need, I will suggere to explicetely using std::cout / std::endl . That way you don't import all the stuff available under the namespace std that you are not using. Using std::cout / std::endl shows clearly the origin the instance you are using. Notice : you have an error in the main function argument, namely : car, it should be char

#include<iostream>
int main(int argc, char** argv)
{
   std::cout << "hello" << std::endl;
   return 0; 
}

Now you can compile and run your code this way : in this example I'm calling the executable file "hello"

g++ -Wall -o hello aaa.cpp
./hello
cyrildz
  • 73
  • 7