I'm programming a board game. When I call the constructor (with parameters) for the game, the program segfaults.
Main file:
#include <iostream>
#include "game.h"
using namespace std;
int main(){
int p_count = 2;
Game g(p_count);
//g.play();
}
Game Header:
#ifndef GAME_H_
#define GAME_H_
#include <iostream>
#include "board.h"
#include "player.h"
using namespace std;
class Game{
private:
Board b;
int moves, gamers;
Player players[10];
bool running;
public:
Game (int p_count);
void setup ();
void play ();
void report_score ();
bool in_mate (Player p);
bool in_check (Player p);
};
Game Constructor:
#include "game.h"
Game::Game(int p_count){
running = true;
moves = 0;
gamers = p_count;
}
Board header
#ifndef BOARD_H_
#define BOARD_H_
#include <iostream>
using namespace std;
class Piece;
class Board{
private:
static const int SIZE = 8;
Piece *board[SIZE][SIZE];
public:
Board ();
};
#endif /* BOARD_H_ */
Board constructor
#include "board.h"
#include "piece.h"
Board::Board(){
bool b = false;
for (int i=0; i<SIZE; i++){
for(int j=0; j<SIZE;j++){
board[i][j]->set_status(b);
}
}
}
Player header
#ifndef PLAYER_H_
#define PLAYER_H_
#include <iostream>
#include "board.h"
#include "piece.h"
using namespace std;
class Player{
private:
static const int NUM = 16;
Piece pieces[NUM];
int side;
public:
Player ();
Player (int p);
#endif
Player constructor
#include "player.h"
Player::Player(){
side = 0;
}
Piece header
#ifndef PIECE_H_
#define PIECE_H_
#include <iostream>
#include "board.h"
using namespace std;
class Board;
struct location{
int row;
int col;
};
class Piece{
private:
location pos_moves[50], loc;
char type;
bool status;
int moved, team;
public:
Piece ();
Piece (int piece_num, int bel);
void set_status (bool b);
};
#endif /* PIECE_H_ */
Piece implementation
#include "piece.h"
Piece::Piece(){
status = false;
team = 0;
moved = 0;
type = 'A';
}
void Piece::set_status(bool b){
status = b;
}
I call some functions from within the constructor that initialize the unused variables, but the program crashes regardless of whether or not they're included.
One problem that I see is that you have defined board as an array of pointers, not objects,
Piece *board[SIZE][SIZE];
and then you proceed to use board in Game::Game() as though board points to valid objects.
Board::Board(){
bool b = false;
for (int i=0; i<SIZE; i++){
for(int j=0; j<SIZE;j++){
// Problem.
// board[i][j] has not been initialized
// to point to any valid object.
board[i][j]->set_status(b);
}
}
}
You can resolve that by making board an array of objects.
Piece board[SIZE][SIZE];
or making sure that you allocate memory each element of the array before using them.
Board::Board(){
bool b = false;
for (int i=0; i<SIZE; i++){
for(int j=0; j<SIZE;j++){
// Allocate memory for the element
// of the array.
board[i][j] = new Piece;
board[i][j]->set_status(b);
}
}
}
I recommend using an array of objects. Then, you won't have to worry about dealing with memory allocation and deallocation. If you use an array of pointers, please read The Rule of Three.
board is a 2-d array of the pointer of Piece, you should new it before use it in Board's ctor:
Board::Board(){
bool b = false;
for (int i=0; i<SIZE; i++){
for(int j=0; j<SIZE;j++){
board[i][j] = new Piece; // add it here
board[i][j]->set_status(b);
}
}
}
BTW, don't forget to delete the pointers, maybe in Board's dtor.
Related
This question already has answers here:
Initialize base class with no default constructor in constructor of derived class
(3 answers)
Closed 3 years ago.
I'm learning oop in c++ and have the following error:
In constructor 'pflFr1::pflFr1()'
Error: No matching function for call to worldBuilder::worldBuilder()
The class worldbuilder is generating protected 2d arrays. The class pflfr1 is inherting from worldbuilder. But somehow, the constructor of worldbuilder has a problem.
My code:
main.cpp:
#include <iostream>
#include "pflfr1.h"
#include "worldbuilder.h"
#include <vector>
using namespace std;
int main()
{
srand(time(0));
int sl = 10;
worldBuilder w_obj(sl);
w_obj.buildPhyWelt();
w_obj.buildVegWelt();
pflFr1 objekt();
return 0;
}
worldbuilder.h:
#ifndef WORLDBUILDER_H
#define WORLDBUILDER_H
#include <vector>
using namespace std;
class worldBuilder
{
public:
worldBuilder(int sl_);
vector<vector<int> > buildPhyWelt();
vector<vector<int> > buildVegWelt();
protected:
vector<vector<int> > phyWelt;
vector<vector<int> > vegWelt;
int sl;
private:
};
#endif // WORLDBUILDER_H
worldbuilder.cpp:
#include <iostream>
#include "worldbuilder.h"
#include <vector>
using namespace std;
worldBuilder::worldBuilder(int sl_)
: sl(sl_)
{
}
vector<vector<int> > worldBuilder::buildPhyWelt()
{
phyWelt.resize(sl, vector<int>(sl));
// initialisiere physische Welt
// 1 = land, -1 ist meer
for(int i=0; i<sl; i++)
{
for(int j=0; j<sl; j++)
{
phyWelt[i][j] = 1;
}
}
}
vector<vector<int> > worldBuilder::buildVegWelt()
{
vegWelt.resize(sl, vector<int>(sl));
// initialisiere Nahrung
for(int i=0; i<sl; i++)
{
for(int j=0; j<sl; j++)
{
if(rand()%100<=2)
{
vegWelt[i][j] = 1;
}
else
{
vegWelt[i][j] = 0;
}
}
}
}
pflfr1.h:
#ifndef PFLFR1_H
#define PFLFR1_H
#include <vector>
#include "worldbuilder.h"
using namespace std;
class pflFr1: protected worldBuilder
{
public:
pflFr1();
protected:
private:
int y;
int x;
int hp;
};
#endif // PFLFR1_H
pflfr1.cpp:
#include <iostream>
#include <pflfr1.h>
#include <cstdlib>
#include <ctime>
using namespace std;
pflFr1::pflFr1()
: hp(10)
{
int initPosY = rand()%sl;
int initPosX = rand()%sl;
y = initPosY;
x = initPosX;
}
Your default constructor pflFr1::pflFr1() 's implementation is rewritten as
pflFr1::pflFr1()
: worldBuilder(), hp(10){
...
}
Because the pflFr1 class derives from worldBuilder, its constructor must be called as part of pflFr1 object creation. The default behaviour is to call the default constructor of the base - worldBuilder().
But this constructor does not exist. You only declared worldBuilder::worldBuilder(int sl_);. By doing so you disabled the automatic generation of default constructors for classes, which is done only if no user-defined constructor was declared.
Either you can write worldBuilder()=default; inside the class, which will leave s1 uninitialized. Or you can use delegating constructors - worldBuilder():worldBuilder(0){}.
Bad access means that i am trying to access memory that doesn't exists I have tried and tried to allocate memory for this class, but have failed everywhere. I do not know where the error is actual coming from. It only tells me that my setter method is when the program crashes. In the setFName() method is where the error occurs. But in the main method is where it actually occurrs.
nurse.hpp
#ifndef Nurse_hpp
#define Nurse_hpp
#include <stdio.h>
#include <string>
#include <stdlib.h>
using namespace std;
class nurse{
private:
string firstName;
public:
nurse() {
firstName = "jim";
}
string getFName() {return firstName;}
void setFName(string fName) {firstName = fName;} // Thread 1: bad access 0x0
};
#endif /* Nurse_hpp */
here is where the error is actually happening
main.cpp
#include <cstdint> // ::std::uint64_t type
#include <cstddef> // ::std::size_t type
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "nurseHolder.hpp"
using namespace std;
nurseHolder *l = new nurseHolder();
int main() {
return 0;
}
and finally here is the class that is causing the issue
nurseHolder.hpp
#ifndef Nurses_hpp
#define Nurses_hpp
#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include "Nurse.cpp"
using namespace std;
class nurseHolder{
private:
int nurse_cnt;
int nurse_cap;
vector<nurse> nurse_list;
public:
nurseHolder() {
nurse_cnt = 0;
nurse_cap = 10;
for(int i= 0; i < 11; i++){
nurse_list[i].setFName("na");
}
}
vector<nurse> &getNurseList() { return nurse_list;}
};
#endif /* Nurses_hpp */
I tried to make this compact as possible sorry if its a lot of code.
here is what I changed to make the code work:
nurseHolder() {
nurse_cnt = 0;
nurse_cap = 10;
for(int i= 0; i < 11; i++){
nurse l;
nurse_list.pushback(l);
}
}
Is this a correct way to do this?
Your vector nurse_list has size 0. So you cannot use [] operator to set names.
There are two ways you can correct this:
Set an initial size to the vector and use [] to set names.
Use push_back to add elements to the vector.
First method.
nurse_list.resize(noOfTotalNurses).
nurse_list[i].setFName("name");
Second method.
nurse tNurse; //local nurse object
tNurse.setFName("name");
nurse_list.push_back(tNurse);
I made a class with a header and a cpp and a main class. When doing so all is good! When separating a class that I will have 2 classes (header+cpp) and one main Class A (Board) does not recognize class B (IllegalCoordinateException) even though I inserted the include. This is probably a newbies question and I might lose some points, but I am stuck in finding my problem.
Here is my working code(stripped down to only the important parts):
main.cpp
#include "Board.h"
#include <iostream>
using namespace std;
int main() {
Board board1{4}; // Initializes a 4x4 board
try {
board1[{3,4}]='O'; // This should raise an exception
} catch (const IllegalCoordinateException& ex) {
cout << "Illegal coordinate" << ex.theCoordinate() << endl; // prints "Illegal coordinate: 3,4"
}
return 0;
}
Board.h
#ifndef CIRC_H
#define CIRC_H
#include <iostream>
#include <string>
using namespace std;
struct coord {
int x;
int y;
};
class IllegalCoordinateException{
coord _coord;
public:
IllegalCoordinateException(coord c){
_coord = c;
}
string theCoordinate() const{
return to_string(_coord.x)+","+to_string(_coord.y);
}
};
class xo{
char x;
public:
char getChar() const{return x;}
char& operator= (const char c){x = c;}
xo& operator= (const xo _xo){
x = _xo.getChar();
return *this;
}
void clear(){
x = '.';
}
operator char() const{
return x;
}
};
class Board{
private:
coord _coord;
xo** board;
int size;
public:
Board();
Board(int v);
~Board();
xo& operator[](coord c); // here is where I use "IllegalCoordinateException"
};
#endif
Board.cpp
#include <iostream>
#include <vector>
#include "Board.h"
using namespace std;
void freeBoard(xo** board,int size){
for(int i = 0 ; i < size ; i++){
delete[] board[i];
}
}
Board::Board()
{
size = 0;
board = new xo* [size];
}
Board::Board(int v)
{
size = v;
board = new xo* [size];
for (int i=0; i<size; i++)
{
board[i] = new xo[size];
for(int j = 0 ; j < size ; j++){
board[i][j].clear();
}
}
}
Board::~Board(){
freeBoard(board,size);
delete[] board;
}
xo& Board::operator[](coord c)
{
if(c.x < size && c.y < size)
{
return board[c.x][c.y];
}
else
{
throw IllegalCoordinateException(c);
}
}
After seperation:
main.cpp
No diff
Board.h also cpp no diff
#ifndef CIRC_H
#define CIRC_H
#include "IllegalCoordinateException.h"
#include <iostream>
#include <string>
using namespace std;
struct coord {
int x;
int y;
};
class xo{
char x;
public:
char getChar() const{return x;}
char& operator= (const char c){x = c;}
xo& operator= (const xo _xo){
x = _xo.getChar();
return *this;
}
void clear(){
x = '.';
}
operator char() const{
return x;
}
};
class Board{
private:
coord _coord;
xo** board;
int size;
public:
Board();
Board(int v);
~Board();
xo& operator[](coord c);
};
#endif
IllegalCoordinateException.h // I also seperated in my code to .h and .cpp (but ofcourse there is no real diff.
#ifndef CIRC_H
#define CIRC_H
#include <iostream>
#include "Board.h"
using namespace std;
class IllegalCoordinateException{
coord _coord;
public:
IllegalCoordinateException(coord c){ _coord = c;}
string theCoordinate() const{return to_string(_coord.x)+","+to_string(_coord.y);}
};
#endif
When doing
$ g++ -g -Og -std=c++0x main.cpp Board.cpp IllegalCoordinateException.cpp
I get:
Board.cpp: In member function ‘xo& Board::operator’:
Board.cpp:60:43: error: ‘IllegalCoordinateException’ was not declared
in this scope
throw IllegalCoordinateException(c);
How can this be? I mean I am including it in the Board.h so Board.cpp is supposed to recognize it!? I tried also to include it in Board.cpp and also make a forward declaration in Board.cpp but both were frugal.
Both of your header files have #ifndef CIRC_H / #define CIRC_H.
So when the first one is included (no matter which order), it defines CIRC_H, and when the second one is included, it gets ignored because the whole file is within #ifndef CIRC_H.
Solution: use a different macro name for each header file.
How would I access my vector declared in aDie.h from aHisto.h, preferably not as a function? Once my vector is in my Histo.h I want to be able to modify it, change size, and just manipulate it however I choose. I am just having troubles with error messages in VS.
aDie.h
#define aDie_H
#pragma once
#include <vector>
#include <iostream>
class aDie {
public:
aDie();
void numRolls();
void getSeed();
void roll();
void myVector(); //just gives my vector values
void Print();
std::vector<int> myV; //declare my vector, it has values stored from void myVector();
private:
int i = 0;
int Rolls;
int dSeed;
int die1;
int die2;
int sum;
};
aHisto.h
#define aHistogram_H
#include "aDie.h"
class aHistogram : public aDie{
public:
//adds a pointer to my vector so I can access and modify it anywhere on this header
aHistogram(); //default const
void getVector(); //does stuff with vector
private:
int i = 0;
int min;
int max;
};
aHisto.cpp
#include "aHistogram.h"
#include "aDie.h"
#include <iostream>
#include <vector>
using namespace std;
aHistogram::aHistogram() { //default constructor
min = 0;
max = 0;
}
void aHistogram::getVector() {
//does stuff with vector here
}
Use the scope resolution operator because aHistogram inherits from aDie.
or you could use this because of the inheritance access level.
void aHistogram::getVector() {
aDie::myV->something();
//Or use this pointer
this->myV->something();
}
So here is the main where i'm trying to call the array by a pointer:
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(int (&Numbers)[6][6]);
}
Apparently "Numbers" is undefined even though the header is included, here's the header and .cpp files relating to it.
LotteryData.h
#pragma once
#include <iostream>
#include <fstream>
#include "Lottery.h"
#include "Player.h"
using namespace std;
class LotteryData
{
private:
public:
LotteryData();
~LotteryData();
void PassInfo(int (&Numbers)[6][6]);
};
LotteryData.cpp
#include <iostream>
#include <fstream>
#include "LotteryData.h"
using namespace std;
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6])
{
int* ptr;
FILE *Numfile;
Numfile = fopen("C:/Num.txt", "wb");
ptr = &Numbers[6][6];
for (int i=0; i<36; i++)
{
fwrite(ptr, sizeof(int), 36*36, Numfile);
}
fclose(Numfile);
//ofstream out("Numbers.txt");
}
Everything seems fine, I'm puzzled why the reference in the main says the array is undefined, any ideas?
edit: Apologies, missed some bits
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
};
Player.cpp
#include <iostream>
#include <fstream>
#include "Player.h"
using namespace std;
Player::Player()
{
}
Player::~Player()
{
}
void Player::Input()
{
int num(0);
int duplicate = 0;
int game = 0;
int NumberofGames = 0;
cout<<"How many games do you want to play for this weeks draw?"<<endl;
cin>>NumberofGames;
if (NumberofGames>6)
{
cout<<"Please enter an amount between 1 and 6"<<endl;
cin>>NumberofGames;
}
do
{
for (int i=0;i<6;i++)
{
cout<<"Enter Number "<< (i+1) <<endl;
cin>>num;
if (num > 0 && num <67)
{
Numbers[game][i]= num;
}
else
{
cout <<"Please enter number between 1 and 66"<<endl;
i = i-1;
}
}
game = game + 1;
NumberofGames = NumberofGames - 1;
}
while (NumberofGames=0);
}
void PassInfo(int (&Numbers)[6][6]);
That line does not declare an array - it declares a function. You have no declaration for an array in your class (in fact, you have no data members declared in your class at all).
If you want to declare a member array, you need to modify your class definition:
class LotteryData
{
private:
int Numbers[6][6]; // this declares an array
public:
LotteryData();
~LotteryData();
void PassInfo(int (&arr)[6][6]); // this is still a function declaration
};
Just because you made a function's parameter be named Numbers doesn't magically mean that your program has an array called Numbers declared in it.
So a Player has an array called "Numbers".
Then you would use it like this:
data.PassInfo(player.Numbers);
In your main() function,
data.PassInfo(int (&Numbers)[6][6]);
This is wrong. You should simply pass a reference to 2D array.
int (&Numbers)[6][6];
data.PassInfo(Numbers);