C++ passing an object -
i'm learning c++, doesn't works expected. made classes bowlinggame (just text-based) , works well. want pass object of bowlinggame class, called scorecalculator.
the header file looks like:
#ifndef scorecalculator_h #define scorecalculator_h #include "bowlinggame.h" class scorecalculator { private: bowlinggame * game; public: //constructor scorecalculator(const bowlinggame &game); //setters void setgame(const bowlinggame &game); }; #endif // scorecalculator_h
and implementation looks like:
#include "scorecalculator.h" #include "bowlinggame.h" #include <iostream> using namespace std; scorecalculator::scorecalculator(const bowlinggame &game) { setgame(game); } void scorecalculator::setgame(const bowlinggame &gamse) { //* game = gamse; fails //cout << gamse.getlastframenummer(); works }
i save 'bowlinggame object' in instance var game, reason console application crashes. i've no idea i'm doing wrong. failure in rule: * game = gamse;
looked examples on internet, can't find solution.
//* game = gamse; fails
you haven't allocated game
. pointer not pointing valid yet. have create new game
object point to.
do this
game = new game(gamse);
assuming game
has accessible copy constructor.
also don't forget delete
game in destructor or earlier if don't need more. or better use smart pointer `game.
Comments
Post a Comment