.h and .cpp files
I have a kind of simple question that has been bothering me.
I have two classes, one is called Matrix3x3, and one is called AffineTransform. Here''s how VC put them into files for me:
//AffineTransform.cpp
#include "AffineTransform.h"
//class implementation goes here
//AffineTransform.h
//class interface goes here.
//Matrix3x3.cpp
#include "Matrix3x3.h"
//class implementation goes here
//Matrix3x3.h
//class interface goes here.
My question is this. The AffineTransform class has a member of type Matrix3x3. (eg. Matrix3x3 foo). However, I get linker errors whenever I try to do anything with foo. This is essentially because the AffineTransform class is being compiled BEFORE matrix3x3 - the linker can''t find the matrix3x3 code. #include "matrix3x3.h" will load the interface for Matrix3x3, but not the implementation. #include "matrix3x3.h" #include "matrix3x3.cpp" just seems weird.
In C, I might have used extern to solve these difficulties. But if I try to do a extern Matrix3x3 foo; inside my AffineTransform class, I get another error message.
Only one of my C++ books appears to cover the issue of where to put things in files, and how to #include them. Unfortunately, I don''t think I get it. (Stroustrup, 3rd edition).
Suggestions? Samples?
Thanks
.3
It''s not because anything is getting compiled before anything else, because everything gets compiled before the linker does anything anyway. What linker error are you getting? All you have to know is that you need to include the Matrix3x3.h file anywhere you use a Matrix3x3 object, meaning you must include it whereever you use an AffineTransform object as well (because all AffineTransform objects use Matrix3x3 objects. A good move would probably be to include Matrix3x3.h at the top of AffineTransform.h, and see if that helps.
I hope I understood the problem
-----------------------------
"And I write and I write and I don't believe it's gonna change today"
- From Yellow Ledbetter by Pearl Jam
http://www.crosswinds.net/~uselessknowledge
I hope I understood the problem
-----------------------------
"And I write and I write and I don't believe it's gonna change today"
- From Yellow Ledbetter by Pearl Jam
http://www.crosswinds.net/~uselessknowledge
January 10, 2000 01:32 PM
I am getting the following errors:
AffineTransform.obj : error LNK2001: unresolved external symbol "public: void __thiscall Matrix3x3::set(double,int,int)" (?set@Matrix3x3@@QAEXNHH@Z)
AffineTransform.obj : error LNK2001: unresolved external symbol "private: static class Matrix3x3 AffineTransform::rotationMatrix" (?rotationMatrix@AffineTransform@@0VMatrix3x3@@A)
The function Matrix3x3::set(double,int,int) exists in my Matrix3x3.cpp/.h file so I''m not sure why I would be getting the first error.
I have a data member in AffineTransform called Matrix3x3 rotationMatrix; which for some reason is giving me this second error.
In AFfineTransform.cpp I first #include "Matrix3x3.h", then #include "AffineTransform.h"
AffineTransform.obj : error LNK2001: unresolved external symbol "public: void __thiscall Matrix3x3::set(double,int,int)" (?set@Matrix3x3@@QAEXNHH@Z)
AffineTransform.obj : error LNK2001: unresolved external symbol "private: static class Matrix3x3 AffineTransform::rotationMatrix" (?rotationMatrix@AffineTransform@@0VMatrix3x3@@A)
The function Matrix3x3::set(double,int,int) exists in my Matrix3x3.cpp/.h file so I''m not sure why I would be getting the first error.
I have a data member in AffineTransform called Matrix3x3 rotationMatrix; which for some reason is giving me this second error.
In AFfineTransform.cpp I first #include "Matrix3x3.h", then #include "AffineTransform.h"
quote: Original post by Qoy
It''s not because anything is getting compiled before anything else, because everything gets compiled before the linker does anything anyway. What linker error are you getting? All you have to know is that you need to include the Matrix3x3.h file anywhere you use a Matrix3x3 object, meaning you must include it whereever you use an AffineTransform object as well (because all AffineTransform objects use Matrix3x3 objects. A good move would probably be to include Matrix3x3.h at the top of AffineTransform.h, and see if that helps.
I hope I understood the problem
-----------------------------
"And I write and I write and I don''t believe it''s gonna change today"
- From Yellow Ledbetter by Pearl Jam
http://www.crosswinds.net/~uselessknowledge
period3:
This might be a silly question, but did you actually add the files to your project? The "undefined external: xxxx" error is because it can''t locate either a .cpp file or something that should be in a .cpp file.
To make sure those files are added to the project:
In Visual C++ 6.0, click on the tab called FileView and this lists the files the compiler uses. Double-click on the Source Files folder listed there and make sure those files with the .cpp extension are listed there. That''s usually the problem. You never need to use #include with a cpp file.
To add those files to the project:
In Microsoft Visual C++ 6.0, click on the FileView tab and right-click on the folder called Source Files. Select Add files to folder... and select both of your .cpp files.
- null_pointer
This might be a silly question, but did you actually add the files to your project? The "undefined external: xxxx" error is because it can''t locate either a .cpp file or something that should be in a .cpp file.
To make sure those files are added to the project:
In Visual C++ 6.0, click on the tab called FileView and this lists the files the compiler uses. Double-click on the Source Files folder listed there and make sure those files with the .cpp extension are listed there. That''s usually the problem. You never need to use #include with a cpp file.
To add those files to the project:
In Microsoft Visual C++ 6.0, click on the FileView tab and right-click on the folder called Source Files. Select Add files to folder... and select both of your .cpp files.
- null_pointer
Yep, both those files are part of my project.
However, I notice one oddity. I don''t have any classes listed in the class view, it says "23 files to load". Very strange.
I think what I may try doing is beginning a new project, then adding all the source files into it. Perhaps the project iI''m using now has become corrupted.
However, I notice one oddity. I don''t have any classes listed in the class view, it says "23 files to load". Very strange.
I think what I may try doing is beginning a new project, then adding all the source files into it. Perhaps the project iI''m using now has become corrupted.
.3
Try #include "Matrix3x3.h" at the top of your AffineTransform.h.
The linker errors may be coming from another cpp file that includes AffineTranform.h but not Matrix3x3.h
The linker errors may be coming from another cpp file that includes AffineTranform.h but not Matrix3x3.h
A bit of C++ trivia:
In an effort to reduce the size of the EXE, the C++ compiler/linker only deals with classes (and members) that are actually used somewhere in the program. So... even if you add a new class cpp/h to your project, it won''t be put in the EXE unless it''s actually used (instantiated) somewhere.
This is why you only see the error if you have a matrix member variable.
Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
In an effort to reduce the size of the EXE, the C++ compiler/linker only deals with classes (and members) that are actually used somewhere in the program. So... even if you add a new class cpp/h to your project, it won''t be put in the EXE unless it''s actually used (instantiated) somewhere.
This is why you only see the error if you have a matrix member variable.
Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
OK, that''s good to know...
I fixed my problem. I had defined Matrix3x3::set as inline within my Matrix3x3 class interface, but I had implemented Matrix3x3::set in a different file (Matrix3x3.cpp).
Also a problem with a static member variable which (apparently) should not have been static. This was a bit dissapointing, but I''m wondering about having a seperate AffineTransform class anyway.
Essentially, I''m writing some kind of basic 2D graphics engine on top of SDL. (Simple Direct Medialayer) I''d like clean, reusable, OO code.
What I want to be able to do, is make a call that looks like:
AffineTransform.rotate(object);
in order to this, the AffineTransform class needs access to the objects transformation matrix. I''m not exactly sure how I''m going to do that. I suppose I could use friends, or some kind of global variable but it seems rather messy.
The alternative is putting the rotate function within the object, and then calling like:
object.rotate();
I don''t particularly like doing it that way, because I''d like to be able to create objects composed of many smaller objects, and then just be able to rotate everything at once by passing an array of objects to a rotate() function contained within a seperate class (eg. AffineTransform). I don''t really want to make an objects transformation matrix public, as this does not seem to be very good OO design. However, short of using friends or an illogical inheritance hierarchy, I don''t really know how to let AffineTransform access an Objects transformation matrix. (Passing a reference to the transformation matrix is out of the question, because the class Object would have to do that, and then I''m back to calling Object.rotate() again)
Maybe I should just use friends. I''m afraid if I do, I''ll start using them for other things as well - I''m sure there must be a cleaner way of doing this. (I''m moving from Java into C++)
.3
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement