Segfault while trying to access a class string member

I have a class Messagethat has std::stringas a data element defined as follows:

class Message
{
// Member Variables
    private:
        std::string text;
        (...)

// Member Functions
    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
{
// Member Variables
    private:
        std::vector<Message> messageLog;
        (...)

// Member Functions
    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();

            // draw the Console frame
            TCODConsole::root->printFrame(x, y, width, height, true);

            // print the Console messages
            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);
            }
        }
+3
4

, , it->getText(), NULL. it != messageLog.end(), , it->getText().

+2

std::vector messageLog, std::vector<Message> messageLog? .

0

, ? :

messageLog.begin()+height-1;

? , , , , , , SIGSEGV.

, , , , . , , , . messageLog.size(), . , :

for(it=messageLog.begin(); it!=messageLog.end(); ++it)
0

, Message, Console, Console. , , draw, , .

Try this (just paste the keyword new):

    Console()
    {
        messageLog.push_back(new Message("Hello World!"));
    }

In this case, the object is not deleted after the Console finishes.

Remember to delete objects created when your program no longer needs them.

-2
source

Source: https://habr.com/ru/post/1751093/


All Articles