Array of Objects

I have been trying to create an array of objects but having difficulty and cannot find info pertaining to it. Don't understand why the code below will not run


1
2
3
4
5
6
7
 File* files;

 for  (unsigned int i = 0; i < nofiles; i++)
 {
   file[i] = new File()
 }
About dynamic arrays. http://www.cplusplus.com/doc/tutorial/dynamic/
However it would be much easier to use a vector<File>. It will save you lots of headache pointer can give you. http://www.cplusplus.com/reference/vector/vector/

As a general rule. Don't use pointers and dynamic arrays unless you absolutely have to. In general the stl containers are much easier to use.
What do you mean "will not run"? The code you've posted won't even compile, because line 5 is missing a semicolon.
files is a pointer to File or an array of File. Not an array of pointer to File.

To achieve this:
1
2
3
4
5
6
 File** files = new File*[nofiles]; // Note: second * and dynamically acquiring the array of pointer to File

 for  (unsigned int i = 0; i < nofiles; i++)
 {
   file[i] = new File();
 }
Last edited on
thomas1965. Thanks I'll take a look into that options. I was not thinking that I could use vectors to store class objects.
@Coder777 this was along what i was thinking and it makes sense.

@MikeyBoy, So would the code run if there is semicolon, obviously that was a typo. Just address the problem if you can or don't thanks.
@coder777 I have some understanding of whats going on but not full. I understand pointer to a pointer etcs. Could you explain in a bit more detail whats going on in that code you have written plz?
So would the code run if there is semicolon, obviously that was a typo.

It would run, yes. It wasn't "obvious" that that was a typo - how can I possibly know whether or not you're correctly copying and pasting your code here?

Just address the problem if you can or don't thanks.

When someone here asks you a question, it is because they want to be better able to help you. One would think that you'd want us to be better able to help you.

You'll find you're more likely to get the help you want, if you engage with us and help us to find out what we need to know.

Of course, that assumes that getting the help you want is more important to you than snarking at people here. That's false a depressingly high percentage of the time, in here.

I will ask again, on the assumption that you actually want to be helped:

What do you mean by "will not run"?

Do you have a compilation error? A runtime crash? Something else? What behaviour are you seeing that is not what you expect?

Help us to help you.
Last edited on
@MikeyBoy my apologies was a bit tired l and now that I've looked at it, I've misread what you said. Again apologies

Here is what I am trying to do. I want to store an array of Objects of the type File of size nofiles. error message:
binary '=' : no operator found which takes a right-hand operand of type 'FileOrganizer *' (or there is no acceptable conversion)

Last edited on
I found the answer after doing some reading and testing.
When File* files = new File[nofiles]; files is pointing to the object created at a memory location. Consecutive objects of number nofiles is created. To access those objects just offset the pointer files

1
2
3
4
5
6
File* files = new File[nofiles];
	for (unsigned int i = 0; i < nofiles; i++)
	{
		files[i].setName( ptr->at(i));
		files[i].setLocation(sourceFolder->getLocation() );
	}


Thanks for the responses though, appreciate it
@MikeyBoy my apologies was a bit tired l and now that I've looked at it, I've misread what you said. Again apologies

No worries - we all have our bad days :)

Glad you were able to figure out your problem!
Topic archived. No new replies allowed.