Class Struct Members

I'm having some difficulty figuring out how to create an instance of a struct within one of my objects.

Here is what I am trying to accomplish:
Within the main function, I want to be able to create an object "beam". Each beam needs to be able to be given loads and reactions. Each load and reaction need to be given a magnitude, x-coordinate, and y-coordinate.

For example:
Within the main function, I want to be able to create beam Lower_FWD;. Then I want to be able to add loads and reactions:
1
2
3
4
5
6
7
Lower_FWD.load P1;
Lower_FWD.P1.x = 50;
Lower_FWD.P1.y = 100;
Lower_FWD.react R1;
Lower_FWD.R1.x = 100;
Lower_FWD.R1.y = 20;
// etc 


The first line from above leads to "invalid use of struct beam::load"

Here is what I have so far:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "beam.h"

using std::cout;
using std::cin;
using std::endl;

int main()
{
    beam Lower_FWD;
    Lower_FWD.load OBD(50, 0, 0);
    cout << Lower_FWD.OBD.x << endl;
    cin.get();
    
    return 0;
}


beam.h
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
class beam
{
    public:
    struct load
    {
        load(double magnitude, double FS, double BL) :
            mag(magnitude), x(FS), y(BL)
        {}

        double mag;
        double x;
        double y;
    };

    struct react
    {
        react(double magnitude, double FS, double BL) :
            mag(magnitude), x(FS), y(BL)
        {}

        double mag;
        double x;
        double y;
    };
};


Any help would be much appreciated. Thanks in advance.
1
2
beam::load OBD(50, 0, 0);
cout << OBD.x << endl;
main.cpp lines 11-12: What is OBD?

edit: Looks like Peter87 nailed it.
Last edited on
To make things a little clearer, only types will be capitalized:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct NameThisType
{
	double mag;
	double x;
	double y;
};

class Beam
{
public:
	NameThisType load;
	NameThisType react;
};

//...

Beam Lower_FWD;
Lower_FWD.load.mag = /*...*/;
Lower_FWD.load.x = 50;
Lower_FWD.load.y = 100;
Lower_FWD.react.mag = /*...*/;
Lower_FWD.react.x = 100;
Lower_FWD.react.y = 20;


EDIT: You mentioned you want to add loads. Do you want a Beam to be able to have an arbitrary number of loads?
Last edited on

beam::load OBD(50, 0, 0);
cout << OBD.x << endl;


OBD is just the name of a particular load. Is there any way that I can do this while keeping OBD as a member of a specific beam, so that loads and reactions can be created for specific "beams" i.e.:
1
2
Lower_FWD.OBD.x = 50;
Lower_AFT.OBD.x = 12; // etc 


You mentioned you want to add loads. Do you want a Beam to be able to have an arbitrary number of loads?


Yes, some beams will have 3 loads, some will have 4, etc.
In that case,
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

class Beam
{
public:
	std::vector<NameThisType> loads;
	std::vector<NameThisType> reacts;
};

//...

Lower_FWD.loads.push_back(NameThisType(/*...*/, 50, 100));
Lower_FWD.loads.push_back(NameThisType(/*...*/, 40, 800));

In that case,
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>

class Beam
{
public:
	std::vector<NameThisType> loads;
	std::vector<NameThisType> reacts;
};

//...

Lower_FWD.loads.push_back(NameThisType(/*...*/, 50, 100));
Lower_FWD.loads.push_back(NameThisType(/*...*/, 40, 800));



I appreciate the quick solution, but I'm looking for a way to give "beams" properties "load" and "react", which also have properties, without having to remember their placement in a vector.
Last edited on
If you want named objects, use either an std::map, or add a string member to NameThisType.

Or you can use an enum:
1
2
3
4
5
enum LoadNames{
    LoadFoo = 0,
    LoadBar,
    LoadBaz,
};
Last edited on
Topic archived. No new replies allowed.