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;
}
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?
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.
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.
Re: Code worked in VS6 but not in VS2017
Quote:
Originally Posted by
Marc G
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.
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.
Re: Code worked in VS6 but not in VS2017
Quote:
Originally Posted by
Marc G
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.
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.