CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2018
    Posts
    3

    Code worked in VS6 but not in VS2017

    The following code worked before with VS6 VC++. But it does not complete the compiling with VS2017 VC++ (Link error is shown below). The complete template is in a .h file. What was changed in VS2017? What needs to be done to resolve the error? Please help. Thanks.

    error LNK2019: unresolved external symbol "class CArrayEx<double,double> __cdecl operator-(class CArrayEx<double,double> const &,class CArrayEx<double,double> const &)" (??G@YA?AV?$CArrayEx@NN@@ABV0@0@Z) referenced in function "public: int __thiscall CMathEx::QNSS(class CArrayEx<double,double> const &,class CArrayEx<double,double> &,class CArrayEx<double,double> &,double &,double &,int &,int)" (?QNSS@CMathEx@@QAEHABV?$CArrayEx@NN@@AAV2@1AAN2AAHH@Z)

    Code:
    //
    template<class TYPE, class ARG_TYPE> 
    class CArrayEx : public CArray<TYPE,ARG_TYPE>
    {
    public:
    	CArrayEx(){}
    	......
    	friend CArrayEx<TYPE,ARG_TYPE>	operator + (const CArrayEx<TYPE,ARG_TYPE>& za1,const CArrayEx<TYPE,ARG_TYPE>& za2);
    	......
    };
    
    template<class TYPE, class ARG_TYPE> 
    CArrayEx<TYPE,ARG_TYPE> operator + (const CArrayEx<TYPE,ARG_TYPE>& za1,const CArrayEx<TYPE,ARG_TYPE>& za2)
    {
    	CArrayEx<TYPE,ARG_TYPE> za;
    	za.SetSize(min(za1.GetSize(),za2.GetSize()));
    
    	for (int i=0;i<za.GetSize();i++)
    	{
    		za[i]=za1[i]+za2[i];
    	}
    	return za;
    }
    Last edited by 2kaud; June 26th, 2018 at 03:10 AM. Reason: Added code tags

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Code worked in VS6 but not in VS2017

    You are using operator-.
    Do you have an implementation for that operator? I only see operator+ in the code snippet you showed.
    Also, where are your implementations? In the header file itself? Somewhere else?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Code worked in VS6 but not in VS2017

    [When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.]

    There was massive changes in c++ between VS6 and VS2017. Also huge changes in the MS compiler used. As a consequence c++ code that compiled ok for VS6 may well not compile for VS2017. Many of us went through this pain a few years ago with VS2013/VS2015.

    As Marc says in post #2, the problem is that operator- is being used in
    Code:
    CMathEx::QNSS(class CArrayEx<double,double> const &,class CArrayEx<double,double> &,class CArrayEx<double,double> &,double &,double &,int &,int)
    but a definition can't be found.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Code worked in VS6 but not in VS2017

    The massive changes between VC++ 6.0 and 2017 that 2kaud mentioned are a good thing
    VC++ 6.0 was not really a good compiler, and was not standard compliant.
    Recent compiler versions are much more, almost fully, standard compliant.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Jun 2018
    Posts
    3

    Re: Code worked in VS6 but not in VS2017

    Quote Originally Posted by Marc G View Post
    You are using operator-.
    Do you have an implementation for that operator? I only see operator+ in the code snippet you showed.
    Also, where are your implementations? In the header file itself? Somewhere else?
    Thanks for your reply.
    I do have operators +/-/* in the definition (only friend CArrayEx<TYPE,ARG_TYPE> operator + is listed above as an example). The implementations are right below the class definition as shown above (operator + was provided above as an example, in the header file itself). But the VS2017 Linker does not seem to see this implementation. What is the workaround? Thanks for your help.

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Code worked in VS6 but not in VS2017

    Try the following syntax for your friend declaration:
    Code:
    friend CArrayEx<TYPE,ARG_TYPE> operator-<TYPE,ARG_TYPE>(const CArrayEx<TYPE,ARG_TYPE>& za1,const CArrayEx<TYPE,ARG_TYPE>& za2);
    Such a friend declaration is tricky: you have to specify that for an instance of the template with types <TYPE, ARG_TYPE>, the <TYPE, ARG_TYPE> instantiation of operator- is a friend.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Jun 2018
    Posts
    3

    Re: Code worked in VS6 but not in VS2017

    Quote Originally Posted by Marc G View Post
    Try the following syntax for your friend declaration:
    Code:
    friend CArrayEx<TYPE,ARG_TYPE> operator-<TYPE,ARG_TYPE>(const CArrayEx<TYPE,ARG_TYPE>& za1,const CArrayEx<TYPE,ARG_TYPE>& za2);
    Such a friend declaration is tricky: you have to specify that for an instance of the template with types <TYPE, ARG_TYPE>, the <TYPE, ARG_TYPE> instantiation of operator- is a friend.

    I tried it. But it does not compile. Do you have an example for implementing this type of operators? Thanks.

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Code worked in VS6 but not in VS2017

    Here is a very simple example that compiles just fine in VC++2017:
    Code:
    template<class TYPE, class ARG_TYPE>
    class CArrayEx
    {
    public:
    	CArrayEx() {}
    	friend CArrayEx<TYPE, ARG_TYPE>	operator+<TYPE, ARG_TYPE>(const CArrayEx<TYPE, ARG_TYPE>& za1, const CArrayEx<TYPE, ARG_TYPE>& za2);
    private:
    	int m_i;
    };
    
    template<class TYPE, class ARG_TYPE>
    CArrayEx<TYPE, ARG_TYPE> operator+(const CArrayEx<TYPE, ARG_TYPE>& za1, const CArrayEx<TYPE, ARG_TYPE>& za2)
    {
    	CArrayEx<TYPE, ARG_TYPE> za;
    	za.m_i = za1.m_i + za2.m_i;
    	cout << za.m_i << endl;
    	return za;
    }
    
    int main()
    {
    	CArrayEx<int, double> e1, e2;
    	e1 + e2;
    	return 1;
    }
    If you still get errors, please post your code.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured