在自定义矢量类中重载求和运算符 [英] Overloading Sum Operator in Custom Vector Class

查看:156
本文介绍了在自定义矢量类中重载求和运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个练习,了解更多关于C ++中的动态内存分配,我正在研究我自己的向量类。我遇到了一个困难重载sum运算符,所以我想我会转向这里来了解一下为什么这不工作。这里是我到目前为止:

 模板< typename T& 
类向量
{
private:
T * pointer_;
unsigned long size_;
public:
//构造函数和析构函数。
vector();
template< typename A> vector(const A&);
template< typename A> vector(const A& const T&);
vector(const vector< T>&;);
〜vector();

//方法。
unsigned long size();
T * begin();
T * end();
vector< T> push_back(const T&);

//运算符。
T operator [](unsigned long);
vector< T> operator =(const vector< T&;);
friend vector< T>运算符+(const vector< T& amp; const vector< T>);
};

模板< typename A> vector(const A&)构造函数看起来像这样:

  template< typename T& template< typename A> 
vector< T> :: vector(const A& size)
{
this-> pointer_ = new T [size];
this-> size_ = size;
}

最后,运算符+ 运算符看起来像这样:

  template< typename T& 
vector< T>运算符+(const向量< T>& lhs,const向量& rhs)
{
vector&结果(lhs.size_);
for(unsigned long i = 0; i!= result.size_; i ++)
{
result.pointer_ [i] = lhs.pointer_ [i] + rhs.pointer_ [i] ;
}
return result;
}



我的编译器(VS2013)返回一个未解析的外部符号错误,当我尝试编译此代码,这(我理解它)意味着有一个函数声明的某处,我没有实际定义。我不知道问题是在哪里: template< typename A> vector(const A&)构造函数工作正常。我缺少什么?

解决方案

您没有正确加入模板操作符功能。有多种方法可以做到这一点,每个都有优势。一个是朋友世界的意识形态,其中模板的所有扩展是彼此的朋友。

您的向量类中,执行以下操作:








$ b

  template< typename T> 
class vector;

template< typename T>
vector< T>运算符+(const向量>& lhs,const向量& rhs);

像以前一样进行,但注意朋友声明中的语法:

  //向量类在这里.... 
模板< typename T>类向量
{
.... stuff ....

friend vector< T>运算符+<>(const vector< T& const; const vector< T&
};

然后定义其余的。



最好的运气。



PS:无效的参考返回值固定在上述代码中。


As an exercise to learn a little more about dynamic memory allocation in C++, I'm working on my own vector class. I'm running into a little difficulty overloading the sum operator, so I thought I'd turn here to get some insight into why this doesn't work. Here's what I have so far:

template<typename T>
class vector
{
private:
    T* pointer_;
    unsigned long size_;
public:
    // Constructors and destructors.
    vector();
    template<typename A> vector(const A&);
    template<typename A> vector(const A&, const T&);
    vector(const vector<T>&);
    ~vector();

    // Methods.
    unsigned long size();
    T* begin();
    T* end();
    vector<T>& push_back(const T&); 

    // Operators.
    T operator[](unsigned long);
    vector<T>& operator=(const vector<T>&);
    friend vector<T>& operator+(const vector<T>&, const vector<T>&);
};

The template<typename A> vector(const A&) constructor looks like this:

template<typename T> template<typename A>
vector<T>::vector(const A& size)
{
    this->pointer_ = new T[size];
    this->size_ = size;
}

Finally, the operator+ operator looks like this:

template<typename T>
vector<T>& operator+(const vector<T>& lhs, const vector<T>& rhs)
{
    vector<T> result(lhs.size_);
    for (unsigned long i = 0; i != result.size_; i++)
    {
        result.pointer_[i] = lhs.pointer_[i] + rhs.pointer_[i];
    }
    return result;
}

My compiler (VS2013) returns an unresolved external symbol error when I try to compile this code, which (as I understand it) means there's a function declared somewhere that I haven't actually defined. I'm not sure where the problem is, however: the template<typename A> vector(const A&) constructor works fine. What am I missing?

解决方案

You're not properly friending the template operator function. There are multiple ways to do this, each of which has advantages. One is the friend-the-world ideology where all expansions of the template are friends with each other. I prefer to be a bit more restrictive than that.

Above your vector class, do this:

template<typename T>
class vector;

template<typename T>
vector<T> operator+(const vector<T>& lhs, const vector<T>& rhs);

The proceed as before, but note the syntax in the friend declaration:

// vector class goes here....
template<typename T> class vector
{
    .... stuff ....

   friend vector<T> operator+<>(const vector<T>&, const vector<T>&);
};

Then define the rest as you have it. That should get you what I think you're trying to achieve.

Best of luck.

PS: invalid reference return value fixed in the above code.

这篇关于在自定义矢量类中重载求和运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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