I have implemented all the missing code and I cannot reproduce your problem. Note that I added a copy assignment operator as this is required for types that are stored in a vector that are not movable. I have published the code below so that you can compare to your implementation. (This uses some C++11 features)
#include <string>
#include <vector>
#include <iostream>
#include <memory>
class Token {
private:
struct Impl;
std::unique_ptr<Impl> impl;
public:
Token();
Token(const Token& token);
Token(const std::string& word);
Token& operator=(const Token& token);
std::string GetWord();
void SetWord(const std::string& word);
};
class TokenList {
private:
struct Impl;
std::unique_ptr<Impl> impl;
public:
TokenList();
TokenList(const TokenList& tokenList);
std::size_t Size() const;
void Push(const Token& token);
Token& operator[](int i);
const Token& operator[](int i) const;
};
// Token Implementation
struct Token::Impl
{
std::string Word;
};
Token::Token() : impl{ std::make_unique<Impl>() }
{
}
Token::Token(const Token& token) : Token{}
{
impl->Word = token.impl->Word;
}
Token::Token(const std::string& word) : Token{}
{
impl->Word = word;
}
Token& Token::operator=(const Token& token)
{
impl->Word = token.impl->Word;
return *this;
}
std::string Token::GetWord()
{
return impl->Word;
}
void Token::SetWord(const std::string& word)
{
impl->Word = word;
}
// TokenList Implementation
struct TokenList::Impl
{
std::vector<Token> tokens;
void push(const Token& token)
{
tokens.push_back(token);
}
};
TokenList::TokenList() : impl{ std::make_unique<Impl>() }
{
}
TokenList::TokenList(const TokenList& tokenList) : TokenList{}
{
impl->tokens = tokenList.impl->tokens;
}
void TokenList::Push(const Token& token)
{
impl->push(token);
}
std::size_t TokenList::Size() const
{
return impl->tokens.size();
}
Token& TokenList::operator[](int i)
{
return impl->tokens[i];
}
const Token& TokenList::operator[](int i) const
{
return impl->tokens[i];
}
int main()
{
TokenList tokens;
Token s1("hello");
Token s2("world");
tokens.Push(s1);
tokens.Push(s2);
for (int i = 0; i < tokens.Size(); ++i)
std::cout << i << ": " << tokens[i].GetWord() << std::endl;
}