-
April 25th, 2013, 07:11 AM
#1
[RESOLVED] problems on resize an array:(
i have 1 class with several class's and 1 struture:
Code:
struct Images
{
HBITMAP ImageImage;
BITMAP Imagebm;
HDC ImagehdcMem;
HBITMAP ImageMaskImage;
BITMAP ImageMaskbm;
HDC ImageMaskhdcMem;
};
//the subclass
class subimages
{
public: int ActualSubimage;
public: int TotalSubimages;
public: struct Images *SubImages;
....................................
//how i use it:
..................
sprite2 a,b,c;
a.Visible =true;
a.Direction=Left;
a.Images.Image1.Stop.SubImages=(struct Images)malloc(1 * sizeof(struct Images));
....................
but i get these error:
"--------------------Configuration: Sprite2 - Win32 Debug--------------------
Compiling...
Test Sprite2.cpp
C:\Users\Joaquim\Documents\Visual C 98\Sprite2\Test Sprite2.cpp(23) : error C2440: 'type cast' : cannot convert from 'void *' to 'struct Images'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
Sprite2.exe - 1 error(s), 0 warning(s)"
i try search more code, but i get more errors
can anyone advice me?
-
April 25th, 2013, 07:43 AM
#2
Re: problems on resize an array:(
Why are you using malloc instead of new?
Why are you multiplying by 1?
Most importantly, malloc returns a pointer. Why are you casting it to a struct?
-
April 25th, 2013, 08:22 AM
#3
Re: problems on resize an array:(
Originally Posted by GCDEF
Why are you using malloc instead of new?
Why are you multiplying by 1?
Most importantly, malloc returns a pointer. Why are you casting it to a struct?
you have right the 'new' is more easy to use, but i get errors too
by 1, because, in these test, i only need 1 subimage
Code:
a.Images.Image1.Stop.SubImages= new Images[1];
i execute the program but the windows give me an errror and then close it(sorry not a normal error message). and in subclass i have 1 pointer like you see that code.
i'm trying but seems that is more complicaded with struct that normal types
-
April 25th, 2013, 08:52 AM
#4
Re: problems on resize an array:(
What is a.Images.Image1.Stop.SubImages?
What's the point of creating an array of size 1?
Your code seems pretty confused.
-
April 25th, 2013, 09:20 AM
#5
Re: problems on resize an array:(
Originally Posted by GCDEF
What is a.Images.Image1.Stop.SubImages?
What's the point of creating an array of size 1?
Your code seems pretty confused.
sorry about that.. i will try do my best(i'm portuguese)
i'm doing a class Sprite these class have several subclass's for be more easy to use
sprite a;
a.Images.Image1.Stop.SubImages....;
the SubImages word is 1 variable from a struture:
Code:
struct Images
{
HBITMAP ImageImage;
BITMAP Imagebm;
HDC ImagehdcMem;
HBITMAP ImageMaskImage;
BITMAP ImageMaskbm;
HDC ImageMaskhdcMem;
};
like you see the SubImages is a Images type\structure. but i need do a dinamic array, because not all animations have 100 subimages. and a good programmer don't use so many memory, if he don't need
i try use the 'new' keyword(from C++) and malloc() function(from C), but without sucess. maybe because i'm using a struture.
i can share all the code
please tell me something for i start see\read. and loking for some code it's very complicated, because theres several C\C++ versions and not all code works on Visual C++ 6(i have the 2010 too).
sorry if my english is bad, but i'm trying
Last edited by Cambalinho; April 25th, 2013 at 09:25 AM.
-
April 25th, 2013, 09:43 AM
#6
Re: problems on resize an array:(
Originally Posted by Cambalinho
sorry about that.. i will try do my best(i'm portuguese)
i'm doing a class Sprite these class have several subclass's for be more easy to use
sprite a;
a.Images.Image1.Stop.SubImages....;
the SubImages word is 1 variable from a struture:
Code:
struct Images
{
HBITMAP ImageImage;
BITMAP Imagebm;
HDC ImagehdcMem;
HBITMAP ImageMaskImage;
BITMAP ImageMaskbm;
HDC ImageMaskhdcMem;
};
like you see the SubImages is a Images type\structure. but i need do a dinamic array, because not all animations have 100 subimages. and a good programmer don't use so many memory, if he don't need
i try use the 'new' keyword(from C++) and malloc() function(from C), but without sucess. maybe because i'm using a struture.
i can share all the code
please tell me something for i start see\read. and loking for some code it's very complicated, because theres several C\C++ versions and not all code works on Visual C++ 6(i have the 2010 too).
sorry if my english is bad, but i'm trying
Your English is fine. I'd get away from VC 6 and focus on 2010. Then you can take advantage of STL and dynamic arrays such as vector. If you're using MFC, even in VC 6 you can use their dynamic arrays such as CPtrArray or CObArray.
FWIW, it's not necessarily true that you don't use more memory than you need. An fixed array of size 100 that just stores pointers, even if you don't use them all, isn't really an excessive use of memory.
-
April 25th, 2013, 09:34 AM
#7
Re: problems on resize an array:(
i execute the program but the windows give me an errror and then close it(sorry not a normal error message).
What error? What debugging of the program have you done to find out what is the cause of the error? Which line in the program is giving the error? If you want our help you'll need to provide more info than just 'gives an error' and also you'll need to post the code - not just a few lines - so that we can look at it (properly formatted with code tags please).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 25th, 2013, 10:26 AM
#8
Re: problems on resize an array:(
Originally Posted by Cambalinho
i have 1 class with several class's and 1 struture:
Code:
struct Images
{
HBITMAP ImageImage;
BITMAP Imagebm;
HDC ImagehdcMem;
HBITMAP ImageMaskImage;
BITMAP ImageMaskbm;
HDC ImageMaskhdcMem;
};
//the subclass
class subimages
{
public: int ActualSubimage;
public: int TotalSubimages;
public: struct Images *SubImages;
....................................
//how i use it:
..................
sprite2 a,b,c;
a.Visible =true;
a.Direction=Left;
a.Images.Image1.Stop.SubImages=(struct Images)malloc(1 * sizeof(struct Images));
....................
but i get these error:
"--------------------Configuration: Sprite2 - Win32 Debug--------------------
Compiling...
Test Sprite2.cpp
C:\Users\Joaquim\Documents\Visual C 98\Sprite2\Test Sprite2.cpp(23) : error C2440: 'type cast' : cannot convert from 'void *' to 'struct Images'
No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.
Sprite2.exe - 1 error(s), 0 warning(s)"
i try search more code, but i get more errors
can anyone advice me?
The biggest advice is to improve your C++ skills. Regardless of what you want to do, you can't throw together a program in C++ that requires knowledge beyond the basics and have it work.
First, I would suggest renaming your struct. You called it "Images" when it really can be only one "Image".
Second:
Code:
#include <vector>
#include <memory>
struct ImageInfo
{
HBITMAP ImageImage;
BITMAP Imagebm;
HDC ImagehdcMem;
HBITMAP ImageMaskImage;
BITMAP ImageMaskbm;
HDC ImageMaskhdcMem;
};
typedef std::shared_ptr<ImageInfo> ImageInfoPtr;
typedef std::vector<ImageInfoPtr> ImageInfoPtrVect;
So you now have two things here:
1) You have a smart pointer of ImageInfo called ImageInfoPtr.
2) You can now store multiple ImageInfoPtr into a vector of type ImageInfoPtrVect. This will be the dynamic array.
Code:
ImageInfoPtrVect imVector;
ImageInfoPtr imPtr = ImageInfoPtr(new ImageInfo);
//..now assume imPtr has been set up with its information
//... add it to our vector
imVector.push_back( imPtr );
So what does this give you?
1) You now have a dynamic array of Images
2) The memory management is controlled by the shared_ptr. When the last reference is gone, the memory manager takes care of deleting the memory.
For 2), you may want to use unique_ptr instead of shared_ptr for your purposes. But regardless, this is how an experienced C++ programmer would approach this issue -- that is to use the standard library, containers, and smart pointers to simplify the work. Approaching the problem as if it's low-level 'C' programming as you did in your attempts is not the way to go about this.
Regards,
Paul McKenzie
-
April 25th, 2013, 10:38 AM
#9
Re: problems on resize an array:(
Originally Posted by Paul McKenzie
The biggest advice is to improve your C++ skills. Regardless of what you want to do, you can't throw together a program in C++ that requires knowledge beyond the basics and have it work.
First, I would suggest renaming your struct. You called it "Images" when it really can be only one "Image".
Second:
Code:
#include <vector>
#include <memory>
struct ImageInfo
{
HBITMAP ImageImage;
BITMAP Imagebm;
HDC ImagehdcMem;
HBITMAP ImageMaskImage;
BITMAP ImageMaskbm;
HDC ImageMaskhdcMem;
};
typedef std::shared_ptr<ImageInfo> ImageInfoPtr;
typedef std::vector<ImageInfoPtr> ImageInfoPtrVect;
So you now have two things here:
1) You have a smart pointer of ImageInfo called ImageInfoPtr.
2) You can now store multiple ImageInfoPtr into a vector of type ImageInfoPtrVect. This will be the dynamic array.
Code:
ImageInfoPtrVect imVector;
ImageInfoPtr imPtr = ImageInfoPtr(new ImageInfo);
//..now assume imPtr has been set up with its information
//... add it to our vector
imVector.push_back( imPtr );
So what does this give you?
1) You now have a dynamic array of Images
2) The memory management is controlled by the shared_ptr. When the last reference is gone, the memory manager takes care of deleting the memory.
For 2), you may want to use unique_ptr instead of shared_ptr for your purposes. But regardless, this is how an experienced C++ programmer would approach this issue -- that is to use the standard library, containers, and smart pointers to simplify the work. Approaching the problem as if it's low-level 'C' programming as you did in your attempts is not the way to go about this.
Regards,
Paul McKenzie
sorry i'm learning C by me... then i will learn C++
sorry can you give me a nice link about these for i understand more?
thanks for all
-
April 25th, 2013, 11:05 AM
#10
Re: problems on resize an array:(
Originally Posted by Cambalinho
sorry i'm learning C by me...
Then you should be using a 'C' compiler only. You are mistakingly using a C++ compiler, and using a C++ compiler that uses C++ rules to learn 'C' programming is totally wrong.
Sprite2.cpp
//...
class subimages
This is in your first post. This means that the program is a C++ program. If you're using Visual C++, and you haven't changed any of the IDE options, any file with a .cpp extension is a C++ program. In addition, there is no such thing as a class in the 'C' language.
Either rename your file with a .C extension, or force Visual Studio to apply the rules of 'C' to your program.
then i will learn C++
If your goal is to learn C++ programming, learning 'C' is not a requirement. Either learn C, or learn C++.
http://www.parashift.com/c++-faq/don...n-c-first.html
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 25th, 2013 at 11:09 AM.
-
April 25th, 2013, 11:43 AM
#11
Re: problems on resize an array:(
You are not casting the return value from malloc to the correct type:
Code:
a.Images.Image1.Stop.SubImages=(struct Images *)malloc(1 * sizeof(struct Images));
-
April 25th, 2013, 01:02 PM
#12
Re: problems on resize an array:(
Originally Posted by Philip Nicoletti
You are not casting the return value from malloc to the correct type:
Code:
a.Images.Image1.Stop.SubImages=(struct Images *)malloc(1 * sizeof(struct Images));
sorry i'm trying do these thing, but now i have a head pain
and i, always, get errors
-
April 25th, 2013, 01:00 PM
#13
Re: problems on resize an array:(
Originally Posted by Paul McKenzie
Then you should be using a 'C' compiler only. You are mistakingly using a C++ compiler, and using a C++ compiler that uses C++ rules to learn 'C' programming is totally wrong.
This is in your first post. This means that the program is a C++ program. If you're using Visual C++, and you haven't changed any of the IDE options, any file with a .cpp extension is a C++ program. In addition, there is no such thing as a class in the 'C' language.
Either rename your file with a .C extension, or force Visual Studio to apply the rules of 'C' to your program.
If your goal is to learn C++ programming, learning 'C' is not a requirement. Either learn C, or learn C++.
http://www.parashift.com/c++-faq/don...n-c-first.html
Regards,
Paul McKenzie
thanks i didn't knew
more quickly i start for C++
-
April 25th, 2013, 04:48 PM
#14
Re: problems on resize an array:(
Code:
int ReDim(void *ag,size_t i)
{
(typeid(ag).name()*) realloc (ag, i * sizeof(typeid(ag).name()));
return 0;
}
Whatever you thought this code was going to accomplish, it doesn't!
realloc returns a pointer to the new memory and needs to be assigned. ag is a pointer to void passed by value so that even if ag is assigned to the return value of realloc, the new value won't get returned to the caller. ag either needs to be passed as a pointer to a pointer or (better) as a reference to a pointer. typeid(..).name() returns a const char * string so sizeof will always give the wrong value (and in any case doesn't provide what you think it provides!).
As Paul already stated, why are you trying to incorrectly use the 'c' method of memory allocation when there are so much better ways of doing this in c++???? If you insist on using malloc/realloc/free then you need a good understanding of how this all hangs together and how pointers work and how to pass values back from functions etc etc etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|