"Alf P. Steinbach" <alfps@start.no> wrote in message
* 3DCoderGuy:
This is my XYZ_POINT.h file
template<typename T>
class XYZ_POINT
{
private:
T x;
T y;
T z;
public:
XYZ_POINT(void) {};
XYZ_POINT(const T allVals) : x(allVals), y(allVals), z(allVals) {};
bool const operator==(const XYZ_POINT<T> &xyzTest);
}
In my cpp file I have this.
template<typename T>
bool const XYZ_POINT<T>::operator ==(const XYZ_POINT<T> &xyzTest)
{
return ((x == xyzTest.GetX()) &&(y == xyzTest.GetY()) &&(z ==
xyzTest.GetZ()));
}
In the main file I have this
int _tmain(int arc, _TCHAR* argv[])
{
XYZ_POINT<double> dXYZPnt1(1,0,0);
XYZ_POINT<double> dXYZPnt2(3,0,0);
if (dXYZPnt1 == dXYZPnt2)
{
bool b = true;
b = false;
}
}
Doesn't compile with the following error
error LNK2019: unresolved external symbol "public: bool const __thiscall
XYZ_POINT<double>::operator==(class XYZ_POINT<double> const &)"
(??8?$XYZ_POINT@N@@QAE?B_NABV0@@Z) referenced in function _main
If I move the implimentation to the header file it compiles fine.
What am I doing wrong?
Quite a lot. But to answer directly what you're probably most interested
in, see
<http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12>.
It's very often a good idea to check the C++ FAQ before asking.
Now as to other wrongdoings.
The class name XYZ_POINT, in all uppercase, is an invitation to name
clashes with macros, and besides it hurts the eyes and ears (all uppercase
is perceived as shouting and is visually distracting). Preferentially
reserve all uppercase names for macros, and make sure that your own macros
are always defined with all uppercase names. That way you minimize the
chance of macro name clashes.
Using 'void' to indicate "no arguments": this is a C-ism, not meaningful
in C++.
Declaring and defining a default constructor that does not initialize the
members: they will have arbitrary (formally invalid) values.
Semicolon after closing '}' of function definition.
Defining a copy constructor when the one you get by default is just fine.
Not defining operator<, which is the one you really need for algorithms,
containers etc.
Missing semicolon after closing '}' of class definition.
"int _tmain(int arc, _TCHAR* argv[])". This is something some Microsoft
employee scooped up from drainage system and it smelled so bad that all
her co-workers just loved it, and even though they failed to find any
reasonable use for it they just added it everywhere, luv it luv it.
Standard for what you need here is "int main()".
Style: "if( something ) { assign to boolean }". Instead do "boolean =
something". I'm assuming the intent was not to have the boolean as a
local variable.
The actual implimentation is more complex then I'm demonstrating and I
need to include some header files that I don't want in the declaration.
When you fix your "main" the above code does not need any headers.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
not the right one. I now have a bookmark to the C++ Faq Lite page.
I always make an effort to do my research first and then ask the groups. I
just wasn't finding the answer, which led me to ask the group. I respect
my question.
That is always greatly appreciated.
One comment on the C-ism of f(void), your right it is a C-ism. At least I
broke my self of f(a,b) int b, int a;{}.
Now I need to decide if I'm on the void *p = NULL; or void *p = 0; camps.
P.S. I even do research on those who reply to me. You are a great