Where to place C++ Struct ?

Hi
I'm just trying to break up my program and am unsure how to handle a struct in c++. I have a function I am calling which may or may not be in the same file as the main function.

I have one source file and a header file. I want to break up the structure but putting it's definition in the source.cpp file and a declaration in the source.h file but I can't seem to get this to work.

What is the conventional way, or how should I structure this? I definitely want to break things up as much as possible.

Thanks




source.h
1
2
3
4
5
6
#ifndef SOURCE_H_INCLUDED
#define SOURCE_H_INCLUDED

// ? I AM NOT SURE WHAT GOES IN HERE?

#endif // SOURCE_H_INCLUDED 



source.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <cstdlib>


using namespace std;

struct mystruct
{
    
string str;
string astr;
int myint;

};




Main Function and myfunction
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
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>
#include <cstdlib>


using namespace std;



void myfunction(void *pointer)
{

    mystruct *st = (mystruct*)pointer;
    cout << st->str << endl;
        cout << st->astr << endl;
            cout << st->myint << endl;

}


int main()
{
  string string_1= "Test";
  string string_2= "Best";
  int integar = 6;

mystruct ss;

ss.str = mystring;
ss.astr = thestring;
ss.myint = integar;

myfunction(&ss);


system("pause>null");

}
Why the void* and C-style cast? What if I call the myfunction with int*?


The question is: Who has to know what?

The main() has to reserve space for object ss, so it has to know the size of mystruct.
The main() has to know that myfunction() exists.
The myfunction() has to dereference the members of mystruct and thus know about them.

The mystruct could have a constructor. It would need to know about mystruct.

mystruct.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <string>
using std::string;

struct mystruct
{
  string str;
  string astr;
  int myint;
  mystruct( string, string, int );
};
#endif 

mystruct.cpp
1
2
3
4
5
6
#include <utility>
#include "mystruct.h"

mystruct::mystruct( string a, string, b, int c )
: str( std::move(a) ), astr( std::move(b) ), myint( c )
{}

myfunction.h
1
2
3
4
5
#ifndef MYFUNCTION_H
#define MYFUNCTION_H
struct mystruct; // predeclaration
void myfunction( const mystruct & st );
#endif 

myfunction.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "mystruct.h"
using std::cout;
using std::endl;

void myfunction( const mystruct & st )
{
  cout << st.str << endl;
  cout << st.astr << endl;
  cout << st.myint << endl;
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string>
#include "mystruct.h"
#include "myfunction.h"
using std::string;

int main()
{
  string string_1 {"Test"};
  string string_2 {"Best"};
  int integar {6};
  mystruct ss {string_1, string_2, integar};
  myfunction( ss );
  return 0;
}
That's an excellent example. Thank you very much.
I used the void cast for something else so I wanted to keep it. That's the easy part, it was breaking everything up which became a bit confusing.

Cheers.
Topic archived. No new replies allowed.