Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote