Difficulties implementing a struct defined in a class

Hello,
I am having difficulty attempting to implement a struct defined in a class. I've researched this problem but am still not understanding the invalidity of what I'm trying to do. I have a class, Structs which is just a header file with a class definition and a member which is a struct type:

Snippet of structs.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
26
#ifndef _STRUCTS_H
#define _STRUCTS_H
#include <cstdlib>
#include <string>

using namespace std;


//A class contianing commonly used data objects and structures
class Structs
{
      public:
         Structs();
         struct Pos
         {
                int x;
                int y;
                Pos(int i, int j)
                {
                        x = i;
                        j = y;
                }
         };
};

#endif 


The structs.cpp file, at this point is trivial as the struct Pos implementation is defined in the header file (I provided a sample of implementing it in the structs.cpp file below)

The struct is implemented in a class plot.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef _PLOT_H
#define _PLOT_H

#include <cstdlib>
#include <string>
#include "structs.h"

using namespace std;

class Plot
{
      public:
         Plot(int x, int y); //constructor where Structs resource is implemented.
         void setResource(int res);
         string showResource(int res);
      private:
         Structs resource;
      
};

#endif   


The error is thrown in the plot.cpp file with the constructor:

1
2
3
4
5
6
7
. . .
Plot::Plot(int x, int y) 
{
     resource.Pos(x,y);
}
. . .

Compilation throws the error:

invalid use of 'struct Structs::Pos'

Placing the implementation in the .cpp file throws the same error.

Placing in .cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//defined in header file structs.h:
struct Pos;
. . . 
//implemented in structs.cpp:
Structs::Pos
{
                int x;
                int y;
                Pos(int i, int j)
                {
                        x = i;
                        j = y;
                }
};


My intention with the Structs class is to define a number of different structs etc. which I can use from one place. For the life of me, I see no invalid type implementation with this. What am I possibly missing?

Thanks!
Last edited on
Whew. Solved it (I think)

All i did was create a member funtion (or method if you choose) in structs.h called setPos(int a, int b);
The implementation in structs.cpp
1
2
3
4
void Structs::setPos(int a, int b)
{
     Pos(a,b);
} 


worked. Or at least compiled. I'm kind of miffed that I had to name copy the same two variables like that, but we'll optimize later.
I think the issue is that this:

1
2
3
4
5
6
7
8
9
10
         struct Pos
         {
                int x;
                int y;
                Pos(int i, int j)
                {
                        x = i;
                        j = y;
                }
         };


Does not create an instance of Pos for you, it simply defines a type you can use.
That is a really good point. I think I tried this at one point:
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
... 
//in structs.h:
class Structs
{
      public:
         Structs();
         struct Pos 
         {
                int x;
                int y;
                Pos(int i, int j)
                {
                        x = i;
                        j = y;
                }
         };
         Pos pos(int a, int b);

};

//And implemented in plot.cpp:
Plot::Plot(int x, int y) 
{
     resource.pos(x,y);
}


Throws the error: [Linker error] undefined reference to `Structs::pos(int,int}'

This is compiling with Dev-C++ 4.9.9.2 btw. I'm pretty sure gcc would say the same or similar enough thing.

Not sure why that wouldn't work either. My solution above works because I'm defining static variables of resource.Pos.x and resource.Pos.y respectively. My inner compiler is telling me though, just as you said, to have a defined object of that type within it's own class. But perhaps that's what's raising the linker errors.
Last edited on
Try something like:

1
2
3
4
5
6
7
8
9
10
         struct Pos 
         {
                int x;
                int y;
                Pos(int i, int j)
                {
                        x = i;
                        j = y;
                }
         } my_instance;
Yeah, I think I had tried that at one point, My cpp is pretty rusty, but it wont let me implenent the constructor in the Pos struct. So when I call
 
resource.my_instance(x,y)

It complains that it cant find a match to call{Structs::Pos)(int&, int&)

Why the type reference escapes me. The following also throw the same error:
1
2
3
resource.my_pos(x,y);
     resource.my_pos.x =x;
     resource.my_pos.y = y;


BTW, I had changed my_instance to my_pos if you were wondering.
Last edited on
No one noticed this?:

1
2
3
4
5
                Pos(int i, int j)
                {
                        x = i;
                        j = y;  // This assignment is backwards
                }
Oh yeah, that must have been an err. . . glitch or something in the form post method for this site. . . yeah . . . that has to be it.

lol, I'm sure it would have cropped up sooner or later.
You want each call to Plot to create a new Pos object? If so, you can use an STL container (like vector) like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Structs
{
      public:
         Structs();
         struct Pos 
         {
                int x;
                int y;
                Pos(int i, int j)
                {
                        x = i;
                        y = j;
                }
         };
         
         vector<Pos> positions;
};

//And implemented in plot.cpp:
Plot::Plot(int x, int y) 
{
     resource.positions.push_back(Pos(x,y));
}

Info on STL containers -> http://cplusplus.com/reference/stl/
Actually, I had already created the vector. The Pos and Plot objects are one to one, so I've created (am creating) a vector of Plot objects. Each plot will contain one position, so no need for a vector in Structs. Now you're probably wondering why I just didn't define it in Plot. I do have my reasons is all I can say right now. But my main problem that I was having was implementing the plot object which seems to be ok for now.
Ok, then why don't you just do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//in structs.h:
class Structs
{
      public:
         Structs();
         struct Pos 
         {
                int x;
                int y;
         };

         Pos pos;

};

//And implemented in plot.cpp:
Plot::Plot(int x, int y) 
{
     resource.pos.x=x;
     resource.pos.y=y;
}

???
See above:


Why the type reference escapes me. The following also throw the same error:
1
2
3
resource.my_pos(x,y);
     resource.my_pos.x =x;
     resource.my_pos.y = y;



It should work. But If you look at the first reply, I had solved the issue. I was just curious why your suggestion and my initial question didn't work at all.
Topic archived. No new replies allowed.