I need help: error: 'cout' was not declared in this scope.

Apr 24, 2011 at 4:38pm
Hi everyone.

I'm trying to compile this simple code:

#include <iostream>

using namespace std;

int main(void)
{
cout << "Testing..." << endl;
return 0;
}

but I get the following errors:

main.cpp:7: error: 'cout' was not declared in this scope
main.cpp:7: error: 'endl' was not declared in this scope

I tried google but all I found were people who forgot to include the namespace declaration or the header file. I really don't see what's wrong. I tried reinstalling the mingw compiler and added the path to the iostream file do my environmental variables. I restarted the computer, but it still doesn't work. A few weeks ago, the codes were compiling fine, but all of a sudden I started getting these errors, which makes me think the problem's not in the code itself, but in something I did to my computer. Does anyone have any idea of what it might be?
Apr 24, 2011 at 4:43pm
The code is perfectly fine.
What version of MinGW are you using?

and added the path to the iostream file do my environmental variables.

That sounds fishy - it's not necessary and perhaps even contra-productive.
Something about your install is probably broken. Remove MinGW tracelessly, remove the entry from your PATH variable and download and install the latest release here:
http://tdm-gcc.tdragon.net/
Apr 24, 2011 at 4:49pm
I'm not sure what version I'm using. I think it might be version 5.1.6 of MinGW. That's what it says on the installation .exe file's name.

What's weired is that everything was working fine a few weeks ago. Anyway, I'll try doing what you suggested and I'll post whether it worked or not.
Last edited on Apr 24, 2011 at 4:54pm
Apr 24, 2011 at 5:11pm
Oh, I just found out what happened. I uninstalled the old version of MinGW and restarted the computer to install the new version, like you told me to. I decided to check that I had really removed it completely by trying to compile my c++ program. Surprisingly, the program compiled just fine! I thought: how could it be, since I had no compiler. However, I searched for leftover mingw files and found an old DevC++ directory with mingw files in it. It turns out that I uninstalled DevC++ but the files were not completely removed. And, since it comes with a compiler, I ended up with two versions of mingw. When I uninstalled the most recent one, everything went back to normal. Perhaps the compiler was getting confused with the ambiguity of having iostream files in two accessible directories.

Thank you very much for your help. Everything's working fine now.
May 10, 2011 at 1:37am
im really stuck on this same issue but ive checked the compiler..i'm really new to programing like day 3 new and am sure it's something simple but if i could get some help on this i could move past this and move on in my learning.

here's the code:
#include <iostream>

int main() {
cout << "\n i am a programer.";
cout << "\n this is great code.";
cout << "\n do you like my style. im done. \n";
return 0;
}

(this is example code from a book just so y'all did'nt think i was a loser)
May 10, 2011 at 1:45am
That looks fine to me. What's not working?
May 10, 2011 at 2:06am
You need some namespace std:: qualifiers. And in the future, please don't hijack someone else's thread.
May 10, 2011 at 2:22am
Oh wow, I totally missed that...need to look harder. :/
May 10, 2011 at 3:11am
as for the OP. try adding
1
2
using std::cout;
using std::endl;


instead of
 
using namespace std;


using namespace std; is stupid anyhow because it essentially defeats the purpose of a namespace.
Topic archived. No new replies allowed.