View Full Version : cout issue
toontastic
November 21st, 2012, 03:36 PM
I've decided to try and learn a programming language. I used C++ many moons ago at Uni and thought if I'm going to start somewhere might as well be there. So I've installed Eclipse along with the extra C++ files. I've have written the following code:
#include <iostream.h>
#include "stdafix.h"
int main()
{
cout << "Hello World!\n";
return 0;
}
which I assumed would simply work. Unfortunatly I get the error '"Symbol 'cout' could not be resolved." - Resource hello.cpp, path /hello, line 12, Type semantic error'.
I figured it would be something I had written wrong so I rang the C++ Hellow World example that comes in Eclipse but this also fails to run with the same error.
Have I missed something somewhere ? :confused:
Thanks.
ssam
November 21st, 2012, 04:02 PM
cout is in the std namespace. either you need to give the full address to it
std::cout << "Hello World!\n";
or you can put
using namespace std
after the includes, to tell the compile to search std for identifiers.
steeldriver
November 21st, 2012, 04:29 PM
I think your confusion arises because <iostream.h> is non-standard (and deprecated - in fact I'm not even sure it's included in modern distributions) - iirc it predated the adoption of namespaces so plain 'cout' used to work back in the day
You should probably be using <iostream> (without the .h) - in which case you will need to specify the namespace, as ssam says
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
See article here for more info / background --> http://members.gamedev.net/sicrane/articles/iostream.html
EDIT: not sure what your "stdafix.h" is - maybe you are trying to mimic Visual Studio's "stdafx.h" convention?
toontastic
November 22nd, 2012, 10:23 PM
Thank you both very much for your help. Seems it was further back when I used it than my brain cells first thought. That has worked perfectly for me thanks.
As for the stdafix.h, I found it in a C++ ebook I was reading and it was suggested as part of the test code for Hello World so I wasn't sure if it was something I was missing. Needless to say I have not stuck with that book.
toontastic
November 22nd, 2012, 10:32 PM
Thank you both very much for your help. Seems it was further back when I used it than my brain cells first thought. That has worked perfectly for me thanks.
As for the stdafix.h, I found it in a C++ ebook I was reading and it was suggested as part of the test code for Hello World so I wasn't sure if it was something I was missing. Needless to say I have not stuck with that book.
Actually I was wrong, that hasn't worked either. Still getting an error about cout. If I try to build the code I get.
**** Build of configuration Debug for project Hello ****
make all
Building file: ../hello.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"hello.d" -MT"hello.d" -o "hello.o" "../hello.cpp"
../hello.cpp: In function ‘int main()’:
../hello.cpp:6:2: error: ‘cout’ was not declared in this scope
../hello.cpp:6:2: note: suggested alternative:
In file included from ../hello.cpp:1:0:
/usr/include/c++/4.7/iostream:62:18: note: ‘std::cout’
What I now have written is
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
SuperCamel
November 22nd, 2012, 11:08 PM
There is nothing wrong with that code. It should just compile and run. There must be something else going wrong.
steeldriver
November 23rd, 2012, 12:23 AM
Agreed - it builds and runs for me [NB on gcc/g++ 4.6.3 not 4.7] even with all of your extra compile options
How did you install the build tools? maybe something is misconfigured?
EDIT: just tried it in a crunchbang (Debian) VM with gcc/g++ 4.7.1 and it works there as well
lisati
November 23rd, 2012, 12:56 AM
@OP: Do you have build-essential installed?
Zugzwang
November 24th, 2012, 12:57 AM
Actually I was wrong, that hasn't worked either. Still getting an error about cout. If I try to build the code I get.
**** Build of configuration Debug for project Hello ****
make all
Building file: ../hello.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"hello.d" -MT"hello.d" -o "hello.o" "../hello.cpp"
../hello.cpp: In function ‘int main()’:
../hello.cpp:6:2: error: ‘cout’ was not declared in this scope
../hello.cpp:6:2: note: suggested alternative:
In file included from ../hello.cpp:1:0:
/usr/include/c++/4.7/iostream:62:18: note: ‘std::cout’
What I now have written is
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
Are you sure that you pasted the correct contents of "hello.cpp"? Your compilation error message says that "cout" was not declared in line 6, but line 6 is actually "return 0;". Is it possible that in fact your IDE tries to compile your old code?
toontastic
November 24th, 2012, 07:51 PM
@OP: Do you have build-essential installed?
Not sure, I installed all the extras that mentioned C++
toontastic
November 24th, 2012, 07:52 PM
Are you sure that you pasted the correct contents of "hello.cpp"? Your compilation error message says that "cout" was not declared in line 6, but line 6 is actually "return 0;". Is it possible that in fact your IDE tries to compile your old code?
Hmmmm not sure, I'll try again deleting everything and creating a new project and let you know.
toontastic
November 24th, 2012, 08:02 PM
Right tried again, I had it set to Build Automatically so didn't get the build error but got the error
Description Resource Path Location Type
Symbol 'cout' could not be resolved brandnew.cpp /Brand New line 5 Semantic Error
Code I used was
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
steeldriver
November 24th, 2012, 08:19 PM
Right tried again, I had it set to Build Automatically so didn't get the build error but got the error
Description Resource Path Location Type
Symbol 'cout' could not be resolved brandnew.cpp /Brand New line 5 Semantic Error
Code I used was
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
So you are using some kind of IDE (code::blocks maybe?) - what happens if you just open a terminal, cd to the directory containing the file 'brandnew.cpp', and try to compile and link on the command line
g++ -o brandnew brandnew.cpp
toontastic
November 24th, 2012, 08:31 PM
Ok I did
cd Brand\ New
Then I wrote
g++ -o brandnew brandnew.cpp
and I got no response at all. So I tried
g++ -o brandnew.cpp
and I got the error
g++: fatal error: no input files
compilation terminated.
steeldriver
November 24th, 2012, 08:41 PM
g++ will give no response if compilation succeeds - basically that means there were no errors and it was able to resolve std::cout correctly
Unfortunately
g++ -o brandnew.cpp probably overwrote your source code file (-o means output so it will try to create the executable program but give it the name 'brandnew.cpp' instead of plain 'brandnew')
I suggest creating the .cpp file again, running
g++ -o brandnew brandnew.cpp and then checking if the executable was produced
ls -land if so trying to run it
./brandnewBTW if your IDE uses Makefiles under the hood you may run in to trouble if your build directory name contains spaces - just a heads up
toontastic
November 24th, 2012, 08:49 PM
g++ will give no response if compilation succeeds - basically that means there were no errors and it was able to resolve std::cout correctly
Unfortunately
g++ -o brandnew.cpp probably overwrote your source code file (-o means output so it will try to create the executable program but give it the name 'brandnew.cpp' instead of plain 'brandnew')
I suggest creating the .cpp file again, running
g++ -o brandnew brandnew.cpp and then checking if the executable was produced
ls -land if so trying to run it
./brandnewBTW if your IDE uses Makefiles under the hood you may run in to trouble if your build directory name contains spaces - just a heads up
Oooooo excellent that worked I was able to finally see Hello World! :guitar:
Ok I'll make sure I leave out the spaces now.
Right it seems I've done something funny in terms of IDEs in Eclipse. Is there a way to fix this so I don't have to use terminal and can infact just run the programs in Eclipse ?
toontastic
November 24th, 2012, 08:58 PM
Ok very strangly after doing that in terminal it now builds fine and returns Hello World! in Eclipse as well now.
Thank you all very much for your help.
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.