Strange "Member function not viable" error in templated linear algebra vector class

Simon Ayzman picture Simon Ayzman · Feb 1, 2014 · Viewed 20.5k times · Source

I'm implementing a templated vector class (not the data container, but the vector in the linear algebra sense), and I'm getting quite a few errors whenever I refer to rhs in my operator overloading. Also, my copy constructor doesn't seem to be working.

#ifndef __VecXd__VecXd__
#define __VecXd__VecXd__

#define ULL unsigned long long
#include <iostream>

using namespace std;

template <class T>
class VecXd
{

public:

    explicit VecXd(ULL newDimension = 1) { dimension = newDimension; vector = new T[newDimension];}
    VecXd(const VecXd<T> &rhs);
    VecXd<T>& operator=(const VecXd<T> &rhs);
    const VecXd<T> operator+(const VecXd<T> &rhs) const;
    VecXd<T>& operator+=(const VecXd<T> &rhs);
    friend ostream& operator<<(ostream &out, VecXd<T> vec);
    friend istream& operator>>(istream &in, VecXd<T>& vec);
    ~VecXd() { delete[] vector; }

    const ULL getDimension() { return dimension; }
    const T itemAtIndex(ULL index) { if(index >= dimension) throw 1; return vector[index]; }

private:

    ULL dimension;
    T *vector;

};

template <class T>
VecXd<T>::VecXd(const VecXd<T> &rhs)
{
    dimension = rhs.getDimension();
    vector = new T[dimension];
    for (ULL i = 0; i < dimension; ++i)
        vector[i] = rhs.itemAtIndex(i);
}

template <class T>
VecXd<T>& VecXd<T>::operator=(const VecXd<T> &rhs)
{
    if (this != &rhs)
    {
        if (dimension != rhs.getDimension())
        {
            delete [] vector;
            dimension = rhs.getDimension();
            vector = new T[dimension];
        }
        for (ULL i = 0; i < dimension; ++i)
            vector[i] = rhs.itemAtIndex(i);
    }
    return *this;
}

template <class T>
VecXd<T>& VecXd<T>::operator+=(const VecXd<T> &rhs)
{
    if (dimension != rhs.getDimension())
    {
        cout << "\nCannot perform addition. Vectors do not have the same dimensions.\n";
        throw 1;
    }
    else
    {
        for (ULL i = 0; i < dimension; ++i)
            vector[i] += rhs[i];
    }
    return *this;
}

template <class T>
const VecXd<T> VecXd<T>::operator+(const VecXd<T> &rhs) const
{
    VecXd<T> temp = *this;
    temp += rhs;
    return temp;
}

template <class T>
ostream& operator<<(ostream &outs, VecXd<T> vec)
{
    for (ULL i = 0; i < vec.dimension; ++i)
        out << vec.vector[i] << (i+1 < vec.dimension ? " " : "");
    return out;
}

template <class T>
istream& operator>>(istream &in, VecXd<T> &vec)
{
    ULL newDim = 1;
    cin >> newDim;
    if (!cin.good())
    {
        cout << "\nImproper input.\n";
        throw 1;
    }
    else
    {
        delete [] vec.vector;
        vec.dimension = newDim;
        vec.vector = new T[vec.dimension];
        for (ULL i = 0; i < vec.dimension; ++i)
            in >> vec.vector[i];
    }
    return in;
}

#endif /* defined(__VecXd__VecXd__) */

I'm getting errors of this style:

Member function 'getDimension' not viable: 'this' argument has type 'const VecXd<int>', but function is not marked const

This happens every time rhs calls a function (such as getDimension() or itemAtIndex()); two error appear (once for VecXd<int> and another for VecXd<int>).

Also, the copy constructor is not recognized in the overloaded +operator function in this line:

VecXd<T> temp = *this;

Help?

Answer

Joseph Mansfield picture Joseph Mansfield · Feb 1, 2014

To be able to call a function on a const object, you need to promise the compiler that the function will not modify the object. To do that, you mark the function with the keyword const after its argument list. For example, to make getDimension a const member function, you would change it to:

const ULL getDimension() const { return dimension; }

(Note that the const in the return type will have absolutely no effect, so you should get rid of it)