2

Ok guys this problem has been bugging me all day and I cant seem find a solution. I know Its a long post but I would be very grateful for any help you can offer. I am working on a chatbot program that reads in from a .dat file to populate a library of keywords. Taking an object orientated approach I defined a class called "Keyword", the class definition is shown below:

class Keyword
{
public:   
    //_Word holds keyword 
    vector<string> _Word;
    //_Resp holds strings of responses
    vector<string> _Resp;
    //_Antymn holds words that are the opposite to keyword
    vector<string> _Antymn;

    // Constructor
    Keyword()
    {
        // Clears members when new instance created
        _Word.clear();
        _Resp.clear();
        _Antymn.clear();
    }
};

Therefore every time a new keyword is found in the .dat file, a new instance of the class keyword must be created. To store all these instances of keyword I create another vector but this time of type Keyword and call it library:

typedef vector<Keyword> Lib;
Lib library;// this is the same as saying vector<Keyword> library

Now this is the problem I have: After a user inputs a string I need to check if that string contains a keyword from the library i.e. I need to see if the string in _Word appears in the user input. Looking at it from a hierarchy of vectors you have:

The top level --> libary //*starting point
                 --> Keyword
                    --> _Word
                      -->"A single string" <-- I want to reference this one
                    --> _Resp
                      -->"Many strings"
                    --> _Antymn
                      -->"Many strings"

Phew! I hope that made sense. This is the code I started to write:

size_t User::findKeyword(Lib *Library)
{
    size_t found;
    int count = 0;

    for(count = 0; count<Library->size(); count++)
    {
      found = _Input.find(Library->at(count)); // this line needs to reference _Word from each keyword instance within library
      if(found!= string.npos)
        return found;
    }

    return 0;
}

I have also tried to use the "operator[]" method but that doesnt seem to do what I want either. Does anyone have any idea ? I would be very suprised if it couldn't be done. Thank you in advance.

6
  • 1
    Make it: found = _Input.find(Library->at(count)._Word); to access the member variable _Word Commented Aug 24, 2012 at 17:03
  • why is _word a vector? If you are only storing one keyword per instance of Keyword then it should just be of type string. Commented Aug 24, 2012 at 17:05
  • 2
    Note that names beginning with an underscore followed by a capital letter are reserved to the implementation. Don't use them. Commented Aug 24, 2012 at 17:05
  • Hey Papergay, I have tried this before posting but it kept throwing me an error. Could it be down to what Charlie has said ? Commented Aug 24, 2012 at 17:08
  • @Papergay: You can make your comment an answer. I would upvote it. Commented Aug 24, 2012 at 17:09

2 Answers 2

3

A bunch of issues first:

  • identifiers beginning with an underscore followed by a capital letter are reserved in any namespace
  • the clear() call in the Keyword constructor are pointless and possibly harmful to optimization

Why is word_ a vector? I though it is one keyword.

struct Keyword
{
    // real words as identifiers, no underscores 
    //anywhere if they are public
    std::string word;
    std::vector<std::string> respones;
    std::vector<std::string> antonym;
};


typedef std::vector<Keyword> Lib;



/// finding a keyword
#include <algorithm>

Lib::iterator findKeyword(const Lib& l, const std::string& x) {
  return std::find_if(begin(l), end(l), 
                      [](const Keyword& kw) { return kw.word == x; })
  // if stuck on non C++11 compiler use a Functor
}
Sign up to request clarification or add additional context in comments.

2 Comments

@pmr-thank you for your help. I have now removed the underscores from the class members. I originally used them to differentiate between local variables and class members. The clear() method was only included to initialise the vectors. Isnt this necessary ? Thank you.
@EdwardPaul A good convention is m_membername or membername_. It is important for private members to avoid ambiguity between a member function with the same name, e.g. the getter size() and the private member size_. The vector::clear is not necessary. A default constructed vector is empty. It also looks like you have using namespace std in a header. This is also bad.
2

You have to change your code to this:

for(count = 0; count<Library->size(); count++)
{
    for(int j = 0; j < Library->at(count)._Word.size(); ++j){
        found = _Input.find(Library->at(count)._Word[j]);
                                              ^^^^^^^^^
        if(found!= string.npos)
             return found;
    }
}

in order to access the member variable and to iterate through your vector of strings. Library->at(count) is an object of class the Keyword. I assume that _Input.find() takes a string as argument.

If your Keyword instance stores just one keyword, you might as well change it to string _Word, so that you wold not need the second loop.

for(count = 0; count<Library->size(); count++)
{
    found = _Input.find(Library->at(count)._Word);
    if(found!= string.npos)
        return found;
}

And to enforce the other comments: you should not use the preliminary _-underscore in your variable names since they are reserved by the implementation.

7 Comments

Thank you so much! _Input.find() does take a string as an argument. I was missing the _Word[j] piece off the end.
This is actively harmful. vector::at will make an out-of-bounds check and throw and is only recommended for debugging. So is rewriting std::find or unnecessary passing of pointers instead of references.
Im not sure, but can you even do Library->[index] ? Or how do you call that operator with pointers?
Btw, you should not neglect what pmr is pointing out.
@Papergay No, you can do Library->operator[](index);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.