jpm
jpm

Reputation: 1083

List object globally and adding local object of the class in c/c++

list<Text*> textList; //global declaration of textList


void addText(int w, int h){ 
    Text *text = new Text; //error in this line
    text->init(w,h);
    textList.push_back(text);
}


void printText(){
for(list<Text*>::iterator it = textList.begin(); it != textList.end(); ++it)
        {
            (*it)->print();
        }
}

I've created a global textList of pointers of Text class. addText() & printText() is called from another class (c++). But getting error in creating text object

is there anyway that I can use textList globally and add objects to it locally in a function

Upvotes: 0

Views: 115

Answers (1)

Theolodis
Theolodis

Reputation: 5102

If you encounter an error in the line Text *text = new Text;, then it is because you have no matching constructor for building a Text without arguments.

Because generally your code should work if you called the correct constructor.

So, to answer your question, yes it is possible to use textList like you do. The error you encounter is not related to that global variable.

Upvotes: 1

Related Questions