Home > OS >  Unable to overload operator using templates
Unable to overload operator using templates

Time:01-28

I am trying to overload the operator for a vector class. The vector class is able to specify the data type using templates. The operator should be able to take two different vectors and create a new resulting one with the dominating data type. Currently my code looks like this:

template<typename T>
class Vector{
private:
    int length;
    T* data;

public:

    /*
    Set of constructors and other operators
    */

    template<typename S>
    Vector<typename std::common_type<S, T>::type> operator (const Vector<S>& other) const
    {
        if(length != other.length) throw "Vectors do not have equal length!";

        Vector<typename std::common_type<S, T>::type> result(length);

        for(auto i = 0; i < length; i  )
        {
            result.data[i] = data[i]   other.data[i];
        }

        return result;
    }
};

When this code is run for two vectors with different data types (example int and double) it will start complaining that other.length and result.data and other.data are all declared private within this scope. The error message looks like this:

./student_code.cpp: In instantiation of 'Vector<typename std::common_type<S, T>::type> Vector<T>::operator (const Vector<S>&) const [with S = double; T = int; typename std::common_type<S, T>::type = double]':
./student_code.cpp:140:20:   required from here
./student_code.cpp:91:28: error: 'int Vector<double>::length' is private within this context
   91 |         if(length != other.length) throw "Vectors do not have equal length!";
      |                      ~~~~~~^~~~~~
./student_code.cpp:13:9: note: declared private here
   13 |     int length;
      |         ^~~~~~
./student_code.cpp:97:20: error: 'double* Vector<double>::data' is private within this context
   97 |             result.data[i] = data[i]   other.data[i];
      |             ~~~~~~~^~~~
./student_code.cpp:14:8: note: declared private here
   14 |     T* data;
      |        ^~~~
./student_code.cpp:97:46: error: 'double* Vector<double>::data' is private within this context
   97 |             result.data[i] = data[i]   other.data[i];
      |                                        ~~~~~~^~~~
./student_code.cpp:14:8: note: declared private here
   14 |     T* data;
      |        ^~~~

That is not a problem when I overload the operator assuming the other Vector has the same data type (so the code has no other template with typename S). I do not know why this happens and any help is very welcome.

CodePudding user response:

The problem is that when you instantiate say a Vector<int> and Vector<float>, then both of these are distinct types. Moreover, currently neither of them is allowed to access the internals of the other.

To solve the mentioned error, you should add a friend declaration as shown below:

template<typename T>
class Vector{
private:
    //friend declaration
    template<typename S> friend class Vector;
    
    int length;
    T* data;

public:
    //other members here
};
  •  Tags:  
  • Related