开发者

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

开发者 https://www.devze.com 2023-02-15 16:04 出处:网络
Exactly as my question reads: 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 LNK开发者_StackOverflow2001: unresolved external symbol "struct _iobuf * stream" (?stream@@3PAU_iobuf@@A) 1>C:\Users***\portReading\Debug\portReading.exe : fatal error LNK1120: 1 unresolved externals


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消