J
J
Julia2016-09-15 21:19:57
C++ / C#
Julia, 2016-09-15 21:19:57

How to create operator= for Template class?

Hello.
I have a SearchVector class Let's
say I want to do the following. Find the largest element from value and return it for initialization
I need an equalization operator.

SearchVector <double> sv1({ 1.0, 1.9, 12.0, 2.5, 3.4 });
     SearchVector <int>    sv2({ 1, 2, 5, 23, 2, 1 });    
     double max_double = sv1;
     int    max_int    = sv2;

I wrote something like this template for a test, with a max test of some number, but I get an error when compiling.
How to do it right?
T  & operator=(SearchVektor<T> & s )   {
    //// Find max from s 

    return (T) max; 
  }

A1.cpp:189:26: error: cannot convert 'SearchVektor' to 'double' in initialization

Example from cppreference
T& T::operator=(T arg) // copy/move constructor is called to construct arg
{
swap(arg); // resources are exchanged between *this and arg
return *this;
} // destructor of arg is called to release the resources formerly held by *this

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question