dynamic array problem

Here I have created a class called text. Later in the new_text function I have declared a dynamic array of pointers to text objects.

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
class text {
    public:
        int xpos, ypos;
        SDL_Surface * drawn;
        void init(string, TTF_Font *, SDL_Color, int, int);
        bool active;
};

text * textlist;

void text::init(string text, TTF_Font * font, SDL_Color c, int x, int y) {
    xpos = x;
    ypos = y;
    drawn = TTF_RenderText_Solid(font, text.c_str(), c);
    active = false;
}

int new_text(string text, TTF_Font * font, SDL_Color c, int x, int y) {
    text * temp = new text[texts + 1];
    if(texts != 0) {
        for(int i = 0; i < texts; i++) {
            temp[i] = textlist[i];
        }
        temp[texts].init(string text, TTF_Font * font, SDL_Color c, int x, int y);
        delete [] textlist;
    }
    else {
        temp[0].init(string text, TTF_Font * font, SDL_Color c, int x, int y);
        delete textlist;
    }
    textlist = temp;
    temp = NULL;
    texts++;
    return texts - 1;
}



However, the compiler throws this error:

In function `int new_text(std::string, TTF_Font*, SDL_Color, int, int)':
19 `temp' undeclared (first use this function)
19 'text' is not a type


I also did this with another class that was nearly identical to this one, and it worked just fine. I just can't figure out what is going wrong. I declared my text class, and it should register as a new type... right?
You have a variable in your function new_text named the same thing as the class. That, in short, is bad.

-Albatross
Oh my lord... thank you!

Why do I always get tripped up on something really simple?

Also, I realized that I accidentally declared the type when calling the init function.

It all works now! Thanks!
Topic archived. No new replies allowed.