Vector Of Strings/Char Arrays

I have a class and I need to add a vector of strings or char arrays to it.
I tried both ways, and I always get weird results, like random characters trough the strings or just nothing stored at all.

After some debugging and outputing of values I discovered that:
> Right after the attribution with push_back(), a proper value is stored.
> When I write the text on the screen the strings are eronated/erased.
> During debugging (unlike actually running the executable) the program crashes most of the times when it tries to delete the vector (Automatically deleted along with the class when using temporary instances of it). It also says a bit about deleting pointers to strings from the vector, which actually contains string and not pointers to strings.

#CLASS HEADER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef GUI_H_INCLUDED
#define GUI_H_INCLUDED
#include "TileBase.h"
#include "Menu.h"
#include <string>
#include <vector>

class Letter
{
    private:
    TileBase tile;
    TileBase tile_small;
    bool is_open;
    std::vector<std::string> lines;

    public:
    ~Letter();
    Letter();
    Letter(int X,int Y,const char* Content);
    void Move(char axis,float speed);
    void draw();
    void drawGUI();
    void update();
    void open();
    void close();
    bool isOpen();
};

#endif // GUI_H_INCLUDED 


#CLASS CPP FILE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "Gui.h"
#include "TileBase.h"
#include "Menu.h"
#include "Util.h"
#include "Player.h"
#include "Map.h"
#include "Defines.h"
#include <windows.h>
#include <string>

Letter::~Letter()
{

}

Letter::Letter()
{

}

Letter::Letter(int X,int Y,const char* Content)
{
    tile = TileBase(getScreenWidth()/2 - 140,getScreenHeight()/2 - 200,280,400,0,0,texture_gui_addr("paper"));
    tile_small = TileBase(X,Y,22,32,0,0,texture_gui_addr("paper_small"));
    is_open = false;

    std::string buffer = Content;
    size_t sentence_start = 0;
    size_t sentence_end = buffer.find_first_of(".?!",sentence_start);
    while(sentence_end != std::string::npos)
    {
        char line[sentence_end];
        strcpy(line, buffer.substr(sentence_start, sentence_end-sentence_start+1).c_str());
        lines.push_back("aab");//EVEN WHEN USING CONSTANTS SAME RESULT
        ///VALUES ARE GOOD HERE, CHECKED WITH COUT
        sentence_start = buffer.find_first_not_of(" /t/n", sentence_end+1);
        sentence_end = buffer.find_first_of(".?!",sentence_start);
    }
}

void Letter::Move(char axis, float speed)
{
    tile_small.Move(axis,speed);
}

void Letter::draw()
{
    if(!is_open)
    if(tile_small.IsOnScreen())
    tile_small.draw();
}

void Letter::update()
{
    extern Player player;
    extern Map world;
    extern bool updated;
    extern SDL_Event currentEvent;

    if(is_open)
    if(updated)
    if(currentEvent.type == SDL_MOUSEBUTTONUP)
    if(currentEvent.button.button == SDL_BUTTON_LEFT)
    is_open = false;

    if(updated)
    if(currentEvent.type == SDL_KEYDOWN)
    if(currentEvent.key.keysym.sym == SDLK_SPACE)
    if(tile_small.ContainsPoint(player.getX()+16,player.getY()+16))
    {
        for(unsigned int i=0;i<lines.size();i++)
        {
            std::cout<<lines[i]<<std::endl;//ERONATED VALUES
            world.addTextAnim(tile.getX()+20, tile.getY() + (i+1)*20,random(2,3),EMPTY,lines[i].c_str(),White);
        }
    }
}

void Letter::open()
{
    is_open = true;
}

void Letter::close()
{
    is_open = false;;
}

bool Letter::isOpen()
{
    return is_open;
}


Please tell me if you see anything wrong or have any ideas why the problem occurs. If you need more code (only other appearances of the Letter class are in the main "storage" map, which contains an vector of Letters and calls update() and draw(). It also contains functions to add Letters to the storage, simply creating an instance of Letter with the function arguments and storing it in the vector of Letters.

Nevermind, solved it.
Last edited on
Topic archived. No new replies allowed.