problem with arrays in WinApi
Hello.
I'm new to winapi but I've read a book about it already. The problem is that my Visual Studio 2012 considers some of the stuff mentioned in my book as an error. For example:
Code:
Array^ arr = Array::CreateInstance(System::Int32::typeid, 10);
doesnt work.It asks me to put a semicolon between 'Array^' and 'arr',which also results in an error ofc.Also it says stuff like "Array must be a class or a namespace when followed by '::' ",CreateInstance is not a member of global namespace" and a ton of other issues.What's more:
Code:
array<int>^ arr = { 0, 1, 2 };
doesn't work as well. it says that " array is not a template ".Furthermore it seems to be completly unfamiliar with the gcnew operator:"error operator gcnew is unidentified".It makes me tired already :mad:
Additional question:
I want to make an array of objects of my own class.How can i make a definition of such an array without initializing it's components?
Thanks in advance; may the Compiler be with You.
Re: problem with arrays in WinApi
Quote:
Originally Posted by
Fides Facit Fortis
Hello.
I'm new to winapi but I've read a book about it already. The problem is that my Visual Studio 2012 considers some of the stuff mentioned in my book as an error.
It is an error because it is not ANSI C++ code.
The code you were attempting is the Microsoft extension of C++ geared toward .NET development. C++/CLI or Managed C++ is what you were using, and WinAPI is solely "traditional" ANSI C/C++.
So you need to get another book, one geared towards Windows API programming as the one you have now is not geared to Windows API, but to using Managed C++ and the .NET framework.
Regards,
Paul McKenzie
Re: problem with arrays in WinApi
For Windows API programming I would suggest
Programming Windows by Charles Petzold
http://www.amazon.co.uk/Programming-...ywords=petzold
and for MFC windows programming,
Programming Windows with MFC by Jeff Prosise
http://www.amazon.co.uk/Programming-...fc+programming
Re: problem with arrays in WinApi
... or, in case you actually do want to go the C++/CLI route, you'd need to create one of the CLR project types (preferably CLR Console Application or Windows Forms Application) instead of a Win32 one.
Also...
Quote:
Originally Posted by
Fides Facit Fortis
Code:
Array^ arr = Array::CreateInstance(System::Int32::typeid, 10);
What kind of book are you trying to learn C++/CLI from? :ehh: In case you'd get that to compile, it actually would create an array of ten integers, but that would then be extremely inconvenient to use. The usual, and practically much more useful way to create such an array is this:
Code:
array<int> ^arr = gcnew array<int>(10);
Quote:
Additional question:
I want to make an array of objects of my own class.How can i make a definition of such an array without initializing it's components?
You can simply declare the array without actually creating it:
Code:
array<YourClass ^> ^arr;
(The first one of the two ^s would or would not be required, depending on whether your class is a reference class or a value class.)
However, in case your class is a reference class, you could as well create the array using gcnew right away. Its items would then be initialized to nullptr, thereby not refering to any concrete instance of your class. You can later fill them with handles refering to instances of your class.
Finally, for discussions about C++/CLI there's a separate forum section: http://forums.codeguru.com/forumdisp...ed-C-and-C-CLI. In case you're actually going to use C++/CLI, please post further questions there.
Re: problem with arrays in WinApi
Wow,thx for a quick response.
Quote:
The code you were attempting is the Microsoft extension of C++ geared toward .NET development. C++/CLI or Managed C++ is what you were using, and WinAPI is solely "traditional" ANSI C/C++.
Yeah, this is propably due to the fact that my book describes both .NET and WinApi programming so I sometimes get lost.
I'll check this out,thanks.
Quote:
Code:
array<YourClass ^> ^arr;
Nope,doesnt work to me. Syntax error missing ';' before '<'. I'm propably doing sth wrong
Code:
class BarButton
{
...
};
array <BarButton^> bar; //doesn't work
array <BarButton^>^bar; //doesn't work as well
Thx for all the great responses, still need help with my objects array,though.
Re: problem with arrays in WinApi
Quote:
Originally Posted by
Fides Facit Fortis
Quote:
Code:
array<YourClass ^> ^arr;
Nope,doesnt work to me. Syntax error missing ';' before '<'. I'm propably doing sth wrong
It does work. ... provided, however, that it's in a module compiled with CLR support (which usually means it's part of one of the CLR type of projects) and YourClass is a managed class (denoted by either of the C++/CLI-specific keywords ref or value preceeding the class in the declaration), since a tracking handle (denoted by the ^) can't refer to a native object [EDIT: ... and it's at function or class member scope, see below].
Quote:
Code:
class BarButton
{
...
};
array <BarButton^> bar; //doesn't work
array <BarButton^>^bar; //doesn't work as well
Assuming this is at global or namespace scope, they don't work for two reasons: BarButton is not a managed class (see above) and global (as well as non-class-member static) variables of managed types (in this case the managed array) are not allowed (error C3145). The first one of the two array declarations also doesn't work because the managed array is a reference type, and that requires declaring variables of that type as tracking handles. The ^ I declared to be optional for a potential value type would be the one after BarButton in this case, not the one before bar.
Also, if you want a bar with buttons on it in a C++/CLI application, why not simply utilize Windows Forms and the ToolStrip and ToolStripButton classes? Writing something even remotely similar would be a vast lot of work you probably can't even imagine, and most certainly wouldn't want to take.
Re: problem with arrays in WinApi
Quote:
Also, if you want a bar with buttons on it in a C++/CLI application, why not simply utilize Windows Forms and the ToolStrip and ToolStripButton classes?
You see, the problem is that the bar I'm trying to implement needs to have custom properties.I want it to have similiar functionality to overlap buttons in browsers. Each one is responsible for one site.
But I also want them to close when I right-click them.All the rest has to rearange then so there are no empty spaces between them.I also want to be able to create a new overlap button each time I press a '+' button, like in Firefox, for example. Also I want to limit their number by up to ten. I know it's going to be hard but I've got some experience in programming and scripting, just not so much in WinApi :D. I'll try hard until I'll achieve desired effect.
Quote:
It does work. ... provided, however, that it's in a module compiled with CLR support (which usually means it's part of one of the CLR type of projects) and YourClass is a managed class (denoted by either of the C++/CLI-specific keywords ref or value preceeding the class in the declaration), since a tracking handle (denoted by the ^) can't refer to a native object
...
global (as well as non-class-member static) variables of managed types (in this case the managed array) are not allowed (error C3145)
So, all I have to do is to switch my project type to an empty CLR project,use ref keyword before class and make my array local?Then I can simply use
Code:
array <BarButton> ^bar;
?
I'll try it out when I'll have some time because I'm busy today.
Re: problem with arrays in WinApi
Quote:
Originally Posted by
Fides Facit Fortis
So, all I have to do is to switch my project type to an empty CLR project,use ref keyword before class and make my array local?Then I can simply use
Code:
array <BarButton> ^bar;
?
I'll try it out when I'll have some time because I'm busy today.
Then please stop posting to this thread.
And if you will need some help with your new managed C++/CLI project - start a new thread in the Managed C++ and C++/CLI forum.
Re: problem with arrays in WinApi
Quote:
Originally Posted by
VictorN
Then please stop posting to this thread.
And if you will need some help with your new managed C++/CLI project - start a new thread in the Managed C++ and C++/CLI forum.
Or, perhaps, some moderator can move this thread over there. Now it seems quite clear that it's C++/CLI what the OP is bound to. I'd really like to reply to post #7 in the C++/CLI section, but, though not impossible, it is much more tideous when doing it "remotely".
Re: problem with arrays in WinApi
Quote:
Originally Posted by
eri523
or, perhaps, some moderator can move this thread over there.
ok.
1 Attachment(s)
Re: problem with arrays in WinApi
Quote:
Originally Posted by
VictorN
ok.
Thanks. :thumb:
Quote:
Originally Posted by
Fides Facit Fortis
You see, the problem is that the bar I'm trying to implement needs to have custom properties.I want it to have similiar functionality to overlap buttons in browsers. Each one is responsible for one site.
I'm not quite sure what you're meaning by that description. Perhaps something like these?
Attachment 32425
The Windows Forms equivalent of this is the standard tab control.
Quote:
But I also want them to close when I right-click them.All the rest has to rearange then so there are no empty spaces between them.I also want to be able to create a new overlap button each time I press a '+' button, like in Firefox, for example. Also I want to limit their number by up to ten. I know it's going to be hard but I've got some experience in programming and scripting, just not so much in WinApi :D. I'll try hard until I'll achieve desired effect.
Except for the auto-arranging, none of what you're describing here is standard behaviour of the Windows Forms tab control. Yet I'd bet it's still way simpler to implement your custom behaviour based on the standard control, compared to implementing everything yourself from scratch.
If for some reason you don't want to use a tab control (e.g. because I simply misunderstood your description above): The tool strip features auto-arranging as well. And if that's still not what you want, you can go low-level and implement auto-arranging on any form you may make look like practically anything you like, using a flow layout panel.
Also, regarding terminology, note that we're way apart from Win API now.
Quote:
So, all I have to do is to switch my project type to an empty CLR project,use ref keyword before class and make my array local?Then I can simply use
Code:
array <BarButton> ^bar;
?
I'll try it out when I'll have some time because I'm busy today.
Basically yes, except:
- I wouldn't really recommend the empty project type, rather a Windows Forms project.
- You'd most probably want the array holding your buttons to persist longer than the runtime of a single function, so it likely would better be a class member than a local variable.
- GUI elements typically are reference objects, so the array declaration would rather need to be like this:
Code:
array<BarButton ^> ^bar;
And if you'd use any of the standard infrastructure I suggested above, you woudn't need that array any more anyway, since all the GUI items I suggested feature their own control collection for that purpose.
Re: problem with arrays in WinApi
Thx,you are my Guru :thumb: