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 -
class Missile{
-
private:
-
-
bool isHuman;
-
int xPos = 0;
-
int yPos = 0;
-
-
public:
-
Missile(){
-
-
}
-
-
Missile(int x, int y, bool isPlayer){
-
xPos = x;
-
yPos = y;
-
isHuman = isPlayer;
-
}
-
//Other Methods not shown
-
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: -
void fire(){
-
Missile *missile = new Missile(xPos, yPos, true);
-
(*missile).setShotXPos(xPos);
-
missiles.add(missile);
-
-
for (int i = 0; i < missiles.getSize(); i++){
-
cout << "Player XPos:" << xPos << endl;
-
cout << "Missile " << i << " XPos: " << (*missiles.get(0)).getMissileXPos() << endl;
-
}
-
missile = nullptr;
-
}
-
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
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: -
-
Missile *missile = new Missile(xPos, yPos, true);//Something went wrong here
-
(*missile).setShotXPos(xPos);
-
missiles.add(missile); //here
-
missile = nullptr; //and here
-
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. -
Missile *missile = new Missile(xPos, yPos, true);
-
missiles.add((*missile));
-
delete missile;
-
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 - 5: error: ISO C++ forbids initialization of member 'xPos'
-
5: error: making 'xPos' static
-
5: error: ISO C++ forbids in-class initialization of non-const static member 'xPos'
-
6: error: ISO C++ forbids initialization of member 'yPos'
-
6: error: making 'yPos' static
-
6: error: ISO C++ forbids in-class initialization of non-const static member 'yPos'
The error is here: - void fire(){
-
Missile *missile = new Missile(xPos, yPos, true);
-
(*missile).setShotXPos(xPos);
-
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.
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: -
-
Missile *missile = new Missile(xPos, yPos, true);//Something went wrong here
-
(*missile).setShotXPos(xPos);
-
missiles.add(missile); //here
-
missile = nullptr; //and here
-
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. -
Missile *missile = new Missile(xPos, yPos, true);
-
missiles.add((*missile));
-
delete missile;
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
|
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....
|
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...
|
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.,...
|
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;
};
...
|
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
|
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 {
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |