I have a class Messagethat has std::stringas a data element defined as follows:
class Message
{
private:
std::string text;
(...)
public:
Message(const std::string& t)
: text(t) {}
std::string getText() const {return text;}
(...)
};
This class is used in a vector in another class, for example:
class Console
{
private:
std::vector<Message> messageLog;
(...)
public:
Console()
{
messageLog.push_back(Message("Hello World!"));
}
void draw() const;
};
There draw()is an iterator that calls getText(). When this happens, the segfaults program. I determined what is textvalid inside the constructor Message. However, I can’t say whether this is really from the inside Console. I assume this is the case, but if I try to check the ConsolemessageLog indices , gdb will tell me the following:
(gdb) p messageLog[0]
One of the arguments you tried to pass to operator[] could not be converted to what
the function wants.
Does anyone know what is going on?
EDIT: draw(). TCODConsole curses, , Console curses. TL BR Point - ( ints), , Console. Message Console , , , , , , . .
void Console::draw() const
{
int x = TL.getX(), y = TL.getY();
int width = BR.getX() - TL.getX();
int height = BR.getY() - TL.getY();
TCODConsole::root->printFrame(x, y, width, height, true);
vector<Message>::const_iterator it;
for(it=messageLog.begin(); it<messageLog.begin()+height-1; ++it)
{
string message = "%c" + it->getText();
TCODConsole::setColorControl(TCOD_COLCTRL_1,
it->getForeColor(),
it->getBackColor());
y += TCODConsole::root->printRectEx(x, y, width, height,
TCOD_BKGND_NONE,
TCOD_LEFT,
message.c_str(),
TCOD_COLCTRL_1);
}
}