|
Taras_Z 102 / 86 / 5 Регистрация: 27.10.2010 Сообщений: 534 Записей в блоге: 2 |
||||||||
|
1 |
||||||||
|
11.08.2012, 21:14. Показов 8804. Ответов 4 Метки нет (Все метки)
Пишу Win32 проект в VS 2010
но получается такая вот ошибка..
понять немогу в чём проблема..
0 |
|
113 / 113 / 42 Регистрация: 02.05.2012 Сообщений: 524 Записей в блоге: 1 |
|
|
11.08.2012, 21:15 |
2 |
|
видимость укажите
0 |
|
Taras_Z 102 / 86 / 5 Регистрация: 27.10.2010 Сообщений: 534 Записей в блоге: 2 |
||||
|
11.08.2012, 21:16 [ТС] |
3 |
|||
Добавлено через 34 секунды
0 |
|
113 / 113 / 42 Регистрация: 02.05.2012 Сообщений: 524 Записей в блоге: 1 |
|
|
11.08.2012, 21:16 |
4 |
|
выкладывайте тогда весь код
0 |
|
102 / 86 / 5 Регистрация: 27.10.2010 Сообщений: 534 Записей в блоге: 2 |
|
|
11.08.2012, 21:18 [ТС] |
5 |
|
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; };
While Trying to create a Menu to SubMenu using InsertMenuItem:
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_SUBMENU | MIIM_STRING | MIIM_ID;
mii.wID = uCmdID++;
mii.hSubMenu = hSubmenu;
mii.dwTypeData = _T("Net&Work Drive Solution");
// InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION | MF_POPUP,
// (UINT_PTR) hSubmenu, _T("NetWork Drive Solution") );
InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii );
I am getting error C2065: ‘MIIM_STRING’ : undeclared identifier
asked Sep 24, 2010 at 11:49
You must define WINVER to be at least 0x0500. MIIM_STRING is defined like this:
#if(WINVER >= 0x0500)
#define MIIM_STRING 0x00000040
...
answered Sep 24, 2010 at 12:01
RichieHindleRichieHindle
267k47 gold badges354 silver badges396 bronze badges
While Trying to create a Menu to SubMenu using InsertMenuItem:
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_SUBMENU | MIIM_STRING | MIIM_ID;
mii.wID = uCmdID++;
mii.hSubMenu = hSubmenu;
mii.dwTypeData = _T("Net&Work Drive Solution");
// InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION | MF_POPUP,
// (UINT_PTR) hSubmenu, _T("NetWork Drive Solution") );
InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii );
I am getting error C2065: ‘MIIM_STRING’ : undeclared identifier
asked Sep 24, 2010 at 11:49
You must define WINVER to be at least 0x0500. MIIM_STRING is defined like this:
#if(WINVER >= 0x0500)
#define MIIM_STRING 0x00000040
...
answered Sep 24, 2010 at 12:01
RichieHindleRichieHindle
267k47 gold badges354 silver badges396 bronze badges
Когда программирую на C++ в Visual Studio, появляется ошибка в операторах сout и cin. Компилятор пишет, что эти операторы не объявлены (undeclared identifier). Подскажите, пожалуйста, что делать?
Добавлено.
Все равно ничего не получается
#include <iostream>
#include "stdafx.h"
#include <cmath>
using namespace std;
int main(){
//объявление переменной:
double V,T,alpha;
//грави постоянная:
const double g=9.8;
//Ввод параметров:
cout<<"V = ";-здесь выводит ошибку
cin>>V;-и здесь
cout<<"T = ";
cin >>T;
alpha = asin(g*T/2*V);
cout<<"Значение угла альфа равно = "<<alpha;
return 0;
}
![]()
задан 27 сен 2011 в 18:30
3
Поменяй местами #include <iostream> и #include "stdafx.h" ("stdafx.h" должно стоять первым).
angry
8,63717 золотых знаков72 серебряных знака180 бронзовых знаков
ответ дан 29 сен 2011 в 10:50
Во-первых, надо было приводить версию среды, т.к. в Студии 2010 работает вполне нормально. В более старых версиях надо читать внимательно сообщения построения. Так, там наверняка должно быть сообщение типа «iostream: файл пропущен при поиске файла предкомпилированных заголовков», а именно — «stdafx.h». Поставьте #include "stdafx.h" в САМОМ НАЧАЛЕ, и да будет Вам счастье.
ответ дан 29 сен 2011 в 18:56
3JIoi_Hy63JIoi_Hy6
8926 серебряных знаков13 бронзовых знаков
Для правильной работы вам надо:
- Подключить библиотеку
iostream. - Воспользоваться конструкцией
using namespace stdдля разрешения области видимости.
Пример кода:
#include <iostream>
using namespace std;
int main() {
cout << "Hello std!" << endl;
return 0;
}
ответ дан 27 сен 2011 в 18:58
![]()
Nicolas Chabanovsky♦Nicolas Chabanovsky
50.5k80 золотых знаков257 серебряных знаков493 бронзовых знака
1
// Требуется создать в VS пустой проект
#include "stdafx.h"
#include "iostream"
#include <cmath>
#include <conio.h>
using namespace std;
int main(){
setlocale(0,"russian");
//объявление переменной:
double V,T,alpha;
//грави постоянная:
const double g=9.8;
//Ввод параметров:
cout << "V="; //-здесь выводит ошибку
cin >> V; //-и здесь
cout << "T=";
cin >> T;
alpha = asin(g*T/2*V);
cout << "Значение угла альфа равно = "<<alpha;
_getch();
}
Но лучше все это дело через std::cin и std::cout
ответ дан 29 сен 2011 в 8:11
johnfelixjohnfelix
1191 серебряный знак11 бронзовых знаков
2
строковый незаявленный идентификатор
Вопрос:
Я новичок в массивах, здесь я написал этот фрагмент кода, и я думал, что правильно сделал все синтаксис. Однако я получаю
C2065 ‘string’: необъявленный идентификатор
Как вы можете видеть, у меня установлены мои прототипы, и все же он все еще дает эту ошибку?
#include<iostream>
#include<string>
//Prototypes:
void getJars(string[], int[], int);
int getTotal(string[], int[], int);
using namespace std;
int main() {
string salsa[] = { "Mild", "Medium", "Sweet", "Hot", "Zesty" };
const int NUM = 5;
int jars[NUM];
getJars(salsa, jars, NUM);
getTotal(salsa, jars, NUM);
return 0;
}
void getJars(string salsa[], int jars[], int NUM) {
cout << "Salsa Sales Calulator...n";
cout << "------------------------n";
for (int i = 0; i < NUM; i++) {
cout << "Please Insert the Jar Amount for Each Salsa: ";
cout << salsa[i];
cout << " ";
cin >> jars[i];
cout << endl;
}
return;
}
int getTotal(string salsa[], int jars[], int NUM) {
int total;
for (int i = 0; i < NUM; i++) {
total += jars[i];
}
return total;
}
Я искал ошибку здесь, однако, решение, которое они придумали, заключалось в том, чтобы добавить прототипы, которые большинство людей забывает, но я не забыл и все еще дает ошибку? Может ли кто-нибудь помочь?
Лучший ответ:
Обмен этими заявлениями
void getJars(string[], int[], int);
int getTotal(string[], int[], int);
using namespace std;
Должно быть
using namespace std;
void getJars(string[], int[], int);
int getTotal(string[], int[], int);
Также инициализировать общую переменную
int getTotal(string salsa[], int jars[], int NUM) {
int total = 0;
^^^^^^^^^^^^^
for (int i = 0; i < NUM; i++) {
total += jars[i];
}
return total;
}
И обновите опечатку
cout << "Salsa Sales Calculator...n";
^^^^^^^^^^^
И вы можете удалить оператор return в функции getJars потому что функция имеет тип возвращаемого типа void и оператор return не требуется для этой функции.
Эта ошибка связана с отсутствием определения переделенной. Смотрим пример:
#include "stdafx.h"
int main(int argc, char* argv[])
{
for (int x=0;x<y;x++) //y не объявлена
{
}
return 0;
}
Переменная y не определена:
D:VСTestErrorTestError.cpp(12) : error C2065: 'y' : undeclared identifier
Основа этой ошибки в том, что переменную прежде чем использовать нужно объявить. А вот почему она не объявлена, может быть много случаев. Например, перепутаны верхние — нижние индексы в названиях.
#include "stdafx.h"
int main(int argc, char* argv[])
{
int X; // надо бы нижнем регистром
for (int x=0;x<y;x++)
{
}
return 0;
}
Отсутствие определения функции или переменной может быть связано с отсутствием заголовочного файла.
#include "stdafx.h"
int main(int argc, char* argv[])
{
BOOL i;
return 0;
}
Она просто не определена в заголовочном файле:
D:VСTestErrorTestError.cpp(11) : error C2065: 'BOOL' : undeclared identifier
Она просто не определена в заголовочном файле. Надо включить windows.h и все станет на свое место.
Область видимости переменной может привести к появлению этой ошибки.
#include "stdafx.h"
int main(int argc, char* argv[])
{
for (int x=0;x<10;x++)
{
for (int y=0;y<10;y++)
{
}
}
y=20;
return 0;
}
И та же ошибка:
D:VСTestErrorTestError.cpp(15) : error C2065: 'y' : undeclared identifier
