I have an issue on using my Push function on the TokenList class where I encapsulated the vector of Tokens. Whenever I call the Push function, the member variable word inside Token class was empty even when I initialize the Token variable properly. I've also tried using integer and it stores garbage number.
Token class
class Token {
private:
class Impl;
std::unique_ptr<Impl> impl;
public:
Token();
Token(const Token& token);
Token(const std::string& word);
~Token();
std::string GetWord();
void SetWord(const std::string& word);
};
TokenList class
class TokenList {
private:
class Impl;
std::unique_ptr<Impl> impl;
public:
TokenList();
TokenList(const TokenList& tokenList);
~TokenList();
std::size_t Size() const;
void Push(const Token& token);
Token& operator[](int i);
const Token& operator[](int i) const;
};
Implementation
class TokenList::Impl {
private:
std::vector<Token> tokens;
public:
/* .... */
void push(const Token& token) {
tokens.push_back(token);
}
};
void TokenList::Push(const Token& token) { impl->push(token); }
Main
TokenList tokens;
Token s1 ("hello");
Token s2 ("world");
tokens.Push(s1);
tokens.Push(s2);
for (int i = 0; i < tokens.Size(); ++i)
cout << i << ": " << tokens[i].GetWord() << endl;
Here is the output when I run the main:
0:
1:
I've tried using string instead of TokenList as the type parameter for the vector and it works perfectly. When I trace the issue, it was in the Push function not getting the correct passed Token value. So what causes the TokenList's Push functioon to not take the member values of the Token class?