VC++ Linking error to do with += Operator
-
I have created a class with the following defined: friend CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ); and then: template CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ) { ///code and return } Everything compiled but when it tries to create the executable it throws up the followinf error: CString.obj : error LNK2019: unresolved external symbol "class CStrTemp & __cdecl operator+=(class CStrTemp &,class CStrTemp const &)" (??Y@YAAAV?$CStrTemp@D@@AAV0@ABV0@@Z) referenced in function "class CString __cdecl operator+(char,class CString const &)" (??H@YA?AVCString@@DABV0@@Z) Any help would be appreciated as I just cannot understand why it is not working.
-
I have created a class with the following defined: friend CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ); and then: template CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ) { ///code and return } Everything compiled but when it tries to create the executable it throws up the followinf error: CString.obj : error LNK2019: unresolved external symbol "class CStrTemp & __cdecl operator+=(class CStrTemp &,class CStrTemp const &)" (??Y@YAAAV?$CStrTemp@D@@AAV0@ABV0@@Z) referenced in function "class CString __cdecl operator+(char,class CString const &)" (??H@YA?AVCString@@DABV0@@Z) Any help would be appreciated as I just cannot understand why it is not working.
-
I have created a class with the following defined: friend CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ); and then: template CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ) { ///code and return } Everything compiled but when it tries to create the executable it throws up the followinf error: CString.obj : error LNK2019: unresolved external symbol "class CStrTemp & __cdecl operator+=(class CStrTemp &,class CStrTemp const &)" (??Y@YAAAV?$CStrTemp@D@@AAV0@ABV0@@Z) referenced in function "class CString __cdecl operator+(char,class CString const &)" (??H@YA?AVCString@@DABV0@@Z) Any help would be appreciated as I just cannot understand why it is not working.
ajisthekingofpop wrote:
unresolved external symbol...referenced in function "class CString __cdecl operator+(
What does the
operator+
method look like?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
I have created a class with the following defined: friend CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ); and then: template CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s ) { ///code and return } Everything compiled but when it tries to create the executable it throws up the followinf error: CString.obj : error LNK2019: unresolved external symbol "class CStrTemp & __cdecl operator+=(class CStrTemp &,class CStrTemp const &)" (??Y@YAAAV?$CStrTemp@D@@AAV0@ABV0@@Z) referenced in function "class CString __cdecl operator+(char,class CString const &)" (??H@YA?AVCString@@DABV0@@Z) Any help would be appreciated as I just cannot understand why it is not working.
There are a few problems here. First, depending on your compiler, some versions of MFC's CString have a hidden class called CStrTemp. Thus, defining your own class with the same name can cause problems (one of the reasons MFC should have put its stuff in a namespace, and the reason you should do the same). Second, you defined the function as:
friend CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s );
, but implemented it astemplate CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s )
(I'm assuming you just mistyped the <> signs and that the template declaration should actually read template<class T>). If you defined the class as a template, that will work, but you will need to declare it similar to the following:friend CStrTemp<T> operator+=(CStrTemp<T>& i, const CStrTemp<T>& s)
and implement it astemplate<class T> CStrTemp<T>& operator+=(CStrTemp<T>& i, const CStrTemp<T>& s) {...}
Put your code in a namespace and fix your template declarations and this should clear some things up.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
There are a few problems here. First, depending on your compiler, some versions of MFC's CString have a hidden class called CStrTemp. Thus, defining your own class with the same name can cause problems (one of the reasons MFC should have put its stuff in a namespace, and the reason you should do the same). Second, you defined the function as:
friend CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s );
, but implemented it astemplate CStrTemp& operator += ( CStrTemp& i, const CStrTemp& s )
(I'm assuming you just mistyped the <> signs and that the template declaration should actually read template<class T>). If you defined the class as a template, that will work, but you will need to declare it similar to the following:friend CStrTemp<T> operator+=(CStrTemp<T>& i, const CStrTemp<T>& s)
and implement it astemplate<class T> CStrTemp<T>& operator+=(CStrTemp<T>& i, const CStrTemp<T>& s) {...}
Put your code in a namespace and fix your template declarations and this should clear some things up.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac