Skip to main content
gamedev.net
🔒 Locked

apvector.cpp and apvector.h

Started by Krisc 15 replies 1.1k views
Original Post
Krisc
Krisc
Hello, I am in my AP Computer Science class and we cannot figure a certain problem out. When we try to overload the operators for apvector<> we get multiple errors. apvector.h apvector.cpp We are trying with these declarations and definitions... apvector.h - Free member functions
apvector operator + ( const apvector &lhs, const apvector &rhs );
apvector operator - ( const apvector &lhs, const apvector &rhs );
  
apvector.cpp
template 
apvector operator + ( const apvector &lhs, const apvector &rhs )
{
}

template 
apvector operator - ( const apvector &lhs, const apvector &rhs )
{
}
  
Please help, everyone is clueless on this. (I know the functions don't work, but we cant get it to even compile correctly. <- Digital Explosions -> "Discipline is my sword, faith is my shield do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship) [edited by - Krisc on January 6, 2003 10:45:32 AM]
bligblug
bligblug
Umm, not being an expert on templates I can''t really help with the syntax of your code but I glanced over the source files you provided in your post and noticed that at the bottom of apvector.h is the line:

#include "apvector.cpp"

Since apvector.cpp contains the line:

#include "apvector.h"

This means each file includes the other, this will crash the preprocessor since it starts up an infinite inclusion loop.
---------------------------A man is in bed with his wife smoking a cigarette. She says, "Dear, you hear all those warnings about cigarettes being bad for you health, why do you still smoke?"The man replies, "Well, I''m a programmer, we don''t pay attention to warnings, only to errors."
null_pointer
null_pointer
Template functions should be defined in header files.
Krisc
Krisc
Ok, so you''re saying that we need to declare the operator, not as a free member function but rather as a normal function?

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
null_pointer
null_pointer
No, I am saying that you must place the function definition - as opposed to merely its declaration - in the header file, i.e.:


  // in null_safe_delete.h:

 
template <typename T>
void safe_delete (T*& p)
{ delete p; p = 0; }
 
// in main.cpp:

 
# include "null_safe_delete.h"
 
int main ()
{
int* p = new int;
 
safe_delete (p);
}
null_pointer
null_pointer
This is a function declaration :


  void some_function ();  


This is a function definition :


  void some_function ()
{
cout << "hello, world!" << endl;
}
Krisc
Krisc
Ohhh, ok thanks, because I am not in the class anymore, I cannot try until tomorrow or at least tonight. But thank you!

EDIT: Thanks null pointer for stating the obvious, I just read his question selectively and read it declaration because I was hoping it would be declaration...

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)

[edited by - Krisc on January 6, 2003 2:36:08 PM]
null_pointer
null_pointer
BTW the same rule also applies to inline functions.
Krisc
Krisc
A couple of friends and I came up with something...

in the .h file, do we declare it then define it, or just define it?

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
null_pointer
null_pointer
quote:
in the .h file, do we declare it then define it, or just define it?


Either will work fine because a definition is a declaration plus something else. Which one you choose depends on the situation. It is less work to just define the template functions if there are relatively few of them, but a large header file might be easier to read if you provide all the declarations first and then the definitions, but then you have to worry about making sure that the declarations match the definitions, or you will get linker errors.
Krisc
Krisc
Wow, you have been a great help so far!

With the declaration and definition, should it be like this do you think...?

class apvector
{
declaration
}

definition
{
}


or like this

class apvector
{
definition
{
}
}

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
null_pointer
null_pointer
quote:
With the declaration and definition, should it be like this do you think...?


I am not sure what you mean when in your examples you write "definition {}" Are you speaking of the class definition or the function definition? Here is a header file that is nice and neat because it lists all the things it defines at the top:


  
// some class declarations.

 
class window;
class window_button;
class window_scrollbar;
class window_toolbar;
 
// some inline function declarations.

 
inline void foo_1 ();
inline void foo_2 ();
inline void foo_3 ();
 
// now the class definitions for the classes that we declared earlier.

 
class window
{
// ...

};
 
class window_button
{
// ...

};
 
class window_scrollbar
{
// ...

};
 
class window_toolbar
{
// ...

};
 
// and now the definitions for the inline functions that we declared earlier.

 
inline void foo_1 ()
{
exit (0);
}
 
inline void foo_2 ()
{
throw exception ("help!");
}
 
inline void foo_3 ()
{
terminate ();
}


Note that those forward declarations are completely optional. I just put them there to save people the trouble of wading through all the code to find what stuff that header file defines.
Krisc
Krisc
Ok well I was thinking it would be like this...


#ifndef BLAH_H
#define BLAH_H

//blah blah

class blah
{
template apvector operator + (apvector &lhs, apvector &rhs);
};

template apvector operator + (apvector &lhs, apvector &rhs)
{
//code here
}


oooorrrrr....



#ifndef BLAH_H
#define BLAH_H

//blah blah

class blah
{
template apvector operator + apvector &lhs, apvector &rhs)
{
//code here
}

};


<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
SabreMan
SabreMan
quote:
Original post by Krisc
Ok well I was thinking it would be like this...
[...]
oooorrrrr....
[...]

It won''t be like either. operator+ and operator- are binary operators, you are trying to write them as ternary operators.
Krisc
Krisc
Ya, well we ended up doing nothing cited here, because quite frankly we didn''t think it would actually work. Instead we just went ahead and wrote the operator for + and - in the cpp without declaring it at all in the .h file, which I guess (now that I think about it...) is like what you suggested, so thank you a lot.

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
xuwen
xuwen
Glad to hear that the AP libraries compiled. But I''m not quite sure why they did not work in the first place. My versions of the library had the + and - overloaded correctly. If you want, I can provide the definitions for the two from my library.

Also, other reasons you may not want to include the definition with the template function in the header file is because definitions of functions that are defined later may use those template functions. Without the prototype, you will encounter an error that says the function does not exist.

Also, the #include mess that was setup is necessary for it to compile. The reason it won''t crash the precompiler is because those libraries SHOULD have the #ifndef __APVECTOR_H, #define __APVECTOR_H commands at the top. If it doesn''t, then I''d say you have a corrupted version of the apvector library.
Krisc
Krisc
Well, the versions we have were taken off the apboard site and our book that also uses the apboard site told us to modify the .h and .cpp to allow for the operations, + and -...but thanks everyone!

btw, I cut my index finger on a razor so sorry for any typos!

EDIT: bandaids rock, um, We took out the statement #include "apvector.cpp" at the end of apvector.h and just include apvector.cpp in our programs...apvector.cpp includes apvector.h so its all good!

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)

[edited by - Krisc on January 7, 2003 9:24:33 PM]

Topic Locked

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

Sign in to reply to this topic.