473,491 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Class, array inside constructor.

7 New Member
I'm creating an array inside the constructor of my class. Looks like:
Expand|Select|Wrap|Line Numbers
  1. class SPR_ANIM
  2. {
  3.     public:
  4.     int index;
  5.     SPR_ANIM::SPR_ANIM(int num);
  6.     void Draw();
  7. };
  8.  
  9. SPR_ANIM::SPR_ANIM(int num)
  10. {
  11.     Sprite sprite[num];
  12.     index = 0;
  13. }
  14.  
  15. void SPR_ANIM::Draw()
  16. {
  17.     App.Draw(sprite[index]);
  18. }
This ends up with the array "sprite" undeclared. I don't know how to declare it outside the constructor when im using the amount of num.
Oct 15 '07 #1
4 2026
ashitpro
542 Recognized Expert Contributor
I'm creating an array inside the constructor of my class. Looks like:
Expand|Select|Wrap|Line Numbers
  1. class SPR_ANIM
  2. {
  3.     public:
  4.     int index;
  5.     SPR_ANIM::SPR_ANIM(int num);
  6.     void Draw();
  7. };
  8.  
  9. SPR_ANIM::SPR_ANIM(int num)
  10. {
  11.     Sprite sprite[num];
  12.     index = 0;
  13. }
  14.  
  15. void SPR_ANIM::Draw()
  16. {
  17.     App.Draw(sprite[index]);
  18. }
This ends up with the array "sprite" undeclared. I don't know how to declare it outside the constructor when im using the amount of num.
compiler is giving the error that "Sprite" is not declared
you are saying that "sprite" is not decalred
Here compiler is not able to find class "Sprite". (Its obvious,I don't see any class named "Sprite" in your code)
Oct 15 '07 #2
Ganon11
3,652 Recognized Expert Specialist
Plus, I don't see a data member in your class to hold this. If you'd like to use this array throughout the class, make it a private data member.
Oct 15 '07 #3
Saile
7 New Member
The compiler says that 'sprite' not 'Sprite' is undeclared. The class Sprite is declared in a header, I didn't show the whole code I have. The problem is that when I declare 'sprite' I do it in the constructor which means that it's temporary in that function? And then when i try to draw it within the funtion Draw it can't find 'sprite' anywhere. I need to declare 'sprite' as a global array with a specific number that you can choose with the constructor's parameters. Shall I create a global pointer that points at the array?
Oct 19 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
NO to global pointers.

Create a pointer in you class (private for perference) of type Sprite *. Then in your constructor new an array of Sprint of the right size and in the destructor delete it again.

Since you have pointers in your class you will need to implement a copy constructor and an assignment operator to ensure the data is copied correctly.
Oct 19 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
13262
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is...
13
2348
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
1
2994
by: Piotr Sawuk | last post by:
just a quick question out of curiosity: how to initialize const arrays? struct srat { long num; ulong den; srat(){} } struct collisions
5
14405
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
17
2193
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
8
429
by: Malciah | last post by:
I posted this on another site, but so far I've had no answers. So, I decided to try it here. -------------------------------------------------------- I've been learning C++ for about 6 weeks now,...
3
2288
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
11
2417
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at...
4
3410
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.