C++ my initialized string disappears when I leave the default constructor but my other member variables values don't -
i'm trying test hangman program. problem i'm running in default constructor initialize member variables. wrong , guess's values saved theres no issue there other member variables the_word , sofar don't saved. think though issue header file. think messed in hangman file declarations of member variables the_word , sofar. if can in figuring out issue(giving me solution love) grateful because has been eating brain long time. thanks!
in main cpp file have
#include <iostream> #include <string> #include <vector> #include "player.h" #include "hangman.h" using namespace std; int main() { hangman game1; while(1) { game1=hangman();//error object of type hangman cannot assigned //because copy assignment implicitly delated }
in player.h file have:
#ifndef player_h_ #define player_h_ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; class player { public: player(); void makeguess(char &guess); void win(); void lose(); char agree(); void display(); private: string name; int score; string mystring; char answer; }; #endif
in hangman file have:
#ifndef hangman_h_ #define hangman_h_ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> #include "player.h" using namespace std; class hangman { public: hangman(); void play(); protected: player player2; vector<string> words; const string the_word;//i think issue here string sofar;//i think issue here string used=""; int wrong; char guess; const int maxwrong=4; //void virtual respondincorrectguess(); }; #endif
in implementation file have:
#include "hangman.h" #include "player.h" hangman::hangman() { wrong=0; guess='a'; words.push_back("monkey"); words.push_back("hangman"); words.push_back("difficult"); const string the_word = words[2];//this might key solving issue cout<<the_word<<endl; string sofar(the_word.size(),'-'); cout<<sofar<<endl; } void hangman::play() { cout<<"this word now:"<<the_word<<endl;//outputs nothing cout<<"now number of - is:"<<endl; cout<<sofar<<endl;//outputs nothing exit(1); }
update:: in short code have in test.h file
#ifndef test_test_h #define test_test_h #include <iostream> #include <string> #include <vector> #include <algorithm> #include <ctime> #include <cctype> using namespace std; class test{ public: test(); void setname(); string getname(); private: string name; }; #endif
in implementation cpp file have:
#include "test.h" test::test() { name="hi"; } void test::setname() { cout<<"input name"<<endl; cin>>name; } string test::getname() { return name; }
in main file have:
#include "test.h" int main(){ test student; student.setname(); cout<<student.getname()<<endl; test::test(); cout<<student.getname()<<endl;//this prints out name set earlier instead of default name don't understand }
you declaring const string the_word
in hangman ctor, you're not setting member variable. simple fix put the_word
in initializer list.
hangman::hangman() : the_word(words[2]) /* other members should init here */ { ... }
edit; well, ctor need sent words
work.
Comments
Post a Comment