-1

I have a .h file inside I have a function that uses a Struct/Class Constructor for the default argument.

It appears on the declaration end like answered here : Where to put default parameter value in C++?

Function declaration

vector<UINT_PTR> scan(const ScanOptions& scan_options = ScanOptions());

Struct definition

struct ScanOptions {

ScanOptions()
{
    //Use some Windows.h functions here to find values
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    start_address = sysinfo.lpMinimumApplicationAddress;
    end_address = sysinfo.lpMaximumApplicationAddress;

}

UINT_PTR start_address;
UINT_PTR end_address;
};

It is answered here that:

Private structures for that file should go in the .c file, with a declaration in the .h file if they are used by any functions in the .h .

Should struct definitions go in .h or .c file?

There doesnt seem to be a way to declare a struct only to forward declare it?

C++, how to declare a struct in a header file

So do I just keep the declaration and definition of my struct inside my header or is there another recommended way?

I mean I dont really care that its global since its working and I dont think its gonna lead to problems but I really wanna know.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ojav
  • 678
  • 6
  • 22
  • 1
    I *think* this is what you're asking about. [Use function returning incomplete type as default argument](https://stackoverflow.com/questions/51139207/use-function-returning-incomplete-type-as-default-argument) –  Feb 13 '19 at 18:07

1 Answers1

2

If you use

vector<UINT_PTR> scan(const ScanOptions& scan_options = ScanOptions());

in the .h file, then the definition of ScanOptions must be visible at the point of declaration of the function.

However, instead of using a default argument, you can overload the function.

vector<UINT_PTR> scan();
vector<UINT_PTR> scan(const ScanOptions& scan_options);

with the understanding that the first function will use a default constructed ScanOptions to do its job. With that change, you can forward declare ScanOptions in the .h file and define it only in the .cpp file.

The following is perfectly valid and does not require the definition of ScanOptions to be in the .h file.

struct ScanOptions;

vector<UINT_PTR> scan();
vector<UINT_PTR> scan(const ScanOptions& scan_options);
R Sahu
  • 204,454
  • 14
  • 159
  • 270