c++ - 模板化线性代数 vector 类中出现奇怪的 "Member function not viable"错误

标签 c++ vector operator-overloading copy-constructor deep-copy

我正在实现一个模板化 vector 类(不是数据容器,而是线性代数意义上的 vector ),每当我引用 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__) */

我遇到了这种风格的错误:

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

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

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

VecXd<T> temp = *this;

帮助?

最佳答案

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

const ULL getDimension() const { return dimension; }

(注意返回类型中的const绝对不会有任何作用,所以你应该去掉它)

关于c++ - 模板化线性代数 vector 类中出现奇怪的 "Member function not viable"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21492291/

相关文章:

c++ - 如何在没有 free() 或 delete 的情况下返回 char*?

c++ - 烦人的错误消息 : cannot merge previous GCDA file

c++ - 实现矩阵类 C++ 的段错误

matrix - glsl 向量*矩阵与 hlsl 不同

c++ - 快速傅立叶变换 : using templated derived classes 'undefined reference' error even though header and . cpp 文件匹配

rust - 如何将 Arc<T> 与 T 进行比较?

c++ - 比较具有替代顺序的自定义类型的 std::tuple(或 std::pair)。是否可以插入自定义小于/比较函数?

c++ - 在 PropertyPage 上的控件上显示工具提示。将 TRUE 返回到 ON_NOTIFY_EX(TTN_NEEDTEXT...) 时崩溃

r - 从向量中选择所有其他元素

c++ - 重载运算符 ->