How do I use string pointer to make dynamically allocated string array inside cl
ID: 3774224 • Letter: H
Question
How do I use string pointer to make dynamically allocated string array inside class.
Question.h
#ifndef QUESTION_H_INCLUDED
#define QUESTION_H_INCLUDED
class Question
{
private:
std::string *list;
public:
Question(int size);
~Question();
};
#endif // QUESTION_H_INCLUDED
Question.cpp
#include <iostream>
#include <string>
#include "Question.h"
using namespace std;
Question:: Question(int size)
{
list = new list[size];
}
Question:: ~Question()
{
delete [] list;
}
It said list is not a name type. What do I need to do?
Explanation / Answer
Here in the constructor , list is not a datatype(not pre defined or user defined)
In the constructor you did this way :
list=new list[size];
Which means that you are trying to assign list variable with an array of list datatype which is not present .. hence the compiler says "list is not a name type"..
But actually what you want may be ,to assign the variable list with dynamically created array of string with some size sent by constructor.
So you better try like this
list = new std :: string[size];
Because list is already a pointer for multiple strings.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.