Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Link

what do i wrong?

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!