奇怪的“成员功能不可行"模板化线性代数向量类中的错误 [英] Strange "Member function not viable" error in templated linear algebra vector class

查看:110
本文介绍了奇怪的“成员功能不可行"模板化线性代数向量类中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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>).

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

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

VecXd<T> temp = *this;

帮助?

推荐答案

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

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; }

(请注意,返回类型中的const绝对无效,因此您应该摆脱它)

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

这篇关于奇怪的“成员功能不可行"模板化线性代数向量类中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