Sign in to follow this  

Using variables in class before declared

This topic is 3336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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
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

This topic is 3336 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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