473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Making new Class Instance and adding to linked list

I have a .cpp file which contains 5 smaller defined classes. In my missile class I have a default constructor and a constructor that I invoke
Expand|Select|Wrap|Line Numbers
  1. class Missile{
  2. private:
  3.  
  4.     bool isHuman;
  5.     int xPos = 0;
  6.     int yPos = 0;
  7.  
  8. public:
  9.     Missile(){
  10.  
  11.     }
  12.  
  13.     Missile(int x, int y, bool isPlayer){
  14.         xPos = x;
  15.         yPos = y;
  16.         isHuman = isPlayer;
  17.     }
  18. //Other Methods not shown
  19.  
In my player class, I want to create a new Missile object whenever the player presses the "fire" button.
The method to create the new object is below:
Expand|Select|Wrap|Line Numbers
  1.     void fire(){
  2.         Missile *missile = new Missile(xPos, yPos, true);
  3.         (*missile).setShotXPos(xPos);
  4.         missiles.add(missile);
  5.  
  6.         for (int i = 0; i < missiles.getSize(); i++){
  7.             cout << "Player XPos:" << xPos << endl;
  8.             cout << "Missile " << i << " XPos: " << (*missiles.get(0)).getMissileXPos() << endl;
  9.         }
  10.         missile = nullptr;
  11.     }
  12.  
My issue is when creating and adding the pointer object; it doesn't seem to create a new instance of the class-the Missile objects all share the same xPos value which is always the first xPos when the "fire" command is given. The "Missile *missile = new Missile(xPos, yPos, true);" line does not seem to create a new instance of the object with different variables but instead creates a new object with the same variables. Any reason as to why?
Is it necessary for me to always make a separate .cpp and .h file for any class I want to create multiple instances of or can I keep the smaller classes in the same file and still create a new separate instance of the class?
Thanks,
CodeNoob117
Oct 31 '13 #1

✓ answered by CodeNoob117

Thanks for the ideas! Though, when I did further debugging, I discovered the real problem; the issue wasn't creating new Missile objects- it was in the beginning of the fire() method:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Missile *missile = new Missile(xPos, yPos, true);//Something went wrong here
  3. (*missile).setShotXPos(xPos);
  4. missiles.add(missile); //here
  5. missile = nullptr; //and here
  6.  
Originally, I had used a linked list of pointers to point to the new Missiles that were being created and for some reason, the program kept adding the same pointer - it never changed where it pointed to but was making new Missiles that were not being used. I fixed the bug by making a linked list of Missile objects, and then deleting the pointer itself after adding its object to the list-instead of setting it to nullptr.
Expand|Select|Wrap|Line Numbers
  1. Missile *missile = new Missile(xPos, yPos, true);
  2. missiles.add((*missile));
  3. delete missile;
  4.  

3 2140
Banfa
9,065 Expert Mod 8TB
I think your error may be at lines 5 and 6. It is only valid to provide an initialiser like this for static const members.

In fact the code you have posted does not compile for me for this reason

Expand|Select|Wrap|Line Numbers
  1. 5: error: ISO C++ forbids initialization of member 'xPos'
  2. 5: error: making 'xPos' static
  3. 5: error: ISO C++ forbids in-class initialization of non-const static member 'xPos'
  4. 6: error: ISO C++ forbids initialization of member 'yPos'
  5. 6: error: making 'yPos' static
  6. 6: error: ISO C++ forbids in-class initialization of non-const static member 'yPos'
Oct 31 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
The error is here:
Expand|Select|Wrap|Line Numbers
  1.     void fire(){
  2.          Missile *missile = new Missile(xPos, yPos, true);
  3.          (*missile).setShotXPos(xPos);
  4. etc...
In the call to setShotXPos, the xPos argument is the value in the this object and not the one pointed at by missile.


The constructor of Missile has already set xPos. Therefore, xPos for the new Missile is already set.

You need to remove this call.
Oct 31 '13 #3
Thanks for the ideas! Though, when I did further debugging, I discovered the real problem; the issue wasn't creating new Missile objects- it was in the beginning of the fire() method:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Missile *missile = new Missile(xPos, yPos, true);//Something went wrong here
  3. (*missile).setShotXPos(xPos);
  4. missiles.add(missile); //here
  5. missile = nullptr; //and here
  6.  
Originally, I had used a linked list of pointers to point to the new Missiles that were being created and for some reason, the program kept adding the same pointer - it never changed where it pointed to but was making new Missiles that were not being used. I fixed the bug by making a linked list of Missile objects, and then deleting the pointer itself after adding its object to the list-instead of setting it to nullptr.
Expand|Select|Wrap|Line Numbers
  1. Missile *missile = new Missile(xPos, yPos, true);
  2. missiles.add((*missile));
  3. delete missile;
  4.  
Oct 31 '13 #4

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

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
2
by: saurabh | last post by:
solve a problem: define a Class(MyClassSArray a, b, c) which implements an array of string. But, in the backend it is actually implementing Linked List, so that memory is dynamically allocated....
6
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesnąt matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
0
by: Joseph Guildino | last post by:
Good afternoon all! I have this program in Python that is really starting to make my head go crazy. It utilizes linked lists (circularly linked list). Now, correct me if I am wrong, but a...
2
by: raamay | last post by:
i have been given an assignment to create a singly linked list which maintains order. but i am not sure whether to use a sorting method or to take care of the sorting while adding the values in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.