Saint
Saint

Reputation: 61

Is there a way to declare a pointer in a header file and instantiate it in a .cpp?

Exactly as my question reads: Is there a way to declare a pointer in a header file and instantiate it in a .cpp?

I have this so far:

.h:

FILE* stream;

.cpp

stream = fopen("com2", "r");

But this give me this error:

1>gpsHandler.obj : error LNK2001: unresolved external symbol "struct _iobuf * stream" (?stream@@3PAU_iobuf@@A) 1>C:\Users***\portReading\Debug\portReading.exe : fatal error LNK1120: 1 unresolved externals

Upvotes: 3

Views: 4534

Answers (2)

Jokester
Jokester

Reputation: 5617

try declare it as extern FILE* straem; in header file?

Upvotes: 1

Marlon
Marlon

Reputation: 20312

As long as the variable in the source file is not static (internal linkage), you can declare the variable in a header file with extern FILE* stream;. This is how we declare global variables:

.h:

extern FILE* stream;

.cpp:

FILE* stream;

As far as your error is concerned, you probably need to #include <cstdio>

Upvotes: 5

Related Questions