Question regarding Classes, Header Files, and Pointers

Good Day Everyone,

I am currently studying C++ in school and have come across a situation which I am unsure of how to resolve.

Our teacher provided us a base framework of a Windows Program which includes the following:

Header files:
GF.h (this is a class for drawing things on the screen)
Resource.h (not really sure what this is/does but has something to do with line.h)

Resource Files:
Line.h

Source Files.
Line.cpp (creates a pointer to the GF class)

The base frame work works fine, it compiles and runs. We were tasked with modifying the code to perform a new task. Because of the nature of the assignment using a class made more sense to me.

Because of the way I work, I wanted to get the program working. So I added all of my code in the Line.CPP. I created my class and defined all my methods, and everything worked perfectly. This way I can at least turn in a working program.

Now my question is I am trying to pull out the class I made DrawShapes and put it in its own header and cpp file. Upon compiling I get errors about unknown identifier and pointer issues. I understand what the error means, it can't find the pointer that is referred to in Line.cpp, so how do I go about resolving this?

Somebody at school mentioned making the pointer "extern" and that should work. I am unfamiliar with this, and even doing research I became a bit lost.

Any and all help will be greatly appreciated, just let me know if more information is needed.

Thank you,

Mark
Without seeing at least some of the code it will be hard to help. We would need to see at a minimum the code where the error is occurring, the full error message(s), and any related code.
Well I got past the compiling errors. Now I'm just getting build errors. (Assuming that I properly fixed the compile errors).

Here is the build log.
1
2
3
4
5
6
7
8
9
1
1>Linking...
1>sqr_and_circle.obj : error LNK2005: "int __cdecl random(void)" (?random@@YAHXZ) already defined in shapes.obj
1>sqr_and_circle.obj : error LNK2005: "int __cdecl Abs(int)" (?Abs@@YAHH@Z) already defined in shapes.obj
1>sqr_and_circle.obj : error LNK2005: "int __cdecl Sign(int)" (?Sign@@YAHH@Z) already defined in shapes.obj
1>sqr_and_circle.obj : error LNK2005: "float __cdecl CalculateSlope(int,int,int,int)" (?CalculateSlope@@YAMHHHH@Z) already defined in shapes.obj
1>sqr_and_circle.obj : error LNK2005: "void __cdecl DrawLine(int,int,int,int,unsigned long)" (?DrawLine@@YAXHHHHK@Z) already defined in shapes.obj
1>Debug/line.exe : fatal error LNK1169: one or more multiply defined symbols found
1>line - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I am more then willing to post code snippets, but I'm just honestly not sure what to post.

Thanks again,

Mark
Last edited on
Are the functions random(), Abs(), Sign(), etc declared and implemented in header files, or do you have header files
that are #including .cpp files?
I am using a Header file caled GeneralFunctions.h. All the functions in the build message are in the GeneralFunctions.h file. I did double check to make sure I included the GeneralFunctions.h file in the necessary places and it seems that I have.

On a positive note I think I may have narrowed down what my issue is.

In the base framework the instructor provided we have the following:

1
2
#include "GraphicsFramework.h"
GraphicsFramework* PGraphics;


The GraphicsFramework is a class that is used to draw pixels and what not on the screen.

I know that PGraphics is a pointer to the GraphicsFramework class.

I'm thinking my problem is I'm not properly identifying PGraphcis in my GeneralFunctions.h file or my Shapes Class methods.

So assuming that I'm referencing PGraphics wrong, and removing the references that I added to it in GeneralFunctions.h I get the following complie errors:

1
2
3
Error	2	error C2065: 'PGraphics' : undeclared identifier	generalfunctions.h	49
Error	3	error C2227: left of '->AddPoint' must point to class/struct/union/generic type	generalfunctions.h	49
Error	5	error C2227: left of '->AddPoint' must point to class/struct/union/generic type	generalfunctions.h	58


I haven't gotten to familiar with pointers yet so still trying to understand how to properly use/reference them.

I guess if I can figure out how to link the files I created to the original pointer provided by the framework everything will work, but that is the mystery to me.

Regards,

Mark
Ok, I fixed my issue and everything is working properly now.

I was actually including my "GeneralFunctions." in one to many places. I removed what I figured were undeded references and everything compiles and builds fine.

The solution doesn't make any sense to me as when I define my header files I do a

1
2
3
4
5
6
#ifndef <blah>
#define <blah>

<code stuff>

#endif 


But it gives me something else to research. Thanks everyone for listening to my issue.

Have a great day,

Mark
Non-template functions that are declared and implemented in header files [i]must/i] be declared as inline.
Topic archived. No new replies allowed.