Reputation: 1165
Im trying to make this CommandLine based game called "DungeonCrawl" in ObjectOrientedProgramming... I have created a header & source file, then I have declared functions and the class in header file, and definitions are on the source file.
The problem: When I print the board using 2 for loops, in a Void function, it prints some random integers instead of just 0's...
Result after printing:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1303831201 100681557
1606416496 32767 17 0 1 0 104 1 1606581343 32767
1606416256 32767 1606423158 32767 1606416280 32767 1606416280 32767 1 0
1606416304 32767 0 1 0 0 1606416320 32767 0 0
0 0 0 0 0 0 0 0 1606416288 32767
This is the header file:
#ifndef dungeoncrawl_hpp
#define dungeoncrawl_hpp
#include <iostream>
#include <stdio.h>
using namespace std;
class dungeoncrawl {
public:
dungeoncrawl();
void drawBoard();
~dungeoncrawl();
private:
int board[10][10];
uint8_t player_pos[0][0], enemy_pos[0][0];
};
#endif /* dungeoncrawl_hpp */
and here's the source file:
#include "dungeoncrawl.hpp"
// Constructor
class dungeoncrawl::dungeoncrawl {
int board[10][10] = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
};
};
// Con-&-Destructor
dungeoncrawl::dungeoncrawl(){}
dungeoncrawl::~dungeoncrawl(){}
void dungeoncrawl::drawBoard(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
cout << board[i][j] << " ";
}
cout << endl;
}
};
Upvotes: 0
Views: 1451
Reputation: 36
There are two errors in your code. At first:
uint8_t player_pos[0][0], enemy_pos[0][0];
Zero length arrays are offered by gcc as an extension. It is useless in your case.
Secondly:
class dungeoncrawl::dungeoncrawl {
It is not a construstor. If you want to initialize class member you can do it in a constructor. Such as here:
// Constructor
dungeoncrawl::dungeoncrawl(){
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
board[i][j] = 0;
}
}
}
Or, for example you can declare the array as static:
//dungeoncrawl.hpp
private:
static int board[10][10];
//dungeoncrawl.cpp
int dungeoncrawl::board[10][10] = {
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0}
};
Upvotes: 1