C++ 模板化线性代数向量类中出现奇怪的“成员函数不可行”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21492291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:39:43  来源:igfitidea点击:

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

c++vectoroperator-overloadingcopy-constructordeep-copy

提问by Simon Ayzman

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 rhsin my operator overloading. Also, my copy constructor doesn't seem to be working.

我正在实现一个模板化的向量类(不是数据容器,而是线性代数意义上的向量),每当我rhs在我的运算符重载中引用时,我都会遇到很多错误。另外,我的复制构造函数似乎不起作用。

#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

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

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

每次rhs调用函数(例如getDimension()or itemAtIndex())时都会发生这种情况;出现两个错误(一个是 for VecXd<int>,另一个是 for VecXd<int>)。

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

此外,在此行的重载 +operator 函数中无法识别复制构造函数:

VecXd<T> temp = *this;

Help?

帮助?

回答by Joseph Mansfield

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

为了能够在const对象上调用函数,您需要向编译器保证该函数不会修改该对象。为此,您可以const在参数列表后使用关键字标记该函数。例如,为了使getDimension一个const成员函数,你将其更改为:

const ULL getDimension() const { return dimension; }

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

(注意,const返回类型中的 绝对没有效果,所以你应该去掉它)