V
V
Vladimir Chernyshev2012-02-13 22:44:21
C++ / C#
Vladimir Chernyshev, 2012-02-13 22:44:21

Helloworld.cpp does not compile to g++

#include <iostream>

int main ()
{
  cout << "Hello world\n";
}


Code taken from Stroustrup second edition. Googling has shown that C++ has changed somewhat since then (1991) and now requires the std prefix or the use of a namespace. But I was clearly told that the course book and the exam were based on the second edition. How to make the program compile? Tried playing with the -std switch - didn't work :( $ uname -a && g++ -v Linux home 3.0.0-15-generic #26-Ubuntu SMP Fri Jan 20 17:23:00 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux Used internal specifications COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target architecture: x86_64-linux-gnu
g++ helloworld.cpp -o helloworld.cpp
helloworld.cpp: В функции «int main()»:
helloworld.cpp:5:3: ошибка: нет декларации «cout» в этой области видимости
helloworld.cpp:5:3: замечание: suggested alternative:
/usr/include/c++/4.6/iostream:62:18: замечание: «std::cout»
$



Configuration options: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README. Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id -- with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/ usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target= x86_64-linux-gnu
Multithreading model: posix
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
M
Meliborn, 2012-02-13
@Meliborn

Um, maybe I don't understand.
using namespace std;
or
std:cout

H
Haroo Mambooroo, 2012-02-14
@AcidLynx

as an option to write and compile using run like
#include <iostream.h>
int main ()
{
cout << "Hello\n";
return 0;
}

g++ helloworld.cpp -Wno-deprecated -o helloworld

./helloworld

G
Gribozavr, 2012-02-13
@gribozavr

If you have an old target compiler that is incompatible with the standard, then develop on it, otherwise, in addition to this quite obvious glitch, you will get a dozen more non-obvious ones.

A
Artyom, 2012-02-13
@Dracontis

> But I was clearly told that the course paper and the exam is the second edition.
In my opinion, it is easier to convince the speaker than to do archeology and try to tune the 1991 compiler. Try creating a kostil.h file and declaring a namespace there. On the one hand, one inclusion will not be a serious deviation from the book, and on the other hand, it can solve the problem.

B
bogolt, 2012-02-14
@bogolt

#ifdef CPP_NEW_VERSION
#define NAMESPACE using namespace std;
#else
#define NAMESPACE
#endif

And in the properties of your project, you can specify the desired define so that the namespace is connected.

E
ertaquo, 2012-02-14
@ertaquo

Maybe try building with gcc 1.xx ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question