dynamic allocatd array

Hello there. I was asked to design a program using a dynamic array of structures. I've done it before and worked fine. But this time around I can't figure out what I'm doing wrong.
Here is 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
32
  header.h
#ifndef HEADER_H
#define HEADER_H
struct Sparks
{
 char name[20];
 int number;

};

#endif // HEADER_H

strucutre.cpp
using namespace std;
main int()
{
Sparks *psparks = new Sparks[3];

psparks = 
{
 {"Special", 73},
 {"Timing", 30},
 {"Speeding", 25}
};

for (int i = 0; i < 3; i++)
     cout << psparks[i] << endl;


return 0;
};


Here is error message I get every time I run this program.
C:\Users\Misbah\Desktop\Classes\chapter 8\main.cpp|157|error: no match for 'operator=' in '*(overing + 72u) = {"Misbahu", 73}........

I even try doing initializing the struct members upon declaring the dynamic array, but it still fail.

I also place the struct in the structure.cpp, place it above the code, but still it failed.

Please someone out there should point my mistake out.

Thanks
Last edited on
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
//header.h
#ifndef HEADER_H
#define HEADER_H
struct Sparks
{
	char name[20];
	int number;
};

#endif // HEADER_H

//strucutre.cpp
#include <iostream> //to use cout
using namespace std;

//main int()
int main()
{
	Sparks *psparks = new Sparks[3]{
		{"Special", 73},
		{"Timing", 30},
		{"Speeding", 25}
	};

	for (int i = 0; i < 3; i++)
		//cout << psparks[i] << endl; //no idea how to print an spark
		;

	delete[] psparks;
	return 0;
};
Topic archived. No new replies allowed.