-3

I have to make a class with the ability to allow a user to designate how many questions they wish to ask and then have the user write each question down. How would I go about this? I have tried using dynamic memory allocation on string objects, but have received issues with the ">>" operator when I allow the user to ask the question.

#pragma once
#include<iostream>
#include<string>

using namespace std;
class TestGenerator
{
private:
    int index;
    string** question;
    void AskQuestions(); //Needs work
    void AskIndex();
public:
    TestGenerator();
{
this->question = new string*[index];
    this->index = 0;
}

    TestGenerator(int, string**);
    TestGenerator(const TestGenerator& aTest);
    ~TestGenerator();
{
for (int i = 0; i < index; i++)
    {
        delete this->question[i];
        this->question[i] = nullptr;
    }
    delete[] this->question;
    this->question = nullptr;
}
    TestGenerator& operator= (const TestGenerator& aTest);

    void setQuestions(string**);
    void setIndex(int);

    string** GetQuestions() const;
    int GetIndex() const;

    void AskAll(); 

    void ShowIndex()const;
    void ShowQuestions()const; 
    void ShowAll()const; 

    friend ostream& operator << (ostream& out, const TestGenerator& aTest);
    friend istream& operator >> (istream& in, TestGenerator& aTest);
};

    void TestGenerator::AskQuestions()
    {
        for (int i = 0; i < index; i++)
        {
            cin >> this->question[i];
        }
    }

istream& operator>>(istream& in, TestGenerator& aTest)
{
    in >> aTest.AskAll();
    return(in);
}

void TestGenerator::AskAll()
{
    this->AskIndex();
    this->AskQuestions();
}

It is giving me an error E0349 with the operator ">>"

Subs103017
  • 23
  • 5
  • 1
    show the code you tried and say what went wrong (compile fails, abort at runtime, wrong oupput...) – pm100 Feb 14 '22 at 23:18
  • 1
    You should simplify your code and post an [mcve], along with a description of what "issues" you are having. Pay attention to compiler errors and warnings and fix as many as possible. Please note that part of simplifying your code is to remove all user input if possible. If that's not possible, at least tell us what input your are providing to the program when you test it. – David Grayson Feb 14 '22 at 23:18
  • 2
    General note you'll figure out on your own fairly quickly, but in the interests of saving you time, *dynamic memory allocation on string objects* is almost always the wrong thing to do. A huge chunk of `std::string`'s job is to hide the messiness of dynamic memory from you. If you dynamically allocate the sucker, you're taking no small amount of that ugly job back on yourself. But what if I want an array of `string`s of user-defined size? `std:: vector` is to arrays what `string` is to strings. If you are not allowed to use `vector`, the best thing to do is write your own simplified `vector`. – user4581301 Feb 14 '22 at 23:22
  • repeating what @user4581301 says ===>> use std::string and std::vector – pm100 Feb 14 '22 at 23:24
  • The code you now show has [many errors](https://godbolt.org/z/T1nW9Y59d). Some typos, some missing includes, some stray semicolons. This can not be the code you say is giving you "an error". Please provide a [mre] of the code you are asking about. – Drew Dormann Feb 14 '22 at 23:39
  • get out of the habit of using 'using namespace std' https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – pm100 Feb 15 '22 at 00:12
  • 1
    FYI, you don't need to use the `this->` syntax for accessing member variables. Change the name of your parameters. – Thomas Matthews Feb 15 '22 at 00:20

1 Answers1

3

There are too many ** on your string array:

string** question;

If you are going to have a naked array of strings, you only need:

string* question;

and:

this->question = new string[index];

But don't do that; I strongly recommend:

vector<string> question;

Life is much simpler with vectors.

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
pm100
  • 48,078
  • 23
  • 82
  • 145
  • 1
    And if the assignment rules say No `vector`, write your own. There are several reasons for this 1) you can keep using it on subsequent assignments that also won't allow you to use `vector`. 2) It's a great learning experience. 3) If you keep the container separated from the program logic, you can test both separately, and it's always easier to solve one problem at a time. – user4581301 Feb 14 '22 at 23:57