Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ struct question

Started by Xero-X2 Jun 18, 2002 at 8:11 PM 11 replies 400+ views
Original Post
Xero-X2
Xero-X2
could any one tell me why this is im useing Borland C++ Builder 5 if anyone wants to know first.cpp
      
struct Point3d
{
GLfloat x, y, z;
};

#include "Second.cpp"

#include "Third.cpp"
...
  
Second.cpp
    
...
//THIS WORKS

Point3d CP;

//THIS WORKS

void DrawModel(Model *Model, GLfloat X, GLfloat Y, GLfloat Z,Point3d CPb)
{
...
  
Third.cpp
    
...
//THIS GIVES ME [C++ Error] Third.cpp(13): E2451 Undefined symbol 'Point3d'

float Dotproduct(Point3d V,Point3d E)
...
      
WHY!! this is weird and the problem has really been making me mad I just don't under stand why this could be? any ideas?? [edited by - Xero-X2 on June 18, 2002 9:13:59 PM]
"I seek knowledge and to help those who also seek it"
Fruny
Fruny
You're including .cpp files ? Shame on you.
Go read this.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 18, 2002 9:17:57 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
maxsteel
maxsteel
Works just fine for me on VC 6.0
Although...passing the objects around on the stack is usually a bad idea. Try using references instead.
Xero-X2
Xero-X2
just wondering but if you include a .h file I don't include a cpp file with it so I get the stuff in the .h file but not the stuff in the cpp file that goes with it why the heck is that?

the cpp includes the H but the h dosn't include the cpp so then in the end you includeing the h dosn't let use use anything in the cpp???? it works so you can include an H and use the stuff in the cpp in anybodys code but when i tryed it it didn't work.

[edited by - Xero-X2 on June 18, 2002 9:34:27 PM]
"I seek knowledge and to help those who also seek it"
munkie
munkie
quote:
Original post by Xero-X2
just wondering but if you include a .h file I don''t include a cpp file with it so I get the stuff in the .h file but not the stuff in the cpp file that goes with it why the heck is that?

the cpp includes the H but the h dosn''t include the cpp so then in the end you includeing the h dosn''t let use use anything in the cpp???? it works so you can include an H and use the stuff in the cpp in anybodys code but when i tryed it it didn''t work.

[edited by - Xero-X2 on June 18, 2002 9:34:27 PM]


Ahh! Use..periods..!

Function and variables should be declared or prototyped in the header files, and the actual coding should be fleshed out in corresponding .cpp files.
Xero-X2
Xero-X2
ok I have

file.cpp
file.h

I use

#include "file.h"

how does .h know that .cpp has the functions?
"I seek knowledge and to help those who also seek it"
IndirectX
IndirectX
It doesn''t need to. As long as functions are declared you can reference them, and linker will put everything together once you compile all your .cpp files.
---visit #directxdev on afternet <- not just for directx, despite the name
Xero-X2
Xero-X2
So how does the linker know that the file exists?

[edited by - Xero-X2 on June 18, 2002 9:59:36 PM]
"I seek knowledge and to help those who also seek it"
IndirectX
IndirectX
You compile only .cpp files to produce .obj files. You don''t compile .h files; they are included in .cpp files, but it''s .cpp files that get compiled, not .h files.

So when you compile your project, you end up with a bunch of .obj files that contain all variable and function definitions you had in either .h or .cpp files.

Then linker takes all these .obj files, plus some .lib files which basically are collections of .obj files as well, and tries to link them together. If a function is declared in one .obj file and is referenced from another .obj file, the linker tells the reference where to find the definition.

If someone references something for which the linker can''t find the definition, for one reason or another, the linker will complain and won''t produce the .exe. Similarly, if you have two definitions of the same thing, the linker won''t know which one it should use and it will complain as well.

Is this becoming any clearer?
---visit #directxdev on afternet <- not just for directx, despite the name
Xero-X2
Xero-X2
ok. I under stand that but then again It still leaves one last question how do I get the compiler to know that the cpp file exists so that I can use the .h file to access it?
"I seek knowledge and to help those who also seek it"
Stoffel
Stoffel
That happens at link phase. You can include the .h file in any file you want. At the very end, you have to have the .obj file in your link file in order for it to work. How to do this depends on your development platform, but if you''re using a modern IDE then it means you have to "add the .cpp file to your project".
Xero-X2
Xero-X2
ok I get it now thanks a ton on the .h and .cpp files
"I seek knowledge and to help those who also seek it"
Oluseyi
Oluseyi
quote:
Original post by Xero-X2
ok. I under stand that but then again It still leaves one last question how do I get the compiler to know that the cpp file exists so that I can use the .h file to access it?

a.) You have project workspaces. Their purpose is to tell the compiler/linker which implementation (.cpp) and object (.obj) files are part of the project, as well as other resources.

b.) RTFM!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.