memory error (access violation) on method parameters - c++

I've got weird problem. I'm trying to write simple game in C++, but I failed on objects and data types. There's a code:
// C++
// Statki
#include <stdio.h>
#include <time.h>
#include <map>
#include <vector>
#include <string>
#include <list>
#define D true
using namespace std;
void _(char* message){printf("%s\n",message);};
struct relpoint { int x,y; };
struct point { int x,y; };
struct size { int w,h; };
map<const char*, vector<relpoint> > shipshape;
list<char*> shipTypes = {"XS", "S", "M", "L", "XL"};
string alpha="ABCDEFGHIJKLMNOPRSTUVWXYZ";
enum fieldtype { UNKNOWN=-1,EMPTY=0,SHIP=1,HIT=2,MISS=3,};
enum rotation { EAST=0, SOUTH=1, WEST=2, NORTH=3 };
class Ship
{
char* type;
};
class Sea
{
public:
void init(size mapsize) { init( mapsize, EMPTY ); };
void init(size mapsize, fieldtype fill)
{
if(D)printf("Generating sea\n");
vector<fieldtype> v;
seamap.reserve(mapsize.h);
v.reserve(mapsize.w);
for (int y=0; y<mapsize.h; y++)
{
v.clear();
for(int x=0; x<mapsize.w; x++)
{
v.push_back(fill);
}
seamap.push_back(v);
}
view();
};
bool place_ship(Ship ship);
void view()
{
for( vector< vector<fieldtype> >::const_iterator yy = seamap.begin(); yy != seamap.end(); ++yy )
{
for( vector<fieldtype>::const_iterator xx = (*yy).begin(); xx != (*yy).end(); ++xx )
{
if(D)printf("%d ", *xx);
}
if(D)printf("\n");
}
};
private:
vector< vector<fieldtype> > seamap;
};
class Game
{
public:
void initmap(size mapsize)
{
if(D) printf("\nInit %d×%d map\n", mapsize.w, mapsize.h);
(*enemymap).init(mapsize, UNKNOWN);
//(*selfmap).init(mapsize);
};
bool placeship(string type, point position, rotation rotate);
fieldtype shoot(point target);
void viewmap(){(*selfmap).view();};
bool eog();
Sea * enemymap;
Sea * selfmap;
};
class Bot
{
public:
void init(size mapsize)
{
if(D)_("Init Bot");
}
private:
Game * g;
};
class Player
{
public:
Player() { if(D){_("Player fake init");} };
void init(size mapsize)
{
(*g).initmap(mapsize);
};
void viewmap(){(*g).viewmap();};
private:
Game * g;
};
class Router
{
public:
void startgame();
void welcomescreen()
{
printf("\n\n\n\t\t\tShips minigame\n\t\t\t\tby Kris\n\n");
mainmenu();
};
void mainmenu()
{
printf("Menu (type letter):\n\tN: New game\n\tS: Settings\n\tQ: Quit game\n\n > ");
char opt;
opt = toupper(getchar());
size ms;
switch(opt)
{
case 'N':
ms = getmapsize();
(*P1).init(ms);
(*P2).init(ms);
break;
case 'S':
break;
case 'Q':
break;
default:
printf("Invalid option %c", opt);
mainmenu();
}
};
private:
Player * P1;
Bot * P2;
size getmapsize()
{
size ms;
printf("\nSet map size (X Y)\n > ");
scanf("%d %d", &ms.w, &ms.h);
return ms;
};
};
int main () {
vector<relpoint> shp;
shp.reserve(5);
list<char*>::const_iterator tp = shipTypes.begin();
shp.push_back({0,0});
shipshape[*(tp++)] = shp;
shp.push_back({1,0});
shipshape[*(tp++)] = shp;
shp.push_back({2,0});
shipshape[*(tp++)] = shp;
shp.push_back({3,0});
shipshape[*(tp++)] = shp;
shp.push_back({2,1});
shipshape[*tp] = shp;
Router R;
R.welcomescreen();
printf("\n\n");
return 0;
}
It can be compiled, but after line Init 5×5 map program stops with Naruszenie ochrony pamięci (Memory access violation in polish) error. Problem seems to occur at both Sea::init() functions.
I'm compiling it with g++ -std=c++0x -Wno-write-strings ships2.cpp (to prevent warnings) on Ubuntu.
Any idea what's wrong with it?

