Returning a Struct from a header file

Hello, I have a working struct return from a function that only works from the main.cpp file...I would like to move the function to a header file...Here is the code as it is now, in working condition:

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
27
28
using namespace std;

struct Pairs{
int pairCard[2];
};

Pairs ReturnStruct();
int main()
{
        Pairs inmain = ReturnStruct();
        cout << inmain.pairCard[0] << inmain.pairCard[1];
        return 0;
}

Pairs ReturnStruct()
{
        Pairs infunc;
        int numbs[5]= {1, 2, 1, 2, 4}, n = 5, j=1, m = 0;
        for (int i = 0; (i < n - 1); i++)
        {                       
			for (j = i + 1; (j < n) && m<2; j++)
			{
				if (numbs[i] == numbs[j]) infunc.pairCard[m] = numbs[j];                     
			}
			m++;                                   
        }
        return infunc;
}


When I move the 'Pairs ReturnStruct' function(at the bottom of the code) to a header file, I get a number of parse and syntax errors...Any ideas why?
Last edited on
If you get errors, you always have to copy and paste them into your post (edit: you also have to read them first. Because they tell you what's wrong).
However, you need to make the function static inline if you want to move it to the header, otherwise you'll get multiple definition errors.

And about this: for (j = i + 1; ... - always declare counter variables inside the for loop, unless you need to know the value the loop stopped at.
Last edited on
Adding 'static inline' didn't fix the errors...unless I put it in the wrong place...here is all of the code...



Here are the two separate files:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <ReturnMultipleValues.h>
using namespace std;

struct Pairs{
int pairCard[2];
}ReturnStruct(), inmain, infunc;

int main()
{
        Pairs inmain = ReturnStruct();
        cout << inmain.pairCard[0] << inmain.pairCard[1];
        return 0;
}


ReturnMultipleValues.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
/*
 *  ReturnMultipleValues.h
 *  TestCodeProject
 *
 *  Created by Applied Statistics on Wed Jul 06 2011.
 *  Copyright (c) 2011 Applied Statistics. All rights reserved.
 *
 */
#include <Carbon/Carbon.h>
using namespace std;

static inline Pairs ReturnStruct()
{
		Pairs infunc;
        int numbs[5]= {1, 2, 1, 2, 4}, n = 5, j=1, m = 0;
        for (int i = 0; (i < n - 1); i++)
        {                       
			for (j = i + 1; (j < n) && m<2; j++)
			{
				if (numbs[i] == numbs[j]) infunc.pairCard[m] = numbs[j];                     
			}
			m++;                                   
        }
        return infunc;
}


Here are the errors:

ReturnMultipleValues.h:12: error: syntax error before `(' token
ReturnMultipleValues.h:16: error: syntax error before `++' token
ReturnMultipleValues.h:16: error: parse error before `for'
ReturnMultipleValues.h:16: error: parse error before `)' token
ReturnMultipleValues.h:18: error: syntax error before `++' token
ReturnMultipleValues.h:18: error: parse error before `)' token
ReturnMultipleValues.h:22: error: syntax error before `++' token

Any help would be great...
Last edited on
Well, in the header file the class Pairs is undeclared, that's why you get these errors.
Note that there is std::pair. You probably should be using std::pair<int,int> instead of your own class.
And never import entire namespaces in a header file.
How do you recommend I declare struct 'Pairs" when it has been declared in the main.cpp file..? The purpose of this class is to determine the pairs by comparing variables...(at the moment the values within the 'numbs' array are set, but they will be replaced with variables)...this is why I can't simply use std::pair.
Last edited on
By not declaring it in main.cpp, but rather in a separate header or in ReturnMultipleValues.h.
argh...declaring struct 'Pairs' in ReturnMutipleValues.h instead of main.cpp worked...unbelievable...thanks.
Topic archived. No new replies allowed.