|
From: 3DCoderGuy on 7 May 2008 19:49 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? 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. Thanks Mark
From: Alf P. Steinbach on 7 May 2008 20:59 * 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?
From: David Wilkinson on 7 May 2008 21:03 3DCoderGuy wrote: > 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? > > 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. Mark: Template function/member definitions must be visible to the caller, so they must be in the .h file. This has nothing to do with operator ==() in particular. -- David Wilkinson Visual C++ MVP
From: Alexander Grigoriev on 7 May 2008 23:52 And, by the way, you want the operator== to be const. Not returning const bool, which doesn't make sense. bool operator==(const XYZ_POINT<T> &xyzTest) const; "3DCoderGuy" <nobody(a)nospam.com> wrote in message news:uzopyzJsIHA.4476(a)TK2MSFTNGP06.phx.gbl... > 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? > > 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. > > Thanks > > Mark > >
From: 3DCoderGuy on 8 May 2008 11:17 "David Wilkinson" <no-reply(a)effisols.com> wrote in message news:%23bvicdKsIHA.552(a)TK2MSFTNGP06.phx.gbl... > 3DCoderGuy wrote: >> 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? >> >> 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. > > Mark: > > Template function/member definitions must be visible to the caller, so > they must be in the .h file. This has nothing to do with operator ==() in > particular. > > -- > David Wilkinson > Visual C++ MVP Thanks David, This is my first go at templates. I was trying to convert some old classes over since it just made sence. Thanks again.
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: How to get my own version? Next: Offset by 1 in CStdiofile Seek while reading 0xa |