C++ :: Space Ship - Class Variable Resetting
Apr 9, 2014
I am creating a SpaceShip class, which has arrays for weapon names and damage, and variables for shields power, hull, engine power, and mass. I created two instances of the 'SpaceShip' class, 'ScoutShip', and 'Cruser'. When one of them calls the 'FireWeapons' function, it has the other call the 'TakeDamage' function, but for some reason the 'sheildPower' resets after 'TakeDamage' ends.
#include <iostream>
#include <vector>
using namespace std;
class SpaceShip {
[Code] ....
View 3 Replies
ADVERTISEMENT
Apr 7, 2013
I want to do this but do not know exactly how to do it:
Create an array of space ship objects and write a program that continually updates their positions until they all go off the screen. Assume that the size of the screen is 1024 pixels by 768 pixels.
I have begun to write a bit code but the problem is when you want to see the space ship moving step for step on the screen I do not know how to create the ship visible so you can see it is moving. Here's my code so far
Code:
#include <iostream>
using namespace std;
struct SpaceShips
{
int x_coordinate;
int y_coordinate;
[Code ......
View 4 Replies
View Related
Mar 2, 2013
This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.
#include <iostream>
using namespace std;
class A {
public:
int ** pointer;
[Code] ....
I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.
View 6 Replies
View Related
Apr 23, 2014
Just wondering how you reset a pointer returned from a 'new' command to the first element in an array (currently I'm just using another pointer).
In the code example below the pointer returned gets moved using pointer arithmetic to clear the array. So what would reset the pointer to the first element in the array of characters?
Size = 100;
char *data = new char [Size];
// clear array
for( int l = 0; l < Size; l++ )
*(data++) = ' ';
delete[] data
View 13 Replies
View Related
Feb 17, 2015
I am not sure why this is happening but I am simply trying to make rounds with my code and have it stop when one of two things, or both, happen.
#include <iostream>
#include "Classes.h"
#include "RNG.h"
#include "select.h"
#include "Fitnesschk.h"
using namespace std;
/*Global Variables************************************/
const int column = 32;
[Code] ....
I've tried resetting and renaming all variables but I don't see any conflict. I tried declaring in different areas but no dice. The other issue I have is that fittest always returns 0 even if the function returns 1. So it infinitely loops. When I go through looking at the variables. the fittest variable never changes even if the function returns 1 and round seems to reset after reproduce is called.
Here is the supporting code.
For Fittracker:
int fittracker(person man[], int row, int column){
for (int y = 0; y < row; y++){
int fit = 0;
for (int z = 0; z < column; z++){
if (man[y].data[z] == 1){
[Code] ....
View 10 Replies
View Related
Nov 9, 2014
So I've actually used rand() happily without any problems so far, but now I faced a problem. I have an AI loop that initialized a random number between 1 and 2 at the beginning, so something like this:
while (condition) {
int random = rand()% 1 + 2;
if (random == 1) {
[Code] ....
So this doesn't really work since it doesn't actually assign a new random value. I came across srand(time(NULL)), which resets the seed, but I didn't quite get when to use it. Do I have to call it in every cycle of the loop? I'm not completely sure how this works.
View 2 Replies
View Related
May 10, 2013
it will search the whole file once then i cant get it to reset without restart the program.
cin >> paycode;
while(paycode!=0) {
while(!infile.eof()) {
infile >> search;
infile.ignore(1);
[Code] .....
View 1 Replies
View Related
May 25, 2014
How to reset the board in order to play again without spots already taken.
#include <iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
char location[10] = {'0','1','2','3','4','5','6','7','8','9'};
int displayMenu(int &player, int &win);
[Code] .....
View 1 Replies
View Related
Apr 13, 2015
Is there some quick way of resetting the contents of an existing array to 0? Just to be clear, I'm not initializing the array, it already exists, has content and needs to be reset at 0. Is there a faster way than the code below?
Code:
for(i=0;i<100;i++)myarray[i]=0;
View 6 Replies
View Related
Oct 12, 2013
I have an example where I have a variable belonging to a base class, but I would like to tell the compiler that it actually belongs to a derived class. How can I do this?
// base class: R0
// derived class: R1
// see function SetR1 for the problem
class R0 {
public:
int a;
[Code] .....
View 5 Replies
View Related
Nov 9, 2013
I am trying to access a variable from another class through another class but it is not returning its "instance"?
Example:
Class View
Code:
...
V3D_Viewer viewer;
...
Class MainWindow
Code:
...
viewer* myView;
myView = new viewer();
...
Class Test
Code:
...
MainWindow window;
window.myView->setBackgroundColor(WHITE);
...
I am new to c++ references and pointers,
View 3 Replies
View Related
Feb 19, 2013
Here is the link for my program, I want to access the average value which is located in grade.c (calculate_grade) class through driver.c (main function) but I don't know how to make "average" visible
[URL] .....
View 1 Replies
View Related
Aug 2, 2014
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class ir;
class Bank_acc {
private:
string name,type,s;
long int accno,temp,balance,in;
[Code]....
errors are:
|6|error: forward declaration of 'class ir'|
|54|error: invalid use of incomplete type 'class ir'|
|99|error: no matching function for call to 'ir::interest()'|
View 1 Replies
View Related
Oct 22, 2014
how can I make the queue of type pcb
and how to access pid in other clases?
View 6 Replies
View Related
May 12, 2013
I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:
cajun/mustard
I want to take that string and make it the variable name of that class type. It would be implemented along the lines of:
Code:
string str;
//read/process string here, get:
str = "mustard";
createName(str);
//pass string to creator function When the function is called, I should get the variable:
Class mustard;
Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.
My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question.
(P.S. if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)
View 6 Replies
View Related
Oct 20, 2012
With what protection level c# treats variable "a" in "myclas", because I get error code when I want to change it?
Code:
using system;
class myclass {
int a;
}
class mainclass {
[Code] .....
View 4 Replies
View Related
Sep 30, 2014
I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable
Is there a way to do this?
static int destructorCount;
View 8 Replies
View Related
Dec 18, 2013
Is it possible to initialize class member variables in their definition statement instead of using a constructor?
Is the code bellow correct?
class rectangle
{
float a=0;
float b=0;
public:
string color="Red";
...
};
Which C++ Standard allows it?
View 2 Replies
View Related
Mar 20, 2014
My program works before i declare a new variable in class. Right after i declared a new int variable called prevans in my guess class, my program crashes when it runs.
Here's my code: Code: #include <iostream>
#include <time.h>
#include <cstring>
#include <cstdlib>
#include <vector>
[Code].....
View 14 Replies
View Related
Oct 24, 2014
I don't understand why my compiler gives me this error when I'm trying to run this code:
Code:
#include <iostream>
#include <cmath>
using namespace std;
class Airplane
{
public:
Airplane();
~Airplane();
[Code]...
The variable is protected. Yeah, that's right. But shouldn't a derived class be able to reach it? Or is it only in a function that the derived class is able to reach protected variables and isn't able to reach protected variables in the constructor?
View 2 Replies
View Related
Oct 22, 2013
I am trying to add data from a file that would go into a class that would later go into a vector of a class. I'm not really sure how to do it exactly. Here is the code:
Champion_Info.h
#ifndef CHAMPION_INFO_H_INCLUDED
#define CHAMPION_INFO_H_INCLUDED
#include <vector>
#include <string>
using namespace std;
class Champ_Info {
[Code] .....
View 2 Replies
View Related
Aug 18, 2012
I've been working on making a class that makes turning any variable or object into a byte array and vice-versa, quick with minimal pointer interface.
As well as a function for turning any variable or object into a binary text string.
Code:
#include <iostream>
#include <bitset>
#include <sstream>
// Convert a Variable to a Byte Array
template <class var>
unsigned char* VarToBytes(var &data) {
[code].....
View 2 Replies
View Related
Apr 13, 2012
In C++, how do i call a method member of class A from a class B, using a pointer. By the way Class A and B are of different types.
I read that when a pointer is pointing to member function it can only point member functions within the class. But how can i point to a member function outside the class.?????
for example
class A {
public:
int add(int x) {
return x+x;
[Code] .....
View 1 Replies
View Related
Dec 1, 2013
How can I add the variable adress to a void pointer inside of a class?
class variant2 {
private:
void *Vvariant=NULL;
public:
template<typename b>
variant & operator = (b *adress)
[Code] ....
if possible i want avoid the '&' when i assign the variable address.(variant2 f=varname;//like you see i don't use the '&')
for the moment i just need put the address to Variant pointer. but i receive several errors .
View 4 Replies
View Related
Jul 30, 2014
recently I developed a class header only in C++ to deal with byte arrays. Since I developed programs in other languages that had some libraries to dial with byte arrays, I developed one that the syntax is very similar to other languages.
Since I'm not very familiar with memory management, I was concerned about somethings I've read about (ex: memory fragmentation).
Here is a very simple example of my practice:
class ByteArray {
public:
ByteArray(size_t size) {
buffer = (int8_t*)malloc(size);
}
[Code].....
The class is intended to be used as part of comunication protocol in a webserver, byte arrays are created and destroyed a lot. Should I use pools? Is there a better practice? Am I doing everything wrong (laugh)?
For those who wants to see the entire class: [URL]
View 9 Replies
View Related
Dec 3, 2014
A have two classes, one inheriting the other, and the parent class being abstract (I plan on adding more child classes in the future). For reasons I won't bother mentioning, I'm making use of an STL container as a way for me to access all of the child objects in the heap. I've done so by making use of a map, with key type int and value type being a pointer to the parent class:
//PARENT.H
class Parent {
protected:
static int n;
static std::map<int, Parent*> map;
public:
virtual void pureVirtual() = 0;
[code]....
The Problem:In line 5 of Parent.cpp, initializing the value of the element to new Child won't work, as according to the compiler, the Child class hasn't been declared yet, and including Child.h into the Parent.h only opens an even bigger can of worms.I also can't initialize it as new Parent, seeing as the parent class is an abstract one.
The Question:Is there a way I can initialize the static map properly. Making the Parent class abstract is not an option.
View 3 Replies
View Related