Я только начал разбираться с плюсами. И тут же воткнулся в какой-то непонятный для меня косяк.
#include <iostream>
#include "stdafx.h"
using namespace std;
int main()
{
cout << "Hello World" << endl;
}
Вроде все верно… Но при компиляции ошибка:
cout: необъявленный идентификатор
Я почитал у вас тут похожие темы. Пишут, что область имен надо объявить, она есть. Вроде вообще все как надо. Я использую VS 2017 для написания кода. Объясните, пожалуйста, что не так.
αλεχολυτ♦
28.4k10 золотых знаков57 серебряных знаков118 бронзовых знаков
задан 8 сен 2017 в 8:21
0
Вот это
#include <iostream>
вы, вероятно, добавили сами перед
#include "stdafx.h"
Поменяйте их местами. stdafx.h
— это такой оченно хитрый вспомогательный заголовочный файл (который я бы, честно говоря, вообще не использовал — но вы уже сделали проект с использованием предварительно скомпилированного заголовка…) Пусть себе идет первым…
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
}
ответ дан 8 сен 2017 в 8:23
HarryHarry
215k15 золотых знаков117 серебряных знаков228 бронзовых знаков
2
попробуйте так
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World ";
return 0;
}
ответ дан 8 сен 2017 в 8:25
std::cout<<"Hello World";
insolor
46k16 золотых знаков54 серебряных знака95 бронзовых знаков
ответ дан 8 сен 2017 в 8:25
North FaceNorth Face
9301 золотой знак9 серебряных знаков25 бронзовых знаков
I ran across this error after just having installed vs 2010 and just trying to get a nearly identical program to work.
I’ve done vanilla C coding on unix-style boxes before, decided I’d play with this a bit myself.
The first program I tried was:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello World!";
return 0;
}
The big thing to notice here… if you’ve EVER done any C coding,
int _tmain(int argc, _TCHAR* argv[])
Looks weird. it should be:
int main( int argc, char ** argv )
In my case I just changed the program to:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world from VS 2010!n";
return 0;
}
And it worked fine.
Note: Use CTRL + F5 so that the console window sticks around so you can see the results.
yk92 0 / 0 / 2 Регистрация: 28.02.2010 Сообщений: 35 |
||||
1 |
||||
07.11.2010, 20:35. Показов 109545. Ответов 35 Метки нет (Все метки)
видаёт мне такую ошибку:
0 |
Programming Эксперт 94731 / 64177 / 26122 Регистрация: 12.04.2006 Сообщений: 116,782 |
07.11.2010, 20:35 |
35 |
Ignat 1260 / 798 / 108 Регистрация: 16.09.2009 Сообщений: 2,010 |
||||
07.11.2010, 22:38 |
2 |
|||
Надо сначала подключить Stdafx, а уже потом iostream, короче говоря поменять местами строки.
6 |
vaselo 19 / 19 / 5 Регистрация: 17.10.2010 Сообщений: 247 |
||||
07.11.2010, 23:40 |
3 |
|||
в вижуале он почему-то требует вот такого описания. Может ты еще и фигурную скобку не открыл?
0 |
M128K145 8381 / 3613 / 419 Регистрация: 03.07.2009 Сообщений: 10,708 |
||||
08.11.2010, 11:02 |
4 |
|||
vaselo, уже есть
а избыточность ни к чему. Правильный ответ во втором посте
0 |
Antariya 0 / 0 / 0 Регистрация: 08.06.2011 Сообщений: 4 |
||||||||
08.06.2011, 23:23 |
5 |
|||||||
А что делать в 10й висуал студо(экспресс)?
не помогает. (Либерти, упражнение 2, день 1)
0 |
593 / 531 / 76 Регистрация: 22.03.2011 Сообщений: 1,585 |
|
08.06.2011, 23:43 |
6 |
Antariya,
не помогает. а вот это странно
0 |
8381 / 3613 / 419 Регистрация: 03.07.2009 Сообщений: 10,708 |
|
09.06.2011, 00:13 |
7 |
Antariya, а мне кажется, что кто-то пытается нас обмануть. При том коде, который сейчас должны вылетать две ошибки:
0 |
0 / 0 / 0 Регистрация: 08.06.2011 Сообщений: 4 |
|
09.06.2011, 12:40 |
8 |
OstapBender, именно как в книге написала. Попробовала исправить.
0 |
3352 / 2611 / 319 Регистрация: 11.03.2009 Сообщений: 5,953 |
|
09.06.2011, 12:49 |
9 |
Таки сложно следить за изменениями, было бы лучше выкладывать текуший вариант программы вместе с его ошибками.
0 |
Antariya 0 / 0 / 0 Регистрация: 08.06.2011 Сообщений: 4 |
||||
09.06.2011, 12:51 |
10 |
|||
kazak, А. Точно. Извиняюсь.
warning C4067: непредвиденные лексемы за директивой препроцессора, требуется newline
0 |
diagon Higher 1953 / 1219 / 120 Регистрация: 02.05.2010 Сообщений: 2,925 Записей в блоге: 2 |
||||
09.06.2011, 12:53 |
11 |
|||
После #include <iostream> не надо точку с запятой
1 |
kazak 3352 / 2611 / 319 Регистрация: 11.03.2009 Сообщений: 5,953 |
||||||||
09.06.2011, 12:54 |
12 |
|||||||
endl в отдельности не используется, end вообще не существует.
1 |
0 / 0 / 0 Регистрация: 08.06.2011 Сообщений: 4 |
|
09.06.2011, 13:44 |
13 |
kazak, diagon, всё получилось, огромное спасибо.
0 |
Oleg35 0 / 0 / 0 Регистрация: 26.01.2011 Сообщений: 8 |
||||
30.10.2012, 19:41 |
14 |
|||
Здравствуйте, а можете мне помочь?
выдает (при отладке) (Урок 2- http://data.com1.ru/prog-schoo… esson2.mp4 Visual 08-Упрощенная(тоесть только для C++)
0 |
M128K145 8381 / 3613 / 419 Регистрация: 03.07.2009 Сообщений: 10,708 |
||||
30.10.2012, 20:56 |
15 |
|||
Oleg35, используйте std::cout, std::cin и std::endl или после инклудов напишите
Первый вариант предпочтительнее
0 |
Мой лучший друг-отладчик! 167 / 167 / 30 Регистрация: 24.06.2012 Сообщений: 662 Записей в блоге: 5 |
|
30.10.2012, 22:09 |
16 |
M128K145, в ходе обучения использование using namespace std; вместо std:: способствует, как мне кажется, лучшему восприятию кода.И на ранних этапах обучения программированию использвание пространства предпочтительнее. Но с другой стороны, в профессиональном программировании, насколько я знаю, юзать нужно только std::.Мне тут все модеры это твердили.И уже за собой тоже заметил — постоянно пишу std:: вместо namespace
0 |
8381 / 3613 / 419 Регистрация: 03.07.2009 Сообщений: 10,708 |
|
30.10.2012, 23:20 |
17 |
M128K145, в ходе обучения использование using namespace std; вместо std:: способствует, как мне кажется, лучшему восприятию кода.И на ранних этапах обучения программированию использвание пространства предпочтительнее. если постоянно привыкать спать на потолке(ну как начинающий), то со временем вы уже с трудом сможете переучится спать как и все люди — на диване, который стоит на полу и то, если захочется
0 |
0 / 0 / 0 Регистрация: 26.01.2011 Сообщений: 8 |
|
31.10.2012, 15:09 |
18 |
Вставил не помогло, теперь выдает это
0 |
Мой лучший друг-отладчик! 167 / 167 / 30 Регистрация: 24.06.2012 Сообщений: 662 Записей в блоге: 5 |
|
31.10.2012, 15:14 |
19 |
Нет в С++ оператора end!!!Есть endl.
1 |
0 / 0 / 0 Регистрация: 26.01.2011 Сообщений: 8 |
|
31.10.2012, 15:21 |
20 |
Ура, спасибо большое. Вот оказывается где собака была зарыта.
0 |
description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
Learn more about: Compiler Error C2065 |
Compiler Error C2065 |
06/29/2022 |
C2065 |
C2065 |
78093376-acb7-45f5-9323-5ed7e0aab1dc |
Compiler Error C2065
‘identifier‘ : undeclared identifier
The compiler can’t find the declaration for an identifier. There are many possible causes for this error. The most common causes of C2065 are that the identifier hasn’t been declared, the identifier is misspelled, the header where the identifier is declared isn’t included in the file, or the identifier is missing a scope qualifier, for example, cout
instead of std::cout
. For more information on declarations in C++, see Declarations and Definitions (C++).
Here are some common issues and solutions in greater detail.
The identifier is undeclared
If the identifier is a variable or a function name, you must declare it before it can be used. A function declaration must also include the types of its parameters before the function can be used. If the variable is declared using auto
, the compiler must be able to infer the type from its initializer.
If the identifier is a member of a class or struct, or declared in a namespace, it must be qualified by the class or struct name, or the namespace name, when used outside the struct, class, or namespace scope. Alternatively, the namespace must be brought into scope by a using
directive such as using namespace std;
, or the member name must be brought into scope by a using
declaration, such as using std::string;
. Otherwise, the unqualified name is considered to be an undeclared identifier in the current scope.
If the identifier is the tag for a user-defined type, for example, a class
or struct
, the type of the tag must be declared before it can be used. For example, the declaration struct SomeStruct { /*...*/ };
must exist before you can declare a variable SomeStruct myStruct;
in your code.
If the identifier is a type alias, the type must be declared by a using
declaration or typedef
before it can be used. For example, you must declare using my_flags = std::ios_base::fmtflags;
before you can use my_flags
as a type alias for std::ios_base::fmtflags
.
Example: misspelled identifier
This error commonly occurs when the identifier name is misspelled, or the identifier uses the wrong uppercase and lowercase letters. The name in the declaration must exactly match the name you use.
// C2065_spell.cpp // compile with: cl /EHsc C2065_spell.cpp #include <iostream> using namespace std; int main() { int someIdentifier = 42; cout << "Some Identifier: " << SomeIdentifier << endl; // C2065: 'SomeIdentifier': undeclared identifier // To fix, correct the spelling: // cout << "Some Identifier: " << someIdentifier << endl; }
Example: use an unscoped identifier
This error can occur if your identifier isn’t properly scoped. If you see C2065 when you use cout
, a scope issue is the cause. When C++ Standard Library functions and operators aren’t fully qualified by namespace, or you haven’t brought the std
namespace into the current scope by using a using
directive, the compiler can’t find them. To fix this issue, you must either fully qualify the identifier names, or specify the namespace with the using
directive.
This example fails to compile because cout
and endl
are defined in the std
namespace:
// C2065_scope.cpp // compile with: cl /EHsc C2065_scope.cpp #include <iostream> // using namespace std; // Uncomment this line to fix int main() { cout << "Hello" << endl; // C2065 'cout': undeclared identifier // C2065 'endl': undeclared identifier // Or try the following line instead std::cout << "Hello" << std::endl; }
Identifiers that are declared inside of class
, struct
, or enum class
types must also be qualified by the name of their enclosing scope when you use them outside of that scope.
Example: precompiled header isn’t first
This error can occur if you put any preprocessor directives, such as #include
, #define
, or #pragma
, before the #include
of a precompiled header file. If your source file uses a precompiled header file (that is, if it’s compiled by using the /Yu
compiler option) then all preprocessor directives before the precompiled header file are ignored.
This example fails to compile because cout
and endl
are defined in the <iostream>
header, which is ignored because it’s included before the precompiled header file. To build this example, create all three files, then compile pch.h
(some versions of Visual Studio use stdafx.cpp
), then compile C2065_pch.cpp
.
// pch.h (stdafx.h in Visual Studio 2017 and earlier) #include <stdio.h>
The pch.h
or stdafx.h
source file:
// pch.cpp (stdafx.cpp in Visual Studio 2017 and earlier) // Compile by using: cl /EHsc /W4 /c /Ycstdafx.h stdafx.cpp #include "pch.h"
Source file C2065_pch.cpp
:
// C2065_pch.cpp // compile with: cl /EHsc /W4 /Yustdafx.h C2065_pch.cpp #include <iostream> #include "stdafx.h" using namespace std; int main() { cout << "Hello" << endl; // C2065 'cout': undeclared identifier // C2065 'endl': undeclared identifier }
To fix this issue, add the #include of <iostream>
into the precompiled header file, or move it after the precompiled header file is included in your source file.
Example: missing header file
The error can occur if you haven’t included the header file that declares the identifier. Make sure the file that contains the declaration for the identifier is included in every source file that uses it.
// C2065_header.cpp // compile with: cl /EHsc C2065_header.cpp //#include <stdio.h> int main() { fpos_t file_position = 42; // C2065: 'fpos_t': undeclared identifier // To fix, uncomment the #include <stdio.h> line // to include the header where fpos_t is defined }
Another possible cause is if you use an initializer list without including the <initializer_list> header.
// C2065_initializer.cpp // compile with: cl /EHsc C2065_initializer.cpp // #include <initializer_list> int main() { for (auto strList : {"hello", "world"}) if (strList == "hello") // C2065: 'strList': undeclared identifier return 1; // To fix, uncomment the #include <initializer_list> line }
You may see this error in Windows Desktop app source files if you define VC_EXTRALEAN
, WIN32_LEAN_AND_MEAN
, or WIN32_EXTRA_LEAN
. These preprocessor macros exclude some header files from windows.h
and afxv_w32.h
to speed compiles. Look in windows.h
and afxv_w32.h
for an up-to-date description of what’s excluded.
Example: missing closing quote
This error can occur if you’re missing a closing quote after a string constant. It’s an easy way to confuse the compiler. The missing closing quote may be several lines before the reported error location.
// C2065_quote.cpp // compile with: cl /EHsc C2065_quote.cpp #include <iostream> int main() { // Fix this issue by adding the closing quote to "Aaaa" char * first = "Aaaa, * last = "Zeee"; std::cout << "Name: " << first << " " << last << std::endl; // C2065: 'last': undeclared identifier }
Example: use iterator outside for loop scope
This error can occur if you declare an iterator variable in a for
loop, and then you try to use that iterator variable outside the scope of the for
loop. The compiler enables the /Zc:forScope
compiler option by default. For more information, see Debug iterator support.
// C2065_iter.cpp // compile with: cl /EHsc C2065_iter.cpp #include <iostream> #include <string> int main() { // char last = '!'; std::string letters{ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; for (const char& c : letters) { if ('Q' == c) { std::cout << "Found Q!" << std::endl; } // last = c; } std::cout << "Last letter was " << c << std::endl; // C2065 // Fix by using a variable declared in an outer scope. // Uncomment the lines that declare and use 'last' for an example. // std::cout << "Last letter was " << last << std::endl; // C2065 }
Example: preprocessor removed declaration
This error can occur if you refer to a function or variable that is in conditionally compiled code that isn’t compiled for your current configuration. The error can also occur if you call a function in a header file that currently isn’t supported in your build environment. If certain variables or functions are only available when a particular preprocessor macro is defined, make sure the code that calls those functions can only be compiled when the same preprocessor macro is defined. This issue is easy to spot in the IDE: The declaration for the function is greyed out if the required preprocessor macros aren’t defined for the current build configuration.
Here’s an example of code that works when you build in Debug, but not Release:
// C2065_defined.cpp // Compile with: cl /EHsc /W4 /MT C2065_defined.cpp #include <iostream> #include <crtdbg.h> #ifdef _DEBUG _CrtMemState oldstate; #endif int main() { _CrtMemDumpStatistics(&oldstate); std::cout << "Total count " << oldstate.lTotalCount; // C2065 // Fix by guarding references the same way as the declaration: // #ifdef _DEBUG // std::cout << "Total count " << oldstate.lTotalCount; // #endif }
Example: C++/CLI type deduction failure
This error can occur when calling a generic function, if the intended type argument can’t be deduced from the parameters used. For more information, see Generic Functions (C++/CLI).
// C2065_b.cpp // compile with: cl /clr C2065_b.cpp generic <typename ItemType> void G(int i) {} int main() { // global generic function call G<T>(10); // C2065 G<int>(10); // OK - fix with a specific type argument }
Example: C++/CLI attribute parameters
This error can also be generated as a result of compiler conformance work that was done for Visual Studio 2005: parameter checking for Visual C++ attributes.
// C2065_attributes.cpp // compile with: cl /c /clr C2065_attributes.cpp [module(DLL, name=MyLibrary)]; // C2065 // try the following line instead // [module(dll, name="MyLibrary")]; [export] struct MyStruct { int i; };
- Remove From My Forums
-
Question
-
Im having trouble building a programme when i do it fails and says this:
error C2065: ‘cout’ : undeclared identifier
I built a programme before using «cout» which works fine called test here it is
/*
this is a test
*/
#include «StdAfx.h»#include <iostream>
using namespace std;int main()
{
cout << «hi this is a test «
;system («pause»);;return 0;
}that one works well it is a CLR console application.
i am using microsoft visual basic 2010
the programme that doesnt work and gets the error
error C2065: ‘cout’ : undeclared identifier
i think is a win32 application
i am new to C++ and have just started learning,can anyone help?
Answers
-
Hi,
In Visual Studio there are two kinds of *.exe binaries, windows applications and console applications. A console application is has a single console window where all input, output and errors are displayed. While a windows application usually presents a window
and interacts with the user using the Microsoft GUI.For developers, the main difference between Windows Applications and Console Applications is that the former’s entry point is WinMain() or wWinMain(), while the latter’s entry point is main() or wmain().
We can use cout in console applications to output strings to the console window. While in Windows applications, cout cannot be used and we can use other APIs such as TextOut instead to output on the window.
So please separate console applications from windows applications. It is not proper for us to mix the two types of applications together.
I hope this reply is helpful to you.
Best regards,
Helen Zhao [MSFT]
MSDN Community Support | Feedback to us
-
Marked as answer by
Monday, November 28, 2011 9:03 PM
-
Marked as answer by