Thread: Using Static Arrays, C++ uses an odd method of assigning values?

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    Using Static Arrays, C++ uses an odd method of assigning values?

    Hello,

    I'm currently working on a simple hangman game. I'm running into two problems, first is that in an array which I've labeled static, if I attempt to assign any values I get an unknown symbel error. " ZeroLink: unknown symbol '__ZN7Hangman5WORDSE' ", the program then exits due to signal 6 (SIGABRT). I have absolutely no clue as to what this means.

    Secondly, I am attempting to assign values to positions in two arrays, one for correctly guessed characters, and one for incorrect guesses. Somehow it ends up assigning any character that is guessed to both arrays. I have gone through the code a number of times and cannot see any logic errors. I'm reduced to thinking that it must be one of the "perks" of c++ that I am missing. ^_^

    I've included the following code, the first is the header file, and the second/third are the only methods which actually affect values.

    My background is primarily Java and basics so that may explain some mistakes you may see ^_^;;.

    Thank you,

    Norehsa

    Code:
    /* Hangman.h */
    
    #ifndef HANG_H
    #define HANG_H
    #include <iostream>
    #include <string>
    #include <cstdlib>
    class Hangman{
    private:
    	static std::string WORDS[3];
    	std::string word;
    	char guessG[];
    	char guessB[7];
    	int  bguess;
    	bool gameWon();
    	bool gameLost();
    	bool gameOver();
    	void handleGuess(char);
    	void printHangman(int);
    public:
    	Hangman();
    	~Hangman();
    	void playGame();
    };
    
    #endif
    Constructor
    Code:
    Hangman::Hangman(){
    	//The bug is in the following three assignment statements.
    	//WORDS[0] = "hello";
    	//WORDS[1] = "cat";
    	//WORDS[2] = "digg";
    	//word = WORDS[rand()%2];	
    	word = "cat";
    	//prepare the good guess section.
    	for(int i = 0; i<word.length(); i++){
    		guessG[i] = '_';
    	}
    	//prepare the bad guess section.
    	bguess = 0;	
    	for(int i=0; i<7; i++){
    		guessB[i] = '_';
    	}
    		
    }
    This is the method that places a guess into the array for correct guesses (guessG) or incorrect guesses (guessB). I've included the header to clarify a bit.

    Code:
    /**
    * handleGuess
     *
     * If the guessed letter (parameter ch) is in the secret word
     * then add it to the array of correct guesses and tell the user
     *      that the guess was correct;
     * otherwise, add the guessed letter to the array of incorrect guesses
     *      and tell the user that the guess was wrong.
     *
     * @param ch the guessed letter
     */
    
    void Hangman::handleGuess(char ch) {
    	bool wasIn = false;
    	for(int i=0; i<word.length(); i++){
    		if(word[i] == ch){		
    			guessG[i] = ch;
    			wasIn = true;
    		}
    	}
    	if(!wasIn){
    		guessB[bguess] = ch;
    		bguess++;
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    147
    I suggest, first, you have no design need for a static array for WORDS.

    Take it out of static and that should work out.

    What I think is happening is that you've declared but not defined space for the static array.

    If you pasted directly from source code, the declaration for char guessG[]; has no space assigned, so there's no real storage allocated there.

    If you intended dynamic storage for guessG, use a vector.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    1. You need to redeclare member static variables in the definition file (.cpp or .c) like so:

    std::string Hangman::WORDS[3];

    2. guessG never gets memory allocated for it. You may want to use a std::string object and simply use the push_back() member function to add new good guesses to your list. A std::vector or std::list would do the job as well. That way, you don't have to work with dynamic memory allocation.

    I'd advise you to reconsider your design. C++ is a multi-paradigm language and therefore supports both OOP and procedural programming (modules). In this case, there is not much of a link between the Hangman game and an object. In the case of the Hangman game, I would go procedural programming all the way. You'd need a few variables in the scope of the main() function and a few functions to deal with those variables. I personally dislike designs based on a playGame() function or run() or whatever. There's usually a better alternative that is simpler and much more adapted to your needs.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I disagree that a Hangman class is a bad idea. Encapsulating the game into a class allows future expansion of the program. For example, you can have multiple games, or you can create multiple UIs for the game. By making it a class, if you have one set of code for console UI and another set of code for a graphical UI, then you can pass the Hangman object to the appropriate UI code.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    6
    Alright thanks for the feedback, I'll give your suggestions a go and see what results.

    Just to clear things up, in my defense I'm simply translating old Java labs into C++ to learn the language, thats why there are a few parts which are awkward in design.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Daved: the current simple class is no different from simple global functions. By dividing the game into useful classes, you can get extensibility, but not by simply taking all code and effectively wrapping a class around it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Multiple Values to Array/ Vector
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2009, 01:15 PM
  2. Replies: 1
    Last Post: 05-31-2009, 04:02 PM
  3. Sorting Arrays - Even and Odd Input
    By deedlit in forum C Programming
    Replies: 19
    Last Post: 11-20-2003, 04:35 AM
  4. Undefined Error
    By peking1983 in forum C++ Programming
    Replies: 1
    Last Post: 03-11-2003, 03:55 PM
  5. call static method
    By geeoff in forum C++ Programming
    Replies: 1
    Last Post: 02-01-2002, 02:48 PM