本文介绍了朋友运营商的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有矢量模板的代码。但是我遇到了问题。

Vector.hpp文件的开头如下:


template< class V,class I = int,class S = FullArray< V> >

类Vector:public Array< V,I,S>


{...


// Array和FullArray是已经构建的一些其他模板。

//这个Vector模板的目标是简单地为vector

//类创建一个带有一些数值功能的类,比如vector vector。


//这不是我原来的代码,但据我所知+运算符

//重载以使Vector类具有一些功能:

//添加两个相同维度的向量。并且通过将标量添加到向量的每个维度来添加向量和标量

//。这是在

//中完成以下方式:


//返回元素的总和

//运算符

矢量< V,I,S>& operator =(const Vector< V,I,S>& source);


Vector< V,I,S> operator - ()const; //一元减去


//为每个元素添加v

friend Vector< V,I,S> operator +(const Vector< V,I,S>& v,const V& a);


朋友矢量< V,I,S> operator +(const V& a,const Vector< V,I,S>& v);


//添加元素

Vector< V ,我,S> operator +(const Vector< V,I,S>& v)const;


//但是当我尝试在主函数中使用此模板时,我得到以下内容:


[警告]朋友声明`Vector< ; V,I,S> operator +(const Vector< V,I,S>&,const V&)''声明一个非模板函数


[警告](如果这不是你想要的,确保函数模板已经声明并在函数名称之后添加<>)-Wno-non-template-friend禁用此警告



我不要我不知道该怎么做。我真的很感激帮助。谢谢。

解决方案



不正确。矢量< V,I,S>类是本身,它是第一个参数,意味着它位于+运算符的左侧。当Vector位于+运算符的左侧时,编译器将提供此指针指向函数,因此您不需要Vector作为第一个参数。


这应该不是朋友。它应该是一个成员函数:

展开 | 选择 | Wrap | 行号





为了成为朋友,它必须是一个已经写好的函数。

展开 | 选择 | Wrap | 行号

Hello,

I have code for a vector template. However I am having problems with it.
Vector.hpp file starts like this:

template <class V, class I=int, class S=FullArray<V> >
class Vector: public Array<V, I, S>

{...

//Array and FullArray are some other templates that are already constructed.
//The goal of this Vector template is to simply create a template for vector
//classes with some numerical capabilities such as vector addition.

//This is not originally my code, however as far as I can understand + operator
//is overloaded to enable the Vector class some functionalities:
//Adding two vectors of same dimension. And also adding a vector and a scalar
//by adding the scalar to every dimension of the vector. This is beeing done in
//the following way:

// Return the sum of the elements
// Operators
Vector<V, I, S>& operator = (const Vector<V, I, S>& source);

Vector<V, I, S> operator - () const; // Unary minus

// Add v to every element
friend Vector<V, I, S> operator + (const Vector<V, I, S>& v, const V& a);

friend Vector<V, I, S> operator + (const V& a, const Vector<V, I, S>& v);

// Add the elements
Vector<V, I, S> operator + (const Vector<V, I, S>& v) const;

//However when I try to use this template in a main function I get the following:

[Warning] friend declaration `Vector<V, I, S> operator+(const Vector<V, I, S>&, const V&)'' declares a non-template function

[Warning] (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning


I don''t know what to do here. I would really appreciate help. Thanks.

解决方案

isn''t correct. Vector<V,I,S> is the class itself and it is the first argument meaning it is on the left of the + operator. When a Vector is on the left of the + operator, the compiler will supply the this pointer to the function so you don''t need a Vector as the first argument.

This should not be a friend. It should be a member function:

Expand|Select|Wrap|Line Numbers



For this to be a friend, it has to be a function that is already written.

Expand|Select|Wrap|Line Numbers


这篇关于朋友运营商的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 15:44
查看更多