Using structs to define a function
Sep 27, 2013 at 3:06am UTC
What I'm trying to do :
1 2 3 4 5 6 7 8 9
struct foo {int x; int y;};
foo function_example(int x, int y)
{
foo temp;
temp.x = x;
temp.y = y;
return temp;
}
Works as is, however when I try doing it through seperate class files :
1 2 3
//header file for class example_class
struct foo {int x; int y;};
foo function_example(int x, int y);
1 2 3 4 5 6 7 8
//source file for example_class
foo example_class::function_example(int x, int y)
{
foo temp;
temp.x = x;
temp.y = y;
return temp;
}
I get an error telling me that foo is undefined, and that declaration of function_example(int x, int y) is incompatible with the declaration of it in the header file.
What am I doing wrong / What am I missing?
Sep 27, 2013 at 3:11am UTC
My apologies, I was declaring the struct inside the public declarations of the header file, when I should have declared it before that.
Solution :
1 2 3 4 5 6 7 8 9 10 11
//header file
#pragma once
struct foo {int x; int y;};
class example_class
{
public :
foo function_example(int x, int y);
...
...
Last edited on Sep 27, 2013 at 3:12am UTC
Topic archived. No new replies allowed.