Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6576809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:33:29+00:00 2026-05-25T15:33:29+00:00

I just wrote a simple game (if you’re interested, each player should say a

  • 0

I just wrote a simple game (if you’re interested, each player should say a word beginning with the last letter of the word said by his opponent.)
It won’t compile with the following output:

totosus@btop:~/projects/dict$ g++ wordgame.cc 
/tmp/ccIjadX8.o: In function `main':
wordgame.cc:(.text+0x90b): undefined reference to `WGdict::~WGdict()'
wordgame.cc:(.text+0x92e): undefined reference to `WGdict::~WGdict()'
collect2: ld returned 1 exit status

The code:

dict.h:

#include <map>
#include <string>
#include <fstream>
#include <set>
#include <vector>

#define ft first
#define sd second
#define MCMSB map<char,map<string,bool> >
#define MSB map<string,bool>
#define PSB pair<string,bool>

using std::map;
using std::vector;
using std::pair;
using std::set;
using std::ifstream;
using std::ofstream;
using std::string;



class WGdict{
    private:

    MCMSB M;//main map
    bool HasBeenFilled;

    bool WasUsed(MSB::iterator it){//a small function which checks the "used" bit at the iterator
    return (*it).sd;
    }

string strip(string str){
    string::iterator i = str.begin();
    char space = ' ';
    while( *i == space){
        i = str.erase(str.begin());
    }
    i = str.end()-1;
    while( *i == space){
        str.erase(i);
        i = str.end()-1;
    }
    return str;
}

bool  FillFromFile(string filename){return FillFromFile(filename,false);}//default

bool  FillFromFile(string filename, bool PreserveContents){//fills the main map with contents of file
    ifstream dicfile ( filename.c_str() );//open file
    if(dicfile.is_open()&&dicfile.good()){
        string str;
        if(!PreserveContents){
            M.clear();//clear map
        }
        char c;
        while(!dicfile.eof()){//get words from file and add them to the map
            dicfile>>str;
            str = strip(str);
            c=str[0];
            M[c].insert(PSB(str,false));
        }
        HasBeenFilled = 1;
        dicfile.close();
        return true;
    }
    else if (dicfile.is_open()) dicfile.close();
    return false;
}

bool setbool(string word, bool state , bool flip){// set to STATE (or flip) the "used" bool
    bool ok = false;
    MCMSB::iterator it = M.find(word[0]);
    if( it != M.end() ){
        MSB::iterator O = it->sd.find(word);
        if( O != it->sd.end()){
            if(!flip) O->sd = state;
            else  O->sd = ! (O->sd);
            ok=true;
        }
    }
}

public:


WGdict(){HasBeenFilled = false;}//default constructor

~WGdict(){ M.clear(); }//destructor

WGdict(string fname){//file-opening constructor
    open(fname);
}

bool open(string fname){//open new file with default settings
    return FillFromFile(fname);
}

bool open(string fname,bool pc){//open new file and choose whether to preserve contents
    return FillFromFile(fname,pc);
}

MSB find(char ch){//search the dictionary by first letter
    MCMSB::iterator it = M.find(ch);
    MSB results;
    if (it!=M.end()){
        results = (*it).sd;
    }
    return results;
}

vector<string> find(char ch, bool wu){//search the dictionary by first letter&"used" parameter
    vector<string> v;
    MSB search = find(ch);
    for(MSB::iterator it = search.begin();it!=search.end();it++){
        if(WasUsed(it)==wu) v.push_back((*it).ft);
    }
}

set<string> findS(char ch, bool wu){//same as previous, but output is in a set
    set<string> s;
    MSB search = find(ch);
    for(MSB::iterator it = search.begin();it!=search.end();it++){
        if(WasUsed(it)==wu) s.insert((*it).ft);
    }
}

void save(string filename){//save a dictionary
    ofstream of( filename.c_str() );
    if(of.is_open()&&of.good()){
        for(MCMSB::iterator me=M.begin() ; me!=M.end();me++){
            for(MSB::iterator i = me->sd.begin(); i != me->sd.end() ; i++){
                of<<( (*i).ft+"\n" ).c_str();
            }
        }
        of.close();
    }
    else if (of.is_open()) of.close();
}

void add(string word){add(word,false);}//add a word to the dictionary

void add(string word,bool b){
    char c = strip(word)[0];
    M[c].insert(PSB(word,b));
}

bool flip(string word){return setbool(word,0,1);}

bool use(string word){return setbool(word,1,0);}

bool use(string word, bool state){ return setbool(word,state,false);}

bool exists(string word){
    bool ok = false;
    MCMSB::iterator it = M.find(word[0]);
    if( it != M.end() ){
        MSB::iterator O = it->sd.find(word);
        if( O != it->sd.end()){
            ok=true;
        }
    }
}

bool used(string word){
    if(exists(word)){
        return M.find(word[0])->sd.find(word)->sd;
    }
    return true;
    }
};

wordgame.cc:

# include "dict.h"
# include <vector>
# include <iostream>
# include <algorithm>

using namespace std;

char getlastletter(string str){return str[str.size()-1];}

char sayword(vector<string> words){
random_shuffle ( words.begin(), words.end() );
string theword = (*words.begin());
cout<<theword;
return getlastletter(theword);  
}

char big(char ch){
if('a'<=ch<='z')  ch-=32;
return ch;
}

char small(char ch){
if('A'<=ch<='Z')  ch+=32;
return ch;
}

string small(string str){
for(string::iterator it = str.begin();it != str.end();it++){
    (*it) = small((*it));
}
}

string big(string str){
for(string::iterator it = str.begin();it != str.end();it++){
    (*it) = big((*it));
}
}


int main(){
    WGdict dict;
vector<string> ws;
string fnm,word;
char yourletter = 'a';
char myletter;

while(true){
    cout<<"Write the filename of the dictionary:";
    cin>>fnm;
    if(fnm == "[exit]" || dict.open(fnm)) break;
    else cout<<"It seems that the file \""<<fnm<<"\" does not exist.\n Try again\n\n" ;
}
if(fnm == "[exit]") return 0;
cout<<"Let's play a word game!\n\n";


while(true){
    cout<<"My turn: ";
    ws = dict.find(yourletter,false);
    if(ws.size() == 0){//exit if no more words are available.
        cout<<"uh oh. It appears that I couldn't find any unused words with this letter. \n But, yet, congratulations! You won! \n bye! \n";
        return 0;
    }
    myletter = sayword(ws);
    cout<<"\n";
    ws = dict.find(myletter,false);
    if(ws.size() ==                          0){//exit if no more words are available.
        cout<<"So... I managed to win this game! My dictionary contains no more words beginning with this letter. \n Although I won, you also did well.  \n bye! \n See you soon!";
        return 0;
    }
    cout<<"Your turn:\n";
    while(true){
        cout<<big(myletter);
        cin>>word;
        word = small(word);
        if (word == "[exit]") return 0;
        if ( word[0] == small(myletter) ){
            if(dict.exists(word)) {
                if(!dict.used(word)){
                    yourletter = getlastletter(word);
                    dict.use(word);
                    break;
                }
                else cout<<"this word has already been used \n";
            }
            else cout<<"Sorry, I couldn't find \""<<word<<"\" in my dictionary. \n";
        }else cout<<"\""<<word<<"\" does not begin with letter "<<big(myletter)<<"! \n";
        cout<<"try again! \n";
    }
}   
}

I couldn’t understand neither what is causing the error nor how to fix it, so please tell me what i’m doing wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:33:29+00:00Added an answer on May 25, 2026 at 3:33 pm

    Delete the compilation/object files in your directory and recompile – the code works great, you probably just need a make-clean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple web service in C# using SharpDevelop (which I just got
I just wrote a very simple Expect script for wrapping around rsync, but it
I'm trying to write a simple program in VC++ which will just initialize the
I'm just starting out writing trying to write a simple program in C and
I just want a simple tool that will help me quickly write scripts/packages that
I just wrote my first web service so lets make the assumption that my
I just wrote a new web part and now I am getting this error
I just wrote the following C++ function to programmatically determine how much RAM a
I just wrote an if statement in the lines of if (value == value1
I have a macro that I wrote to just help me with my unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.