All the classes contain pointers, but you never seem to initialize the pointers or allocate space for the objects they should point to.
Doing this
(*enemymap).init(mapsize, UNKNOWN);
when enemymap doesn't point anywhere, is an almost sure way to get an access violation.

You are using an uninitialized pointer. You can fix it by instantiating an object here, or in a constructor somewhere else.
Here is an example of instantiating in the initmap call.
void initmap(size mapsize)
{
// Initialize the pointer by instantiating a class
enemymap = new Sea;
if(D) printf("\nInit %d×%d map\n", mapsize.w, mapsize.h);
(*enemymap).init(mapsize, UNKNOWN);
//(*selfmap).init(mapsize);
};

+1 for Bo. But for your own sake:
compile with -g and then then
gdb ./mygame.bin
type 'run'
after setting the map size 5 5 :
Program received signal SIGSEGV, Segmentation fault.
mainmenu (this=<optimized out>) at memacvio.cpp:158
158 (*P1).init(ms);
This should tell you that P1 is probably not a valid poiner.

Related

Accessing double pointer from another class

I'd like to access to a double pointer which is located in another class "Board".
class Board
{
public:
Board(void);
Board(unsigned int xSize, unsigned int ySize);
~Board(void);
void SetObjectManager(ObjectManager* pObm);
void SetBlock(Block* block);
void LoadBoard(void);
void InitBoard(void);
//Other Functions...
private:
ObjectManager* m_obm;
Block* m_block;
//pointer to pointer to a int. (for 2 dimensional-array)
int **m_board;
};
First, the Board class. at the last line of class, you can see m_board.
I want to change this value in outside of this class.
Like this,
void Block::InitBlock(void)
{
int randPiece = Random::GIRand().RandInt(0, 1);
int randPos = Random::GIRand().RandInt(0, 10);
switch (randPiece)
{
case 0:
m_piece[2][1] = 1;
m_piece[2][2] = 1;
m_piece[2][3] = 1;
m_piece[3][3] = 1;
break;
//Other cases are here...
}
std::cout << "RandPos : " << randPos << std::endl;
std::cout << "RandPiece : " << randPiece << std::endl;
for (int y = 0; y < m_ySize; ++y)
{
for (int x = 0, pX = randPos; x < m_xSize; ++x, ++randPos)
{
if (m_piece[x][y] != 0)
m_board->SetBoardStatus(randPos, y, 1);
}
}
}
But, When I run this program, It blows up at SetBoardStatus(int, int, int)
SetBoardStatus looks like this,
void Board::SetBoardStatus(int x, int y, int value)
{
m_board[x][y] = value; //Visual Studio breaks the program here.
}
I allocate the double pointer properly.
And I set the board at the outside of this classes.
void Block::SetBoard(Board* board)
{
m_board = board;
}
And this is my block class.
class Block
{
public:
Block(void);
~Block(void);
void SetObjectManager(ObjectManager* pObm);
void LoadBlock (void);
void InitBlock (void);
void UpdateBlock (void);
void ReleaseBlock (void);
void SetBoard(Board* board);
private:
ObjectManager* m_obm;
Board* m_board;
int **m_piece;
int m_xSize;
int m_ySize;
};
Consider inheriting Block in Board; This will eliminate any possible de-referencing errors or bugs, as you can access the pointer right away.

Segmentation fault (core dumped) - Access violation reading location x

