Getting frustrated with Initializing array of objects

Pages: 12
Hi, my C++ professor is requiring me to initialize an array of objects in this exact format (AKA copy and paste this into the code and leave it 100% intact):

1
2
3
4
5
6
7
8
9
10
11
  Invoice[] invoices =
{
	 new Invoice(83, "Electric sander", 7, 57.98),
	 new Invoice(24, "Power saw", 18, 99.99),
	 new Invoice(7, "Sledge hammer", 11, 21.5),
	 new Invoice(77, "Hammer", 76, 11.99),
	 new Invoice(39, "Lawn mower", 3, 79.5),
	 new Invoice(68, "Screwdriver", 106, 6.99),
	 new Invoice(56, "Jig saw", 21, 11.00),
	 new Invoice(3, "Wrench", 34, 7.5)
};


My issue is that I have no idea on how to receive this array. I'm completely at a loss. Do I use some time of pointer, or maybe a special type of class constructor?

This is the current constructor I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Invoice {
	private:
		int partNum, quantity;
		double price;
		string partDescription;
	public:
		Invoice(int, string, int, double);
		Invoice();
};

Invoice::Invoice(int num, string descrip, int quan, double cost)
{//class constructor
	partNum = num;
	partDescription = descrip;
	quantity = quan;
	price = cost;
}

