简体   繁体   中英

C++: Unable to resolve identifier cout, Netbeans, Ubuntu

I am using C++ on Netbeans 7.1 on Ubuntu 11.04. For some reason, the following code results in the error message "Unable to resolve identifier cout".

#include <iostream>

using namespace std;
int main()
{
    std::cout << "Hello,world!\n";
    return 0;
}

Any help resolving this problem would be greatly appreciated.

The solution for your problem is at least strange ;) Once iostream header is added, one has to reparse code. Click right on a project, go to code assistance and click to reparse project. Worked for me. I was using netbeans for mac.

What sort of file is this in? Is it a .h file, or .hpp file? I had this same issue. Netbeans can be ridiculous sometimes with C++. For me, I changed #include <iostream> to #include<iostream.h>

This may seem too simple, but...

In my NetBeans installation, when I go to create a new project, specify C/C++, it brings up a dialog box prompting for "Project Name:", location, folder, makefile name, and then... a check box for "Create Main File", an edit box with "main" filled in, and to the right of that is a drop down list that reads "C". If you hit Finish, this will create "main.c" (C, but NOT a C++ file). Instead, in the drop down list, select "C++". Then the IDE creates main.cpp, which will be compiled with g++ and will find those includes and functions.

There is a difference between std::cout and cout. You don't currently have std::cout defined in your file. std::cout is ac standard out. In C++ we only need cout to work with iostream.

If you must use a standard c out then do the following:

Add this to the top under iostream

#include <iostream> //Input output stream in C++
#include <cstdlib> //Stands for c standard library
using namespace std; 

Your code will now work because: This change defines std::cout and std::cin among other things. (standard in, standard out respectively.)

However, I'd recommend this alternative if you don't need standard in outs: Replace std::cout with cout, because cout is defined in iostream in C++. Your program would have worked without the std:: portion of your cin cout commands because you originally included iostream.

check whether iostream is really getting included; i have tried your code on my machine using eclipse cdt it worked fine.so, please check the includes.

Hey look at your Output Debug. You may see "no permission". After I changed the file permission of "/YourProjekt/dist/Debug/GNU-Linux/file" to runable and everyone can read and write the error disappeared. (BTW: I had the bug because I was on a NTFS System with my Projekt, it have to be ext partition) Hope I can help you with that.

Try taking out the using namespace std; - it's generally considered bad form anyway :-)

I'm not sure that will fix the problem but most people either use the namespace or fully qualify things like std::cout . I've never seen code that does both.

The other thing to check is that the iostream header actually is being bought in. In other words, are there any errors on that line. A lot of problems (at least in the Windows world, so it may not necessarily apply to you) seem to be due to faulty path setup in NetBeans.

尝试取出cout旁边的std ::

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM