Passing data structure to a class in another fille

Hello! I am pretty new in C++ and I apologise in advance if my questions seems silly.

I have a data structure declared in main.cpp and I want to pass that data to another class located in myClass.cpp (or myClass.h) without declaring it again.

Snippet from main.cpp:
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
#include "myClass.h"

#pragma	pack(1)
typedef struct
{
	float		analog;
	unsigned char	marker;
} WaveformDataReal;

typedef struct
{
	unsigned short	value;
} WaveformDataInteger;
#pragma	pack()

int main()
{
	WaveformDataReal data;
	myClass a;

	...

	a.Close(data);

	...
}


and this is my function in myClass.cpp:

1
2
3
4
5
int myTek::Close(?????)
{
	...

}



I dont really know what terminology to search this under and most results I find uses extern but the examples i found are in the same file.

I really appreciate any help that you can offer.
Thank you in advance.

You need to put those declarations into a header and #include it. Notice that the C style of typedef'ing structs is not needed in C++:

1
2
3
4
5
6
struct Foo {
    int bar;
};

Foo a;
a.bar = 2;

Also, are you sure you need to eliminate data structure padding?
Hi Felipe,

Do you mean that I should place this code in, say, a struc.h:

1
2
3
4
5
6
7
8
9
10
11
12
#pragma	pack(1)
typedef struct
{
	float		analog;
	unsigned char	marker;
} WaveformDataReal;

typedef struct
{
	unsigned short	value;
} WaveformDataInteger;
#pragma	pack() 


and include it in both main.cpp and myClass.cpp? I will try that out and I think that would work.

and yup, i made changes to the struct.. also, yes, i need to eliminate paddings to communicate with my device. So far, no problem to transmit the data (if everything is written in a single file).

Thank you kind sir!
Just remember to use include guards for your header files, to prevent double definition:

1
2
3
4
5
6
#ifndef MYHEADER_H
#define MYHEADER_H

// code goes here

#endif 
Yup, did that.

And your suggestion works! Thank you very much!
Topic archived. No new replies allowed.