0

I'm learning about classes in c++ and am working on a project that requires me to read in input from a file and make class objects based on what is read in. I don't know how many objects need to be created. I also can't use vectors. How can I create class objects in a loop with different names?

while(!inFile.eof())
{
   // Create class objects
}

Is this even possible?

Edit: By names, I meant identifiers. I have class objects that are of type Ship. I need to make multiple Ship objects iteratively after reading input from a file. I need to be able to access the members of each ship object throughout the program's runtime. I have other classes like Dock and LinkedList that have Ship pointers as members that I need to attach the created Ship objects to based on file input. I don't know how many Ship objects need to be made. I can't use vectors.

  • What exactly do you mean by "class objects with different names"? Actually create objects of different types? Or is "name" some sort of member of a class? – UnholySheep Apr 23 '20 at 18:51
  • Sorry, yes by names I meant identifiers. I'll edit the post and add more detail. – Ryan Khavari Apr 23 '20 at 18:53
  • In the single line of code you've written you already have a bug. Read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – rustyx Apr 23 '20 at 18:55
  • Yes I know it's incorrect, and I am not using this format in my actual code. Just wanted quickly convey the idea. – Ryan Khavari Apr 23 '20 at 18:58
  • Identifiers are a compile-time concept only. It is nonsensical to consider identifiers that depend on run-time information, like how many ships you have. You need to store your ships in a container. Normally, `std::vector` would do the job. If you can't use that, there are [a lot of other containers](https://en.cppreference.com/w/cpp/container) you could use. But I'm guessing you're expected to use your `LinkedList` container to store them. Normally, if you need to identify a dynamically created object in a collection, you use its index, a pointer or an iterator. – François Andrieux Apr 23 '20 at 18:59
  • Basically any time you find your self looking for "dynamic identifiers", you want to use [`std::unordered_map`](https://en.cppreference.com/w/cpp/container/unordered_map). – 0x5453 Apr 23 '20 at 19:09
  • We aren't expected to use any concepts we haven't learned yet, and we haven't learned about any containers except for arrays. And LinkedList is only to be used if all 10 dock objects are full, like a queue. So I'm not sure if I'm severely missing some crucial information or I'm supposed to go through the file once and count how many Ship objects will need to be created in advance. – Ryan Khavari Apr 23 '20 at 19:10
  • 1
    @RyanKhavari If all you have is arrays, then you probably need to use an array. Either count the ships first then allocate an array or reallocate bigger arrays and move the data as you need more space. But be sure to understand that you would never under any circumstances do that manually in real production code. This is exactly what `std::vector` does for you. By doing it manually you are taking on the responsibility of managing the lifetime of the ships and of the arrays. It's a bit irritating that teachers still teach like this. – François Andrieux Apr 23 '20 at 19:13
  • Yeah I've asked my prof why we haven't learned vectors yet and he said that my university doesn't teach vectors because they're not applicable to other languages. I feel like we should've learned them anyway but that's how it is I guess. – Ryan Khavari Apr 23 '20 at 19:20
  • 2
    @RyanKhavari That makes no sense to me. Most languages have the concept of sets. `std::vector` is incredibly relevant to many other programming languages. I'm confident you are learning C++ from a C teacher. Learning C++ as "C with classes" is very damaging and I encourage you to either search for a better class or at least complement your education with sources that teach proper C++. Consider the books from [this list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and use resources based on C++11 or later (the 2011 update to C++). – François Andrieux Apr 23 '20 at 19:27

0 Answers0