How to access struct member from class constructor

Jul 8, 2020 at 7:30pm
Hey guys, I am trying to pass a struct from main function to a class constructor and want to access it. But it's not working. It's saying "error: invalid use of incomplete type 'struct SS'"

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

/* ###### Patient.h ###### */

#ifndef PATIENT_H
#define PATIENT_H

class Patient
{
    public:
        Patient();
        Patient(struct SS&);

};
#endif // PATIENT_H


/* ##### Patient.cpp ###### */

#include <iostream>
#include "Patient.h"

Patient::Patient() { }

Patient::Patient(SS& ss)
{
    std::cout << ss.address << std::endl;
    std::cout << pmv.firstName << std::endl;

}

/* ##### main.cpp ###### */

#include <iostream>
#include "Patient.h"

using namespace std;

struct SS
{
    std::string firstName = "First";
    std::string middleName = "Middle";
    std::string lastName = "Last";
    std::string address = "Address";
    std::string city = "City";
    std::string state = "State";
    int zipCode = 00000;
    long phoneNum = 9292192212;
    std::string emergencyName = "John Doe";
    long emergencyPhnNum = 6462121282;
} sObj;

int main()
{
    Patient obj(sObj);
    return 0;
}
Last edited on Jul 8, 2020 at 8:16pm
Jul 8, 2020 at 7:35pm
Put
1
2
3
4
5
6
Patient::Patient(SS& ss)
{
    std::cout << ss.address << std::endl;
    std::cout << pmv.firstName << std::endl;

}

below the definition of struct SS.

Edit: You need to put your struct SS in a file that you can #include before the Patient(SS&) constructor.

Both main.cpp and patient.cpp need to know the full definition of the SS struct.

Also, avoid global objects like sObj. Just put it in main.

1
2
3
4
5
int main()
{
    SS sObj;
    Patient obj(sObj);
}
Last edited on Jul 8, 2020 at 7:41pm
Jul 8, 2020 at 7:37pm
You need a "forward reference" of struct SS in Patient.h:

1
2
3
4
5
6
7
8
9
10
11
#ifndef PATIENT_H
#define PATIENT_H

struct SS;   // tell the compiler that struct SS actually exists (somewhere)

class Patient
{
    public:
        Patient();
        Patient(SS&);    // don't need the word 'struct' here
...


BTW, what's up with the anonymous struct object pmv?
What's the point?
Jul 8, 2020 at 8:09pm
I put "struct SS;" in my header file like you say. And this should automatically include it in my "Patient.cpp".
But I am still getting the same error :(


both the &sObj and &ss are the same but can't access struct members.
Last edited on Jul 8, 2020 at 8:18pm
Jul 8, 2020 at 8:17pm
Ganado has the right answer here. Something like:

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
/* ###### Patient.h ###### */

#ifndef PATIENT_H
#define PATIENT_H

struct SS;

class Patient
{
    public:
        Patient();
        Patient(SS&);

    public:
        struct
        {
            std::string firstName, middleName, lastName, address, city, state;
            int zipCode;
            long phoneNum;
            std::string emergencyName;
            long emergencyPhnNum;
        } pmv;
};
#endif // PATIENT_H


/* ##### Patient.cpp ###### */

#include <iostream>
#include "Patient.h"

struct SS
{
    std::string firstName = "First";
    std::string middleName = "Middle";
    std::string lastName = "Last";
    std::string address = "Address";
    std::string city = "City";
    std::string state = "State";
    int zipCode = 00000;
    long phoneNum = 9292192212;
    std::string emergencyName = "John Doe";
    long emergencyPhnNum = 6462121282;
};

Patient::Patient() { }

Patient::Patient(SS& ss)
{
    std::cout << ss.address << std::endl;
    std::cout << pmv.firstName << std::endl;

}


/* ##### main.cpp ###### */

#include <iostream>
#include "Patient.h"

using namespace std;

int main()
{
    SS sObj;
    Patient obj(sObj);
    return 0;
}

Jul 8, 2020 at 8:39pm
Thanks guys. Now I start loving c++. So much flexibility :D
Topic archived. No new replies allowed.