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!