I’m practicing using mulitple files and header files etc. So I have this project which takes two numbers and then adds them. Pretty simple.
Here are my files:
main.cpp
#include <iostream>
#include "add.h"
int main()
{
int x = readNumber();
int y = readNumber();
writeAnswer(x + y);
return(0);
}
io.cpp
int readNumber()
{
int x;
std::cout << "Number: ";
std::cin >> x;
return x;
}
void writeAnswer(int x)
{
std::cout << "Answer: ";
std::cout << x;
}
add.h
#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED
int readNumber();
void writeAnswer(int x);
#endif // #ifndef ADD_H_INCLUDED
The error is showing up in io.cpp. The exact errors are:
Does anyone have any idea why this may be happening? Thanks.
EDIT: I made a small project yesterday with the same amount of files (2 .cpp and 1.h) and I didn’t include the iostream header in the other .cpp and it still compiled and ran fine.
asked Jul 7, 2012 at 14:43
3
add #include <iostream>
to the start of io.cpp
too.
answered Jul 7, 2012 at 14:45
unkulunkuluunkulunkulu
11.5k2 gold badges31 silver badges49 bronze badges
5
If you’re using pre-compiled headers with Microsoft’s compiler (MSVC), remember that it must be:
#include "stdafx.h"
#include <iostream>
and not the other way around:
#include <iostream>
#include "stdafx.h"
In other words, the pre-compiled header include file must always come first. (The compiler should give you an error specifically explaining this if you forget.)
answered Sep 20, 2018 at 19:41
JukesJukes
3973 silver badges5 bronze badges
0
I had a similar issue and it turned out that i had to add an extra entry in cmake
to include the files.
Since i was also using the zmq library I had to add this to the included libraries as well.
answered May 22, 2018 at 17:26
Dimedron 0 / 0 / 0 Регистрация: 09.09.2015 Сообщений: 1 |
||||
1 |
||||
06.06.2017, 21:00. Показов 36613. Ответов 1 Метки нет (Все метки)
Вот такие ошибки в общем Собственно, вот сам код
0 |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
06.06.2017, 21:00 |
Ответы с готовыми решениями: error C2886: std::cout: использование символа в «using»-объявлении члена не допускается
В зависимости от времени года «весна», «лето», «осень», «зима» определить погоду «тепло», «жарко», «холодно», «очень холодно» namespace «std» has no member «vector» 1 |
- VsCode Version: 1.23.1
- Extension Version: 0.17.1
- Os: Win10 Pro Education
- Version: 1803
- gcc : wsl ubuntu16.04(5.4)
I’ve followed instruction described in https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/LanguageServer/Windows%20Subsystem%20for%20Linux.md, however, the intellisense tool generate: namespace «std» has no member «cout». This issue also is presented with other members in headers I use in my project, e.g., namespace «omnetpp» has no member «simsignal_t»
c_cpp_properties.json
{
"configurations": [
{
"name": "WSL",
"intelliSenseMode": "clang-x64",
"compilerPath": "/usr/bin/gcc",
"includePath": [
"${workspaceFolder}",
"/mnt/c/Users/Geo/omnetpp-5.2/include",
"/mnt/c/Users/Geo/omnetpp-5.2/include/omnetpp",
"/mnt/c/Users/Geo/omnetpp-5.2/include/omnetpp/platdep",
"/mnt/c/Users/Geo/omnetpp-5.2/include/platdep",
"/mnt/c/Users/Geo/omnetpp-5.2/src/cmdenv",
"/mnt/c/Users/Geo/omnetpp-5.2/src/common",
"/mnt/c/Users/Geo/omnetpp-5.2/src/envir",
"/mnt/c/Users/Geo/omnetpp-5.2/src/eventlog",
"/mnt/c/Users/Geo/omnetpp-5.2/src/layout",
"/mnt/c/Users/Geo/omnetpp-5.2/src/nedxml",
"/mnt/c/Users/Geo/omnetpp-5.2/src/qtenv",
"/mnt/c/Users/Geo/omnetpp-5.2/src/qtenv/osg",
"/mnt/c/Users/Geo/omnetpp-5.2/src/scave",
"/mnt/c/Users/Geo/omnetpp-5.2/src/sim",
"/mnt/c/Users/Geo/omnetpp-5.2/src/sim/netbuilder",
"/mnt/c/Users/Geo/omnetpp-5.2/src/sim/parsim",
"/mnt/c/Users/Geo/omnetpp-5.2/src/tkenv",
"/mnt/c/Users/Geo/inet/src",
"/mnt/c/Users/Geo/inet/src/inet",
"/mnt/c/Users/Geo/inet/src/inet/common",
"/mnt/c/Users/Geo/inet/src/inet/common/geometry/common",
"/mnt/c/Users/Geo/inet/src/inet/mobility/base",
"/mnt/c/Users/Geo/inet/src/inet/mobility/contract",
"/mnt/c/Users/Geo/inet/src/inet/mobility/single",
"${workspaceRoot}/src",
"${workspaceRoot}/src/base",
"${workspaceRoot}/src/engine",
"${workspaceRoot}/src/simple"
],
"defines": [
],
"browse": {
"path": [
"${workspaceFolder}",
"/mnt/c/Users/Geo/omnetpp-5.2/include",
"/mnt/c/Users/Geo/omnetpp-5.2/src",
"/mnt/c/Users/Geo/inet/src",
"/mnt/c/Users/Geo/gsim/slaw/src"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}
ive made a simple hello world script:
#include <QCoreApplication>
#include <iostream>
int main()
{
std::cout << "hello world" <<std::endl;
return 0;
}
I get an error in the editor, and not when running the script. the thing is, i dont get why the editor tells me something is wrong, and i dont really know how to fix it, maybe someone else have runned into the same issue?
Edit:
Thanks everyone for helping me!
mrjj’s solution: Turning off «clang format» in help > About_Plugins seems to work (in hiding every clang notification thus hiding even the incorrect ones). Im fine with the solution though, they arent useful in my opinion anyway.
Indian Technical Authorship Contest starts on 1st July 2023. Stay tuned.
In this article, we have explored the reason behind the error «cout is not a member of std» and 2 fixes to resolve this compilation error with C++ code.
Table of contents:
- Reason for the error
- Fix 1: Add header file
- Fix 2: For Microsoft’s compiler MSVC
Reason for the error
While compiling a C++ code, you may face the following error:
cout is not a member of std
The reason is that the relevant header files are not provided in the code due to which the compiler is unable to locate the function cout.
Following C++ code which give this error when compiled:
int main() {
std::cout << "data" << std::endl;
return 0;
}
In this article at OpenGenus, we have presented fixes which will resolve this issue.
The cout function is available in iostream header file which needs to be included at the beginning of the C++ code.
#include <iostream>
Following is the working code:
#include <iostream>
int main() {
std::cout << "data" << std::endl;
return 0;
}
Fix 2: For Microsoft’s compiler MSVC
If you are compiling C++ code on Windows using Microsoft’s compiler MSVC (Microsoft Visual Code), include the header file stdafx.h just before iostream.
stdafx.h is a precompiled header file which is used in Microsoft Visual Studio to keep track of files which have been compiled once and not changed following it. This is an optimization in MSVC to reduce compilation time. If it is not added, correct header files are not included in subsequent runs. If iostream header file is not included due to this issue, the compilation error will come.
Include these header files in the same order:
#include "stdafx.h"
#include <iostream>
Following is the working code:
#include "stdafx.h"
#include <iostream>
int main() {
std::cout << "data" << std::endl;
return 0;
}
Compile using MSVC.
With these fixes in this article at OpenGenus, the error must be resolved. Continue with your development.