how to create constructor for object containing array of other objects

I have tried many ways, but it seems that problem is in the string part, because I am always getting error core dumped segmentation fault.

header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct aut{
    std::string reg;
    double dom;
    int bropu;

    aut();
    aut(std::string reg, double dom, int broput);

    bool distance(double x1, double y1, double x2, double y2); }

struct parking {
    double X, Y;

    aut Auti[100];

    pa(double X, double Y, aut aut1[100], int nraut);
    pa();
    int brojautomobila();
};

main
1
2
3
4
5
6
7
8
 int main(void) {

aut a1("fist", 30.0, 6);
aut a2("second", 50.0, 5);

aut auti1[100] = {a1, a2};

pa p1(10.0, 10.0, auti1, 2);


my constructors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
aut::aut(){
  reg= nullptr;
  dom= 0.0;
  bropu= 0;
}  


aut::aut(string reg, double dom, int bropu) {
  reg= reg;
  dom= dom;
  bropu= bropu;
};
 pa::pa(double x, double y, aut auti1[100], int braut) {
  x=x;
y=y;
auti1[100] = ?????
}; 
Consider which compiles and runs (no output?):

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
#include <string>
#include <iostream>
#include <cmath>
#include <algorithm>

struct aut {
	std::string reg;
	double dom {};
	int bropu {};

	aut() {}
	aut(const std::string& reg, double dom, int broput);

	//bool distance(double x1, double y1, double x2, double y2);
};

struct parking {
	double X {}, Y {};
	aut Auti[100] {};
	int braut {};

	parking(double X, double Y, aut aut1[100], int nraut);
	parking() {}
	//int brojautomobila();
};

aut::aut(const std::string& reg_, double dom_, int bropu_) : reg(reg_), dom(dom_), bropu(bropu_) {}

parking::parking(double x_, double y_, aut auti1[100], int braut_) : X(x_), Y(y_),  braut(braut_) {
	std::copy_n(auti1, 100, Auti);
}

int main()
{
	aut a1("fist", 30.0, 6);
	aut a2("second", 50.0, 5);

	aut auti1[100] = {a1, a2};

	parking p1(10.0, 10.0, auti1, 2);
}


So now elements of struct aut are setting properly, but when I try to initialize object parking like this:
1
2
3
4
5
6
7
8
9
int main()
{
aut a1("fist", 30.0, 6);
aut a2("second", 50.0, 8);

aut auti1[100] = {a1, a2};

parking pa1(10.0, 10.0, auti1, 2);
}

I get core dumped, segmentation fault.
How can I make this work, do I need another constructor or something else because of the array of auts?
Seeplus's code works for me. If you're getting a core dump, it's probably in another part of the code. Can you post your entire program?
Everything works fine without this
1
2
3
4
5
aut auti1[100] = {a1, a2};

parking pa1(10.0, 10.0, auti1, 2);
}


Here is entire program, ignore other functions, I don't think they are correct, but can't test them until I get constructor and initialization right.
header file:
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
#include <iostream>
#include <string>

struct aut {
    std::string reg;
    double dom;
    int bropu;

    
    aut();
    
    aut(std::string reg, double dom, int bropu);

    bool distance (double x1, double y1, double x2, double y2);
    std::string getreg();
};



struct parking {
    double X, Y;

    aut Auti[100];

    parking(double X, double Y, aut auti[100], int nraut);
    parking();
    
    int nraut();

    double getx();

    double gety();

    aut biggestdom();
    
    bool insidedom(parking second);

    bool removeaut(std::string reg);


};


declaration of functions:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include "park.h"
#include <algorithm>

using namespace std;
aut::aut(){
  reg = nullptr;
  dom = 0.0;
  bropu = 0;
}  
aut::aut(string reg_, double dom_, int bropu_) : reg(reg_), dom(dom_), bropu(bropu_) {};

 bool aut::distance (double x1, double y1, double x2, double y2){
    const double dx = x1 - x2, dy = y1 - y2;
    const double distance = std::sqrt(dx * dx + dy * dy);
  return distance <= dom;
  };

string aut::getreg(){
  return reg;
}


parking::parking(double x, double y, aut auti[100], int nraut) : X(x), Y(y) {
	copy_n(auti, 100, Auti);
}

int parking::nraut(){
  int i;
  for(i = 0; i < 100 ; ++i) 
  Auti[i];
  return i;
}


aut parking::biggestdom() {
  int i;
  double biggest = Auti[0].dom;
  for(i = 0; i < nraut() ; ++i) {
    if(Auti[i].dom > biggest)
    biggest = Auti[i].dom;
  }
  return Auti[i];
}


bool parking::insidedom(parking second) {
    const double dx1 = second.X - X, dy1 = second.Y - Y;
    const double distanceparking = sqrt( dx1 * dx1 + dy1 * dy1 );
    int i;
    for ( i = 0; i < nraut(); ++i ) {
      if(Auti[i].dom >= distanceparking) 
      return 1;
      else return 0;
    }
}

double parking::gety() {
  return Y;
}

double parking::getx() {
  return X;
}

bool parking::removeaut(std::string reg){

}


main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "park.h"

using namespace std;

int main(void) {

aut a1("fist", 40.0, 3);
aut a2("second", 10.0, 8);

aut auti1[100] = {a1, a2};

parking pa1(10.0, 10.0, auti1, 2);

return 0;
};
Try this which compiles and runs OK with VS2019:

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
#include <algorithm>

struct aut {
	std::string reg;
	double dom {};
	int bropu {};

	aut() {}
	aut(const std::string& reg, double dom, int bropu);

	bool distance(double x1, double y1, double x2, double y2) const;
	std::string getreg() const;
};

struct parking {
	double X {}, Y {};
	aut Auti[100] {};
	size_t nraut {};

	parking() {}
	parking(double X, double Y, const aut auti[100], size_t nraut);

	double getx() const;
	double gety() const;

	aut biggestdom() const;
	bool insidedom(parking second) const;
	bool removeaut(const std::string& reg);
};

aut::aut(const std::string& reg_, double dom_, int bropu_) : reg(reg_), dom(dom_), bropu(bropu_) {};

bool aut::distance(double x1, double y1, double x2, double y2) const {
	const auto dx {x1 - x2}, dy {y1 - y2};
	const auto distance {std::sqrt(dx * dx + dy * dy)};

	return distance <= dom;
};

std::string aut::getreg() const {
	return reg;
}

parking::parking(double x, double y, const aut auti[100], size_t nraut_) : X(x), Y(y), nraut(nraut_) {
	std::copy_n(auti, 100, Auti);
}

aut parking::biggestdom() const {
	size_t big {};
	double biggest {Auti[0].dom};

	for (size_t i = 1; i < nraut; ++i)
		if (Auti[i].dom > biggest) {
			biggest = Auti[i].dom;
			big = i;
		}

	return Auti[big];
}

bool parking::insidedom(parking second) const {
	const auto dx1 {second.X - X}, dy1 {second.Y - Y};
	const auto distanceparking {sqrt(dx1 * dx1 + dy1 * dy1)};

	for (size_t i = 0; i < nraut; ++i)
		if (Auti[i].dom >= distanceparking)
			return true;

	return false;
}

double parking::gety() const {
	return Y;
}

double parking::getx() const {
	return X;
}

bool parking::removeaut(const std::string& reg) {
	//TO DO
}

int main()
{
	aut a1("fist", 40.0, 3);
	aut a2("second", 10.0, 8);
	aut auti1[100] {a1, a2};

	parking pa1(10.0, 10.0, auti1, 2);
}

It works!!! Thank you so much, I appreciate your help.
Topic archived. No new replies allowed.