Sign in to follow this  
stupid_programmer

Using variables in class before declared

Recommended Posts

One of the functions in my class needs to be passed a struct that is delcared private in the same class. But unless the struct is declared first I get an error, I thought there was a way to get around this. This is the class that gives me an error:
class Test
{
public:
     void Func1(my_struct data);

private:
     struct my_struct {
          int x;
          int y;
     };
};
I can fix it by doing this, but I don't want to believe its the best way:
class Test
{
private:
     struct my_struct;

public:
     void Func1(my_struct data);

private:
     struct my_struct {
          int x;
          int y;
     };
};

Share this post


Link to post
Share on other sites
Zahlman    1682
Hold on and think about this carefully. If the struct declaration is private, how is any outside code going to be able to call the function? If it won't, why is the function public?

Share this post


Link to post
Share on other sites
b1gjo3    213
Quote:
Original post by Zahlman
Hold on and think about this carefully. If the struct declaration is private, how is any outside code going to be able to call the function? If it won't, why is the function public?


agreed, if the function is supposed to do anything useful to outside code, it should be written as follows


struct my_struct {
int x;
int y;
};

class Test
{
public:
void Func1(my_struct data);
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this