T
Travis L Spencer
Hey,
I am trying to insert a bunch of Token objects into a collection
called tokens of type deque<Token>. The problem is that the output of
Tokens:rint is just junk. For instance, the call at the end of main
produces this output:
Value: fsdf
Width: 0
Value: dfwf
Width: 0
Value: sdfasd
Width: 0
The call to cout's insertion operation on the line after tokens.print
produces this output:
Value: test3
Width: 5
I can't figure it out. I suspect that it has something with variables
going out of scope, but I could use some help finding the problem.
Here is a simplified example:
#include <deque>
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
class Token
{
const Token& operator=(const Token& r);
char value[255];
int width;
public:
Token(void) : width(0) { /*EMPTY*/ }
Token(const Token& t) {
strcpy(value, t.value);
width = t.width;
}
void append(const char c) {
value[width++] = c;
}
friend ostream& operator<<(ostream& os, const Token& token) {
os << "Value: " << token.value << endl
<< "Width: " << token.width << endl;
return os;
}
};
class Tokens
{
deque<Token> tokens;
Tokens(const Tokens&);
public:
Tokens(void) { /*EMPTY*/ }
Token* begin(void) {
return &*tokens.begin();
};
Token* end(void) {
return &*tokens.end();
};
void addNew(void) {
Token newToken;
this->tokens.push_back(newToken);
};
void Tokens:rint(std:stream& output = cout)
{
std::deque<Token>::iterator itr;
for (itr = tokens.begin() ; itr != tokens.end() ; itr++ )
output << " " << *itr << std::endl;
}
};
int main(int c, int v)
{
char* test[] = {"Test", "Test2", "test3"};
Tokens tokens;
for (int i = 0; i < 3; i++)
{
tokens.addNew();
for (int j = 0; j < strlen(test); j++)
tokens.end()->append(test[j]);
}
tokens.print(); // This prints all of the tokens but they are rubbish.
cout << *tokens.end(); // This prints a test3 that isn't just junk.
return 0;
}
Thanks in advance.
--
Regards,
Travis Spencer
Portland, OR. USA
I am trying to insert a bunch of Token objects into a collection
called tokens of type deque<Token>. The problem is that the output of
Tokens:rint is just junk. For instance, the call at the end of main
produces this output:
Value: fsdf
Width: 0
Value: dfwf
Width: 0
Value: sdfasd
Width: 0
The call to cout's insertion operation on the line after tokens.print
produces this output:
Value: test3
Width: 5
I can't figure it out. I suspect that it has something with variables
going out of scope, but I could use some help finding the problem.
Here is a simplified example:
#include <deque>
#include <iostream>
#include <cstring>
#include <cassert>
using namespace std;
class Token
{
const Token& operator=(const Token& r);
char value[255];
int width;
public:
Token(void) : width(0) { /*EMPTY*/ }
Token(const Token& t) {
strcpy(value, t.value);
width = t.width;
}
void append(const char c) {
value[width++] = c;
}
friend ostream& operator<<(ostream& os, const Token& token) {
os << "Value: " << token.value << endl
<< "Width: " << token.width << endl;
return os;
}
};
class Tokens
{
deque<Token> tokens;
Tokens(const Tokens&);
public:
Tokens(void) { /*EMPTY*/ }
Token* begin(void) {
return &*tokens.begin();
};
Token* end(void) {
return &*tokens.end();
};
void addNew(void) {
Token newToken;
this->tokens.push_back(newToken);
};
void Tokens:rint(std:stream& output = cout)
{
std::deque<Token>::iterator itr;
for (itr = tokens.begin() ; itr != tokens.end() ; itr++ )
output << " " << *itr << std::endl;
}
};
int main(int c, int v)
{
char* test[] = {"Test", "Test2", "test3"};
Tokens tokens;
for (int i = 0; i < 3; i++)
{
tokens.addNew();
for (int j = 0; j < strlen(test); j++)
tokens.end()->append(test[j]);
}
tokens.print(); // This prints all of the tokens but they are rubbish.
cout << *tokens.end(); // This prints a test3 that isn't just junk.
return 0;
}
Thanks in advance.
--
Regards,
Travis Spencer
Portland, OR. USA