Invoice::Invoice()
{

};
Last edited on
I'd really appreciate if someone could also explain what the first code means also, that'd be fantastic lol.
I've been googling all over and can't seem to find a solution :(
That array IS creating the invoices. They are using the constructor you provided in your class to construct each object in that array. That is an array of 'Invoice' objects, and each index in that array is one of those objects.

Check out: http://www.cplusplus.com/doc/tutorial/arrays/ and look at 'Initializing Arrays'

What are you supposed to do with that array?

Though, I do wonder why your professor is making you dynamically allocate them all. An array like doesn't need to be created dynamically - you're giving it all the initial values. Hmm.
Well, in any case, make sure you delete those elements before they go out of scope or you'll get a memory leak.
I'm not exactly sure why he's making us do it this way. I was able to do it easily in another format lol. He just wants us to write a function to sort the data within the objects. It's a beginner's class. This is probably a dumb question, but is a memory leak permanent? or does it only effect the program I'm writing? Anyhow, it's giving me an error when I compile because it says "empty attribute block is not allowed," "expected an identifier," and "missing ; before [." Any clue on how to fix this?
It's not permanent, but it will persist throughout the execution of your program. Really bad memory leaks can suck up the entire RAM, but yours won't. You still need to avoid them at all costs, though.

It's when you create an object on the heap (with 'new'/'malloc') but don't delete it before you get rid of the pointer. The object still exists, but there's no way of getting back to it.

Think of it like this: You have a string attached to a ball, and you throw the ball out into space with the string in your hand. If you cut that string, you cannot get that ball back, but that ball is still in space. That's like a memory leak. That ball is your object and that string is your pointer. That ball is taking up your memory, but you can't get rid of it to get your memory back. But if you pull the ball in and THEN cut the string (like calling 'delete' before making the pointer null), then that ball won't be floating out in the void anymore and you have safely gotten your memory back.

To avoid a memory leak, make sure that everything that is created with 'new' is also freed with 'delete'. You only have to do this with dynamically-allocated objects.

Your array declaration should look like this:

1
2
3
Invoice invoices[] = {
//same thing in here
};


The '[]' was in the wrong place

For example:
1
2
3
4
5
	myClass classArray[] = {
		myClass("object1", 1),
		myClass("obejct2", 2),
		myClass("object3", 3)
	};
Last edited on
my C++ professor is requiring me to initialize an array of objects in this exact format (AKA copy and paste this into the code and leave it 100% intact):

Are you sure he isn't your java professor?
THAT MAKES SO MUCH SENSE GRRRR. I thought that the bracket was in the wrong place, but he told me to literally copy and paste that into my code. I figured that you could declare arrays similar to how you can declare pointers, and that I was doing something wrong.
1
2
3
4
//for example
int* i,j,k; //all of these would be pointers.

int[] a,s,d; //I figured it would make all of these arrays lol. that's why I didn't know what was wrong :P 

Almost. You have the right concept, but the syntax is weird.

int* i,j,k;
Only 'j' will be a pointer.
Declare it like this if you want all 3 to be pointers: int *i, *j, *k;
I always put the '*' next to the variable name instead of the type name (unless I'm declaring a function that returns a ptr, then I put it on the left like this: int* returnsAPointer()).

int[] a,s,d;
I'm not sure how to interpret this syntax, but you put the '[]' after the variable name, not the variable type.
You could do this: int a[3], b[3], c[3];
But you must give it a size if you don't give it a list of initial values (like your professor did).
Hmmm I'm still getting errors. It's saying that "no suitable constructor exists to conver from "Invoice *" to "Invoice"... now what :O. Sorry, I'm really clueless and its final time :(
Could you show me your code?
I'm trying to get it to compile before I write the sorting functions. What you can see is pretty much what I have, but here's everything:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;

class Invoice {
	private:
		int partNum, quantity;
		double price;
		string partDescription;
	public:
		Invoice(int, string, int, double);
		Invoice();
};

Invoice::Invoice(int num, string descrip, int quan, double cost)
{//class constructor
	partNum = num;
	partDescription = descrip;
	quantity = quan;
	price = cost;
}

Invoice::Invoice()
{

}

//initialize array of invoices
Invoice invoices[] =
{
	 new Invoice(83, "Electric sander", 7, 57.98),
	 new Invoice(24, "Power saw", 18, 99.99),
	 new Invoice(7, "Sledge hammer", 11, 21.5),
	 new Invoice(77, "Hammer", 76, 11.99),
	 new Invoice(39, "Lawn mower", 3, 79.5),
	 new Invoice(68, "Screwdriver", 106, 6.99),
	 new Invoice(56, "Jig saw", 21, 11.00),
	 new Invoice(3, "Wrench", 34, 7.5)
};


int main() {



	system("pause");
	return 0;
}


That's literally it, but I don't expect that to have any effect on what is going wrong. I feel like its a contextual error from what he has, but idk!
Well, there's your problem! Get rid of the 'new's in that array. There's no need to create that dynamically. You actually CAN'T initialize the array with those 'new's like that.

Is your professor getting C++ mixed up with Java?
He or She should know that you can't do this in C++.


This compiles on my machine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
using namespace std;

class Invoice {
private:
	int partNum, quantity;
	double price;
	string partDescription;
public:
	Invoice(int, string, int, double);
	Invoice();
};

Invoice::Invoice(int num, string descrip, int quan, double cost)
{//class constructor
	partNum = num;
	partDescription = descrip;
	quantity = quan;
	price = cost;
}

Invoice::Invoice()
{

}

//initialize array of invoices
Invoice invoices[] =
{
	Invoice(83, "Electric sander", 7, 57.98),
	Invoice(24, "Power saw", 18, 99.99),
	Invoice(7, "Sledge hammer", 11, 21.5),
	Invoice(77, "Hammer", 76, 11.99),
	Invoice(39, "Lawn mower", 3, 79.5),
	Invoice(68, "Screwdriver", 106, 6.99),
	Invoice(56, "Jig saw", 21, 11.00),
	Invoice(3, "Wrench", 34, 7.5)
};


int main() {



	system("pause");
	return 0;
}


I'm not sure what you need to do with that array, but it should compile on your machine.
Last edited on
None of those news are needed.
It looks like your professor is trying to use java syntax instead of c++.
Hmmmm I just fear that there was a way to do it with its original context. Are we 100% sure there isn't? I just don't want to get marked down for failing to figure it out. BTW Thanks so much guys, the feedback is great :)
Looked it up and he also teaches multiple Java classes.... hmmm... he probably messed up then, lol. I'm really annoyed with him. He refuses to answer questions for me, and also misleads me and makes me waste 4 hours of my time that I could've been using for other finals. So frustrating.
Make a note of the syntax in your submission. Your professor should see that and realize he made the mistake.
Thank you so much Jayhawk! I wish I could do something for you, you've helped me a bunch!
Oh last question, I see that you're learning openGL. I was wondering if you'd know of a way to integrate graphics into a pre-existing program that uses the console. I wrote a fully functioning game of minesweeper using the console, and I wanted to add graphics to make it the real deal.
Well, integrating graphics isn't always the easiest thing to do. OpenGL is nice, but it takes a bit of code to get something running.

However, minesweeper is 2D, so I might recommend SFML: http://www.sfml-dev.org/

It's an easy-to-use 2D graphics API, and plenty of us on here use it (myself included). It makes doing 2D graphics 1000x easier than doing the raw OpenGL.
Since you have a console version working, porting it to SFML would be much easier than doing it from scratch.
Pages: 12