I'm writing a somewhat toy program to get into c++. However I'm having quite a lot of trouble with a segmentation fault at a very specific line in my code. I know that the segmentation fault means I tried to access memort that was not given to my program.
The code is (somewhat) as follows(it's actually too big to just copy paste) :
class car {
protected:
int speed;
std::string brand;
public:
car (int s, std::string b):speed(s),brand(b) {
std::cout << "New car has been created" << endl;
}
virtual int is_stopped () {
if (speed==0) return 1;
else return 0;
}
virtual void speed_up () {
speed++;
}
car* clone () {
car* tmp(this);
return tmp;
}
};
class fast_car : public car {
public:
fast_car (int s, std::string b):car(s,b) { }
};
class slow_car : public car {
public:
slow_car (int s, std::string b):car(s,b) { }
};
class highway {
int number_of_cars;
car **first; //There are derived classes from car hence the double pointer
public:
highway (int c, int s, std::string b):number_of_cars(c) {
first = new car*[c];
for (int i=0;i<c/2;i++)
first[i] = new fast_car (s,b);
for (int i=c/2;i<c;i++)
first[i] = new slow_car (s,b);
}
void speed_up () {
int pos;
pos = rand()%number_of_cars; //give me a random number between 0 and number_of_cars;
if (pos!=0) pos--; //We don't want position -1
first[pos]->speed_up ();
clone_fast (first[pos], (number_of_cars - pos - 1)); //the second argument is the remaining array "spots" until the end of the array
}
void clone_fast (car* cur, int rem) {
car* tmp;
for (int i=-;i<=rem;i++)
if ((cur+1)->is_stopped ()) { //Exact line where I get the segmentation fault
tmp = cur->clone ();
cur++;
}
}
};
So there it is. I tried to give you everything I could to recreate the problem. I'll try to solve any further questions.
Any help would be greatly appreciated.
The car*s are created by new, and aren't necessarily contiguous; they certainly aren't arrays that you can pointer-arithmetic around with. (car*) + 1 is not the same as (car**)[i + 1] (which is more like (car**) + 1). You probably want something a bit more like (*(first + 1))->doThing(), not (first[X] + 1)->doThing().
Edit: to be a bit more clear, if you can't do car[X + 1], then you can't do *(car + 1). Since the cars in clone_fast() are object pointers and not arrays, you can't do car[X + 1], thus (cur + 1)->is_stopped() is wrong.
I would suggest removing all pointer crap, and use more modern idioms. You can expand on the code below. However, it is unclear to me why you have two derived classes: they have no specific data neither methods, so why ?
class car
{
private:
int speed;
std::string brand;
public:
car (int s, std::string b):speed(s),brand(b)
{
std::cout << "New car has been created" << endl;
}
bool is_stopped ()
{
return speed==0;
}
void speed_up ()
{
speed++;
}
};
class fast_car : public car
{
public:
fast_car (int s, std::string b):car(s,b) {}
};
class slow_car : public car
{
public:
slow_car (int s, std::string b):car(s,b) {}
};
class highway
{
private:
std::vector<std::shared_ptr<car>> v_cars;
public:
highway (int c, int s, std::string b):number_of_cars(c)
{
for (int i=0;i<c/2;i++)
v_cars.push_back( std::make_shared<fast_car>(s,b) );
for (int i=c/2;i<c;i++)
v_cars.push_back( std::make_shared<slow_car>(s,b) );
}
};
int main()
{
highway h( 50, 20 );
return 0;
}

Issues creating a vector of class object in c++

I created the following class
#include "cliques.h"
#include "vector"
#include <iostream>
using namespace std;
cliques::cliques(){
}
cliques::cliques(int i) {
clique.push_back(i);
clique_prob = 1;
mclique_prob = 1;
}
cliques::cliques(const cliques& orig) {
}
cliques::~cliques() {
}
void cliques::addvertex(int i) {
clique.push_back(i);
}
double cliques::getclique_prob() const {
return clique_prob;
}
double cliques::getMaxclique_prob() const {
return mclique_prob;
}
void cliques::showVertices() {
for (vector<int>::const_iterator i = clique.begin(); i !=clique.end(); ++i)
cout << *i << ' ';
cout << endl;
}
vector<int> cliques::returnVector() {
return clique;
}
void cliques::setclique_prob(double i) {
clique_prob = i;
}
void cliques::setMaxclique_prob(double i) {
mclique_prob = i;
}
Here's the header file
#include "vector"
#ifndef CLIQUES_H
#define CLIQUES_H
class cliques {
public:
void addvertex(int i);
cliques();
cliques(int i);
cliques(const cliques& orig);
virtual ~cliques();
double getclique_prob() const;
double getMaxclique_prob() const;
void showVertices();
std::vector<int> returnVector();
void setclique_prob(double i);
void setMaxclique_prob(double i);
private:
float clique_prob;
float mclique_prob;
std::vector <int> clique;
};
#endif /* CLIQUES_H */
I want to create a vector of these objects in order to implement a heap
int main(int argc, char** argv) {
cliques temp(1);
cliques temp1(2);
temp.setclique_prob(0.32);
temp.setclique_prob(0.852);
temp.showVertices();
temp1.showVertices();
vector <cliques> max_heap;
max_heap.push_back(temp);
max_heap.push_back(temp1);
double x =max_heap.front().getclique_prob();
cout<<"prob "<<x<<endl;
cliques y = max_heap.front();
y.showVertices();
//make_heap (max_heap.begin(),max_heap.end(),max_iterator());
//sort_heap (max_heap.begin(),max_heap.end(),max_iterator());
return 0;
}
For reasons unknown to me none of my class functions work properly after i create my vector, meaning that while the following function works as intended
temp.showVertices()
the next one doesn't,
y.showVertices()
You miss implementation for
cliques::cliques(const cliques& orig) {
}
STL vector uses copy constructor inside when you add values to it. As your cliques class does not allocate any memory, you can just remove the copy constructor from the code and compiler will generate one for you.

Field Has Incomplete Type, Compiling From Multiple Files

I am trying to compile several files together for an agent based simulation of a zombie apocalypse (Awesome, right!?) Anyway, I am getting an error that I think has to do with the order in which header files are being included, but I can't wrap my head around how to figure out what's going wrong and how to fix it. The exact error is: "In file included from main.cpp, field 'Location' has incomplete type." Then similarly, "In constructor Creature::Creature() 'Location' undeclared".
Here are my files:
definitions.h
#ifndef definitions_h
#define definitions_h
class Creature;
class Item;
class Coords;
class Grid
{
public:
Creature*** cboard;
Item*** iboard;
Grid(int WIDTH, int HEIGHT);
void FillGrid(int H, int Z); //initializes grid object with humans and zombies
void Refresh(); //calls Creature::Die(),Move(),Attack(),Breed() on every square
void UpdateBuffer(char** buffer);
bool isEmpty(int startx, int starty, int dir);
};
class Random
{
public:
int* rptr;
void Print();
Random(int MIN, int MAX, int LEN);
~Random();
private:
bool alreadyused(int checkthis, int len, int* rptr);
bool isClean();
int len;
};
class Creature
{
public:
bool alive;
Coords Location;
char displayletter;
Creature() {Location.x=0; Location.y=0;} //ERROR HERE
Creature(int i, int j) {Location.xvalue(i); Location.yvalue(j);}
virtual void Attack();
virtual void Breed();
virtual void Move(Creature*** cboard);
virtual void Die();
virtual void MoveTo(Creature*** cboard, int dir);
virtual int DecideSquare(Creature*** cboard);
};
class Human : public Creature
{
public:
bool armed; //if armed, chances of winning fight increased for next fight
bool vaccinated; //if vaccinated, no chance of getting infected
int bitecount; //if a human is bitten, bite count is set to a random number
int breedcount; //if a human goes x steps without combat, will breed if next to a human
int starvecount; //if a human does not eat in x steps, will die
void Attack(Creature*** cboard);
void Breed(Creature*** cboard); //will breed after x steps and next to human
void Move(Creature*** cboard); //moves and checks itemboard for food
void Die(); //depends on bitecount, starvecount, and bool alive
void MoveTo(Creature*** cboard, int dir);
int DecideSquare(Creature*** cboard) {Creature::DecideSquare(Creature*** cboard);}
};
class Zombie : public Creature
{
public:
Zombie(int i, int j) {Creature::Creature()};
void Attack(Creature*** cboard); //will attack any adjacent human
void Breed() {} //does nothing
void Move(Creature*** cboard) {Creature::Move(Creature*** cboard;}
void Die(); //can only die from being attacked, never starves
};
class Item
{
};
class Coords
{
public:
int x;
int y;
int MaxX;
int MaxY;
Coords() {x=0; y=0; MaxX=0; MaxY=0;}
Coords(int X, int Y, int WIDTH, int HEIGHT) {x=X; y=Y; MaxX=WIDTH; MaxY=HEIGHT; }
void MoveRight();
void MoveLeft();
void MoveUp();
void MoveDown();
void MoveUpRight();
void MoveUpLeft();
void MoveDownRight();
void MoveDownLeft();
void MoveDir(int dir);
void setx(int X) {x=X;}
void sety(int Y) {y=Y;}
};
#endif
main.cpp
#include <cstdlib>
#include <iostream>
#include "definitions.h"
using namespace std;
int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}
definitions.cpp
#include <cstdlib>
#include "definitions.h"
Grid::Grid(int WIDTH, int HEIGHT)
{
//builds 2d array of creature pointers
cboard = new Creature**[WIDTH];
for(int i=0; i<WIDTH; i++)
{
cboard[i] = new Creature*[HEIGHT];
}
//builds 2d array of item pointers
iboard = new Item**[WIDTH];
for (int i=0; i<WIDTH; i++)
{
iboard[i] = new Item*[HEIGHT];
}
}
void Grid::FillGrid()
{
/* For each creature pointer in grid, randomly selects whether to initalize
as zombie, human, or empty square. This methodology can be changed to initialize
different creature types with different probabilities */
int random;
for (int i=0; i<WIDTH; i++)
{
for (int j=0; j<HEIGHT; j++)
{
Random X(1,100,1); //create a single random integer from [1,100] at X.rptr
random=X->rptr;
if (random < 20)
cboard[i][j] = new Human(i,j);
else if (random < 40)
cboard[i][j] = new Zombie(i,j);
else
cboard[i][j] = NULL;
}
} //at this point every creature pointer should be pointing to either
//a zombie, human, or NULL with varying probabilities
}
void Grid::UpdateBuffer(char** buffer)
{
for (int i=0; i<WIDTH; i++)
{
for (int j=0; j<HEIGHT; j++)
{
if (cboard[i][j])
buffer[i][j]=cboard[i][j]->displayletter;
else
buffer[i][j]=' ';
}
}
}
bool Grid::isEmpty(int startx, int starty, int dir)
{
Coords StartLocation(startx,starty,WIDTH,HEIGHT);
switch(dir)
{
case 1:
StartLocation.MoveUp();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 2:
StartLocation.MoveUpRight();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 3:
StartLocation.MoveRight();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 4:
StartLocation.MoveDownRight();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 5:
StartLocation.MoveDown();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 6:
StartLocation.MoveDownLeft();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 7:
StartLocation.MoveLeft();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
case 8:
StartLocation.MoveUpLeft();
if (cboard[StartLocation.x][StartLocation.y])
return 0;
}
return 1;
}
void Coords::MoveRight() {(x==MaxX)? (x=0):(x++);}
void Coords::MoveLeft() {(x==0)? (x=MaxX):(x--);}
void Coords::MoveUp() {(y==0)? (y=MaxY):(y--);}
void Coords::MoveDown() {(y==MaxY)? (y=0):(y++);}
void Coords::MoveUpRight() {MoveUp(); MoveRight();}
void Coords::MoveUpLeft() {MoveUp(); MoveLeft();}
void Coords::MoveDownRight() {MoveDown(); MoveRight();}
void Coords::MoveDownLeft() {MoveDown(); MoveLeft();}
void Coords::MoveDir(int dir)
{
switch(dir)
{
case 1:
MoveUp();
break;
case 2:
MoveUpRight();
break;
case 3:
MoveRight();
break;
case 4:
MoveDownRight();
break;
case 5:
MoveDown();
break;
case 6:
MoveDownLeft();
break;
case 7:
MoveLeft();
break;
case 8:
MoveUpLeft();
break;
case 0:
break;
}
}
Your forward declaration of class Coords in definitions.h is not enough to declare a variable of type Coords.
The forward declaration introduces the type but leaves it incomplete. You can declare a variable with pointer to incomplete type, but not a variable with incomplete type. So you must move the definition of class Coords before the definition of class Creature.
If you find yourself with a circular declaration dependency, you must introduce a pointer or reference declarator to solve it.
When a variable is declared, its type must be known. In your case, this means the definition of Coords must precede its use in the declaration Coords Location;.
Look at it from the compiler's perspective: it needs to know how much space Location will take, and for this it needs to know the definition of Coords. And of course, it's parsing from top to bottom.

What is the error in this code?

I have created a code in C++ that is supposed to display an O and an X, and you can move the O with w/a/s/d, and the X moves randomly. It compiles fine, but when I run it, there is an O, but no X. I put the source code here:
#include <iostream>
using namespace std;
//program class: defines player, enemies and powerups
#include<stdlib.h>
#include<time.h>
class prgm
{
friend void set_pos(prgm*const nprgm,int px,int py){nprgm->x=px;nprgm->y=py;}
friend void set_pos(prgm*const nprgm,int px,int py,int php){nprgm->x=px;nprgm->y=py;nprgm->hp=php;}
friend void set_pos(prgm*const nprgm,int px,int py,int php,char psym){nprgm->x=px;nprgm->y=py;nprgm->hp=php;nprgm->sym=psym;}
protected:
int x; //x-position
int y; //y-position
int hp; //health
char sym; //single-character representation
public:
prgm(){sym=' ';}
prgm(int px,int py,int php,char psym):x(px),y(py),hp(php),sym(psym){};
const int xpos(){return x;}
const int ypos(){return y;}
const char rsym(){return sym;}
void up(int n){y-=n;} //move program up
void left(int n){x-=n;} //move program left
void down(int n){y+=n;} //move program down
void right(int n){x+=n;} //move program right
void mutate(char nsym){sym=nsym;} //change program's symbol
void harm(int dam){hp-=dam;}
void heal(int dam){hp+=dam;}
prgm &prgm::operator=(const prgm&nprgm){x=nprgm.x;y=nprgm.y;hp=nprgm.hp;sym=nprgm.sym;}
};
//null program
prgm null();
//abstract enemy class for individual ai of each enemy
class enemy:public prgm
{
public:
void move(){}; //what enemy will do during its move
};
prgm player(0,0,0,'O');
void pmove()
{
char move;
cin>>move;
switch(move)
{
case 'w':
player.up(1);
break;
case 'a':
player.left(1);
break;
case 's':
player.down(1);
break;
case 'd':
player.right(1);
break;
case 'q':
exit(0);
break;
}
return;
}
//the X1 Virus Component, the simplest version of Virus X: moves in random directions, harms on contact.
class X1:public enemy
{
static const char sym='X';
public:
X1(){}
X1(int px,int py){x=px;y=py;}
void move();
};
void X1::move()
{
srand(time(0));
int dir=(rand()%2);
int dis=(rand()%3)-1;
while(dis=0)
{
dis=(rand()%3)-1;
}
if(dir=0)
{
y+=dis;
}
if(dir=1)
{
x+=dis;
}
return;
}
//sector 1
const int maxx=10; //length of sector
const int maxy=10; //height of sector
char sector[maxx][maxy]; //sector declaration
const int rsize=4;
prgm reactive_items[rsize];//array of powerups
X1 a(4,4); //enemy(ies)
const int vsize=1;
X1 viruses[vsize]={a}; //array of enemies
int px=0,py=0,php=1; //players position and hp
//function zeros-out the sector
void clear_sect()
{
for(int i=0;i<maxy;i++)
{
for(int j=0;j<maxx;j++)
{
sector[j][i]=' ';
}
}
}
//function sets all the enemies/powerups/player in the sector
void set_sect()
{
for(int i=0;i<rsize;i++)
{
int*tx=new int(reactive_items[i].xpos());
int*ty=new int(reactive_items[i].ypos());
char*ts=new char(reactive_items[i].rsym());
sector[*tx][*ty]=*ts;
delete tx;
delete ty;
delete ts;
}
for(int j=0;j<vsize;j++)
{
int*tx=new int(viruses[j].xpos());
int*ty=new int(viruses[j].ypos());
char*ts=new char(viruses[j].rsym());
sector[*tx][*ty]=*ts;
delete tx;
delete ty;
delete ts;
}
int*tx=new int(player.xpos());
int*ty=new int(player.ypos());
char*ts=new char(player.rsym());
sector[*tx][*ty]=*ts;
delete tx;
delete ty;
delete ts;
}
//function displays sector
void display_sect()
{
for(int i=0;i<maxy;i++)
{
for(int j=0;j<maxx;j++)
{
cout<<sector[j][i];
}
cout<<endl;
}
return;
}
void vmove()
{
for(int i=0;i<vsize;i++)
{
viruses[i].move();
}
return;
}
int main()
{
//load the sector # from the .txt, load the sector
set_pos(&player,px,py,php); //set the players x and y values
while(true){//while the player is on this sector
clear_sect();//clear the sector map
set_sect();//use the set function
display_sect();//use the display function
pmove();//players move
vmove();//enemy-ai move
//check board; relocate whats needed, initiate powerups, etc.
}//end of while
//when player wins sector
//write current sector to a .txt
//load program: restart this/story program/APHQ
return 0;
}
I know its long and pretty convoluted, (they were originally seperate .h files, but i put them together so that I could see all the code at once) but I really need an answer. Thanks
Your default constructor to prgm() sets the sym member to ' '. The static const char field does not override the base sym member, so the character used is probably the ' '.
Your enemy constructor should actually call the (int,int,int,char) base constructor to set the sym member.
Check your X1 class. It declares a static const char sym while it inherits a protected char sym member variable from class pgrm. Your display_sect function uses prgm's rsym() method, which returns the member variable, not the static member.