Structures With Multiple Files

I am trying to figure out how to declare an object of a struct, as well as assign values to its members. The issue is that I can't manipulate or use that struct in other files, or rather the struct is not recognized in the other files. Below is an example on what I mean.

1
2
3
4
5
6
7
8
9
10
11
//-----------------------------------------------
// Structures.h
//-----------------------------------------------

struct OBJECT {
	float cost;
};

OBJECT pencil;

pencil.cost = 0.44;


1
2
3
4
5
6
7
8
//-----------------------------------------------
// Main.h
//-----------------------------------------------

#include "Structures.h"
float total;

total = pencil.cost * 24;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//-----------------------------------------------
// Main.cpp
//-----------------------------------------------

#include <iostream>
#include "Main.h"
using namespace std;

int main()
{
	cout << "Cost of 24 pencils: " << total;

	return 0;
}



structures.h(11) : error C2143: syntax error : missing ';' before '.'
structures.h(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
structures.h(11) : error C2371: 'pencil' : redefinition; different basic types
structures.h(9) : see declaration of 'pencil'
main.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
main.h(8) : error C2371: 'total' : redefinition; different basic types
main.h(6) : see declaration of 'total'
main.h(8) : warning C4244: 'initializing' : conversion from 'float' to 'int', possible loss of data
main.cpp(11) : error C2088: '<<' : illegal for class

Last edited on
In Structure.h isnt you struct OBJECT while your using UNIT?
In Structure.h isnt you struct OBJECT while your using UNIT?

Yes, that's absolutely correct. However, pencil and total are still not able to be accessed in Main.cpp. Below is my attempt at using extern, but I am unsure if it is necessary. It did not make a difference when I compiled it.


1
2
3
4
5
6
7
8
9
10
11
//-----------------------------------------------
// Structures.h
//-----------------------------------------------

struct OBJECT {
	float cost;
};

extern OBJECT pencil;

pencil.cost = 0.44;


1
2
3
4
5
6
7
8
//-----------------------------------------------
// Main.h
//-----------------------------------------------

#include "Structures.h"
extern float total;

total = pencil.cost * 24;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//-----------------------------------------------
// Main.cpp
//-----------------------------------------------

#include <iostream>
#include "Main.h"
#include "Structures.h"
using namespace std;

float total;
OBJECT pencil;

int main()
{
	cout << "Cost of 24 pencils: " << total;

	return 0;
}
Why exactly are you trying to use pencil in other headers anyway? It should be local to where it is used (in this case, main).
I am trying to use a structures and variables throughout multiple files in a different project. This is just a simplified example so that I can figure it out. I was just wondering how to do it if it's possible.
Yes, but it's not very clean. You would need to make it extern like your float and do the same thing. But like I said, you should try to keep your variables as local as possible.
So should I use pointers to the variables in the main source file if say a header file had a function that used those variables?
Topic archived. No new replies allowed.