Help requested with a dynamic array.

Mar 10, 2013 at 7:13pm
SO I'm very new to c++ (as in a few weeks) and I've been reading the C++ Primer Plus by Stephen Prata. At the end of each chapter he outlines programming exercises. The one I'm stuck on involves using new to create a dynamic array. Here's the code
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
#include <iostream>
#include <string>

void tag(int);
using namespace std;

struct candybar
{
	char name[20];
	double weight;
	int calories;
};

int main()
{
	tag(0);
	tag(1);
	tag(2);
	return 0;
}

void tag(int n)
{
		
    candybar *pc = new candybar [3];
    pc[0] = {"Mars" , 2.75, 245};   //Error on this line
    pc[1] = {"Butterfinger", 2.85, 300};  //Error on this line
    pc[2] = {"Twix", 2.12, 150};  //Error on this line
	cout << "The Candy Company is releasing a new treat called " << pc[n].name << " which weighs a total of " << pc[n].weight ;
	cout <<" pounds and only contains " << pc[n].calories << " calories.\n";
};


I'm using Visual Studio Express 2012 and I'm getting the build error "Error C2059: syntax error : '{'" and intellisense is telling me it "expected an expression." I could use some help understanding this, thanks in advance.
Mar 10, 2013 at 7:56pm
You can only use the {}-syntax to initialize the object when the object is created. When creating an array with new candybar [3] the objects in the array will be default initialized, and in C++03 there is no way around this.

Instead of
pc[0] = {"Mars" , 2.75, 245};
do would have to do something like:
1
2
3
std::strcpy(pc[0].name, "Mars");
pc[0].weight = 2.75;
pc[0].calories = 245;


In C++11 you can initialize the objects in the array when the array is created like this:
1
2
3
4
5
6
candybar *pc = new candybar [3] 
{
	{"Mars" , 2.75, 245}, 
	{"Butterfinger", 2.85, 300}, 
	{"Twix", 2.12, 150}
};
You can also do it the way you did it if you add candybar after the = symbols.
1
2
3
pc[0] = candybar{"Mars" , 2.75, 245};
pc[1] = candybar{"Butterfinger", 2.85, 300};
pc[2] = candybar{"Twix", 2.12, 150};
Last edited on Mar 10, 2013 at 8:01pm
Mar 10, 2013 at 7:58pm
Your code is fine, it should compile without errors. http://liveworkspace.org/code/5waqH$0

Requires compiler support for uniform initialization http://www.stroustrup.com/C++11FAQ.html#uniform-init

The November 2012 CTP would have it. http://www.microsoft.com/en-us/download/details.aspx?id=35515


You need to delete[] the array before you return from tag(), or else there would be a leak.
Better still, just use a std::vector<> instead http://www.mochima.com/tutorials/vectors.html
Mar 10, 2013 at 8:58pm
@JLBorges I tried installing the ctp and the problem persists.
Mar 11, 2013 at 1:15am
Have you changed the project configuration to switch to the new compiler?
See the section on 'Installation and Usage' http://blogs.msdn.com/b/vcblog/archive/2012/11/02/visual-c-c-11-and-the-future-of-c.aspx

Note: I don't have a Windows machine at hand; so I can't test it with the Microsoft compiler.

Mar 11, 2013 at 2:54am
Yes, I went through all of the steps of the installation.
Mar 11, 2013 at 4:04am
The November 2012 CTP seems to have some issues with uniform initialization.

In addition to the code in the OP not being accepted, one cannot specify the type explicitly pc[0] = candybar{"Mars", 2.75, 245};.
Mar 11, 2013 at 2:07pm
Thanks, brutaltomrammen and cire.

This should compile cleanly:
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
49
50
51
#include <iostream>
#include <string>
#include <vector> // *** added

void tag(int);
using namespace std;

struct candybar
{
	std::string name; // *** changed
	double weight;
	int calories;
};

int main()
{
	tag(0);
	tag(1);
	tag(2);
}

void tag(int n)
{

    //candybar *pc = new candybar [3];
    std::vector<candybar> pc(3) ; // dynamic array of 3 candybars

    candybar a = {"Mars" , 2.75, 245}; pc.push_back(a) ;
    candybar b = {"Butterfinger", 2.85, 300}; pc.push_back(b) ;
    candybar c = {"Twix", 2.12, 150}; pc.push_back(c) ;

    cout << "The Candy Company is releasing a new treat called " << pc[n].name 
         << " which weighs a total of " << pc[n].weight ;
    cout <<" pounds and only contains " << pc[n].calories << " calories.\n";
}

void tag_1(int n) // or
{

    candybar *pc = new candybar [3];

    candybar a = {"Mars" , 2.75, 245}; pc[0] = a ;
    candybar b = {"Butterfinger", 2.85, 300}; pc[1] = b ;
    candybar c = {"Twix", 2.12, 150}; pc[2] = c ;

    cout << "The Candy Company is releasing a new treat called " << pc[n].name 
         << " which weighs a total of " << pc[n].weight ;
    cout <<" pounds and only contains " << pc[n].calories << " calories.\n";

    delete[] pc ;
}

Topic archived. No new replies allowed.