0

I'm looking for a way to create class objects through a for loop. For reference, I'm trying to create a class of objects, user input will determine how many objects are needed. I'm using a constructor within a for loop to do this, but it requires the object name to be variable. I've tried a lot of methods, nothing has worked, including vectors. I literally started using C++ a day ago, so ignore the messy code or foolish errors unless they are pertinent to the question, its a pet project.

// Enemy Names
    string eNames[ENum]{};

    // Enemy Initiatives
    double eInits[ENum]{};

    //Enemy Temp ID's
    int eTempids[ENum]{};

    // Enemy Names Vector
    std::vector<EnemyClass> en(ENum);

    if (ENum > 0) {
        for(int i = 0; i < ENum; i++){
            string name;
            double init;
            int tempid = i + PLAYER;
            cout << "Enter Enemy " << i + 1 << "'s Name" << endl;
            cin >> name;
            cout <<  "Enter " << name << "'s Initiative" << endl;
            cin >> init;
            eNames[i] = name;
            eInits[i] = init;
            eTempids[i] = tempid;
            EnemyClass en[i](string name, double init, int tempid);
        };
    }; // END of IF Statement

en[i] is what needs to be the variable name. I either get an error stating that "deceleration of 'en' as array of functions" or that 'EnemyClass' is not a declared function, Not sure how to use that it was an attempt from another forum post I found:

Question was the same as mine with below code:

for (int count = 0;  count < no_of_objects; count ++)
{
    ClassName object_name[count]
}

Answer code:

std::vector<ClassName> objects (no_of_objects);

But this produces the "Not a declared function" error in the stl_construct.h file

Gwynoak
  • 37
  • 5
  • 1
    Variable names like `name` exist in your source code. Strings like whatever gets input into `name` exist in your program. They're entirely separate things, and your program can never create more variable names. But you can assign to the unnamed objects within the `vector`: see the `en[i] = `... in @S.M.'s answer. – aschepler Apr 02 '20 at 21:50
  • 1
    A rule of thumb: If you are using parallel arrays, make a structure with the fields, and use one array (std::vector) of the structure. – Thomas Matthews Apr 02 '20 at 21:51

1 Answers1

1

Look at Why aren't variable-length arrays part of the C++ standard? to understand why the declaration string eNames[ENum], double eInits[ENum] and int eTempids[ENum] are wrong. Use vectors, as you did it for en: std::vector<string> eNames(ENum), std::vector<double> eInits(ENum) and std::vector<int> eTempids(ENum).

Regarding your question. Assignments are used so:

en[i] = EnemyClass(name, init, tempid);

It is much better if you would declare the empty enemy names vector and fill it in-place:

std::vector<EnemyClass> en;
// ...
  en.emplace_back(name, init, tempid);

P.S. Do not study C++ on Stackoverflow. Start read books from The Definitive C++ Book Guide and List

273K
  • 29,503
  • 10
  • 41
  • 64