what do i wrong?

Started by
7 comments, last by Link 20 years, 2 months ago
i have a class and i declare a const in this way: #include "stdafx.h" #include "Form1.h" #include <windows.h> using namespace MAIN; const int MAX_NUMBER = 4; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA; Application::Run(new Form1()); return 0; } then i would to use this in a separate file: #pragma once using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Diagnostics; namespace MAIN { extern MAX_NUMBER; public __gc class myclass : public System::Windows::Forms::Form { public: bool myArray __nogc [ MAX_NUMBER ]; // error C2057: expected constant expression WHY? What do i wrong?
Advertisement
It''s not actually a constant, as it has link-time resolution.

You could try making it a static const int, which may or may not work. Or just make it a member of an anonymous enum, which makes it really constant and will work.
enum Bool { True, False, FileNotFound };
Sorry can you explain better this: It''s not actually a constant, as it has link-time resolution

why if i respect all the roles of my C++ do it not work or is not considered as a constant?

using enum what i must change?
Have you tried:

extern const int MAX_NUMBER;

Without the type, it probably defaults to ''int'', not ''const int''.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
Of course.
what does it happen is that if i move the mouse over MAX_NUMBER of "extern MAX_NUMBER" or "extern const int MAX_NUMBER"
i can see the helping write "const int MAX_NUMBER = 4;"
but if i move the mouse over MAX_NUMBER of "public: bool myArray __nogc [ MAX_NUMBER ];" the write doesn''t appear.
Why?
The value of the constant must be visible at compile-time from the point it is used - just like the code of an inline function.

Your IDE actually keeps a table of the symbols it has seen in your project and use it when you mouse over them. The compiler is an independent program that is not aware of that - it only processes one single file at a time.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on February 8, 2004 5:23:59 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ok but what is the remedy?
Either repeat the constant in each file where it is used or, which is strictly equivalent, but more maintainable, put it in a header file and #include that header in each file where the constant is used.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian W. Kernighan (C programming language co-inventor)
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Hey, just try this -- seems to work for me

static bool myArray __nogc [ MAX_NUMBER ];

This topic is closed to new replies.

Advertisement