Operater<重载结构问题 [英] Operater < overloading for struct problem

查看:87
本文介绍了Operater<重载结构问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我无法重载<作业的操作员。我用
使用一个包含信息的结构,我想用我自己的排序标准对这个

结构进行排序。基本上,我

想要对吸引力结构的访客数进行排序。

但是,它从不使用<用我的代码重载操作符。


Handler.h:


#ifndef H_HANDLER // Guard

#define H_HANDLER


#include< iostream>

#include< vector>

using namespace std;
< br $>
struct Attraction {

string name;

int visitor;

};


class Handler {//定义类Handler


private:

vector<景点*景点;

vector< ;吸引力*> :: iterator p;


public://公共功能

~Handler();

void addAttraction(字符串名称,int访问者);

void printAttractions();

};

#endif


和Handler.cpp我有:


Handler.cpp - 代码:


bool operator<(const Attraction& a,const Attraction& b){

返回a.visitors< b.visitors;

}


这是添加值后执行排序的函数:


void Handler :: addAttraction(string name,int visitor){


Attraction * attr;

attr = new Attraction();


attr-> name = name;

attr-> visitor = visitor;

attractions.push_back(attr);

sort(attractions.begin(),attractions.end());


}


然而,无论我做什么,它永远不会使用重载<运营商

进行排序。我究竟做错了什么?如果我在标题中添加了重载函数

,它会开始抱怨,因为它也会被插入到主程序中,因为我有一个后卫,因此它会让人感到困惑。 >
头文件。


问候,

弗兰克

Hello everyone,

I am having trouble overloading the < operator for an assignment. I
use a struct that contains information and I would like to sort this
structure using STL sort with my own criteria of sorting. Basically, I
would like to sort on visitor count of the Attraction structure.
However, it never uses the < overloaded operator with my code.

Handler.h:

#ifndef H_HANDLER //Guard
#define H_HANDLER

#include <iostream>
#include <vector>
using namespace std;

struct Attraction {
string name;
int visitors;
};

class Handler { //Define the class Handler

private:
vector<Attraction*attractions ;
vector<Attraction*>::iterator p ;

public: //Public functions
~Handler();
void addAttraction(string name, int visitors);
void printAttractions();
};
#endif

and in the Handler.cpp I have:

Handler.cpp - snippet:

bool operator<(const Attraction& a,const Attraction& b){
return a.visitors < b.visitors;
}

Here is the function that performs the sort after adding a value:

void Handler::addAttraction(string name, int visitors){

Attraction *attr;
attr=new Attraction();

attr->name=name;
attr->visitors=visitors;
attractions.push_back(attr);
sort(attractions.begin(),attractions.end());

}

However, whatever I do, it will never use the overloaded < operator
for sorting. What am I doing wrong? If I add the overloaded function
in the header it starts complaining because it will also be inserted
into the main program which is confusing since I have a guard around
the header file.

Regards,
Frank

推荐答案

Frank写道:
Frank wrote:

我无法重载<作业的操作员。我用
使用一个包含信息的结构,我想用我自己的排序标准对这个

结构进行排序。基本上,我

想要对吸引力结构的访客数进行排序。

但是,它从不使用<用我的代码重载操作符。


Handler.h:


#ifndef H_HANDLER // Guard

#define H_HANDLER


#include< iostream>

#include< vector>

using namespace std;
< br $>
struct Attraction {

string name;

int visitor;

};


class Handler {//定义类Handler


private:

vector<景点*景点;

vector< ;吸引力*> :: iterator p;


public://公共功能

~Handler();

void addAttraction(字符串名称,int访问者);

void printAttractions();

};


#endif


和Handler.cpp中我有:


Handler.cpp - 代码片段:


bool运算符< (const吸引力& a,const吸引力& b){

返回a.visitors< b.visitors;

}


这是添加值后执行排序的函数:


void Handler :: addAttraction(string name,int visitor){


Attraction * attr;

attr = new Attraction();


attr-> name = name;

attr-> visitor = visitor;


attractions.push_back(attr);

sort(attractions.begin(),attractions.end());


}


然而,无论如何我这样做,它永远不会使用重载<运营商

进行排序。我究竟做错了什么?
I am having trouble overloading the < operator for an assignment. I
use a struct that contains information and I would like to sort this
structure using STL sort with my own criteria of sorting. Basically, I
would like to sort on visitor count of the Attraction structure.
However, it never uses the < overloaded operator with my code.

Handler.h:

#ifndef H_HANDLER //Guard
#define H_HANDLER

#include <iostream>
#include <vector>
using namespace std;

struct Attraction {
string name;
int visitors;
};

class Handler { //Define the class Handler

private:
vector<Attraction*attractions ;
vector<Attraction*>::iterator p ;

public: //Public functions
~Handler();
void addAttraction(string name, int visitors);
void printAttractions();
};
#endif

and in the Handler.cpp I have:

Handler.cpp - snippet:

bool operator<(const Attraction& a,const Attraction& b){
return a.visitors < b.visitors;
}

Here is the function that performs the sort after adding a value:

void Handler::addAttraction(string name, int visitors){

Attraction *attr;
attr=new Attraction();

attr->name=name;
attr->visitors=visitors;
attractions.push_back(attr);
sort(attractions.begin(),attractions.end());

}

However, whatever I do, it will never use the overloaded < operator
for sorting. What am I doing wrong?



您的运营商<是为_objects_定义的,但是你的向量存储了

_pointers_。删除''new'',删除指针,保留对象,然后

一切都会好的。


你可以声明/定义一个局部的Attraction对象在''addAttraction''

然后push_back它,向量将复制。

Your operator< is defined for _objects_, but your vector is storing
_pointers_. Drop the ''new'', drop the pointers, keep objects, and
everything will be fine.

You can declare/define a local Attraction object in ''addAttraction''
and then push_back it, the vector will make a copy.


[..]
[..]



V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


3月1日上午10点45分,Frank < ffnee ... @ gmail.comwrote:
On Mar 1, 10:45 am, "Frank" <f.f.nee...@gmail.comwrote:

大家好,


我在重载<时遇到问题;作业的操作员。我用
使用一个包含信息的结构,我想用我自己的排序标准对这个

结构进行排序。基本上,我

想要对吸引力结构的访客数进行排序。

但是,它从不使用<用我的代码重载操作符。


Handler.h:


#ifndef H_HANDLER // Guard

#define H_HANDLER


#include< iostream>

#include< vector>

using namespace std;
< br $>
struct Attraction {

string name;

int visitor;


};


类Handler {//定义类Handler


私有:

vector<景点*景点;

vector< Attraction *> :: iterator p;


public://公共功能

~Handler();

void addAttraction(string name,int visitor);

void printAttractions();


};


#endif


和Handler.cpp我有:


Handler.cpp - 代码:

bool op erator<(const吸引力& a,const吸引力& b){

返回a.visitors< b.visitors;

}


这是添加值后执行排序的函数:


void Handler :: addAttraction(string name,int visitor){


Attraction * attr;

attr = new Attraction();


attr-> name = name;

attr-> visitor = visitor;


attractions.push_back(attr);

sort(attractions.begin(),attractions.end());


}


然而,无论如何我这样做,它永远不会使用重载<运营商

进行排序。我究竟做错了什么?如果我在标题中添加了重载函数

,它会开始抱怨,因为它也会被插入到主程序中,因为我有一个后卫,因此它会让人感到困惑。 >
头文件。
Hello everyone,

I am having trouble overloading the < operator for an assignment. I
use a struct that contains information and I would like to sort this
structure using STL sort with my own criteria of sorting. Basically, I
would like to sort on visitor count of the Attraction structure.
However, it never uses the < overloaded operator with my code.

Handler.h:

#ifndef H_HANDLER //Guard
#define H_HANDLER

#include <iostream>
#include <vector>
using namespace std;

struct Attraction {
string name;
int visitors;

};

class Handler { //Define the class Handler

private:
vector<Attraction*attractions ;
vector<Attraction*>::iterator p ;

public: //Public functions
~Handler();
void addAttraction(string name, int visitors);
void printAttractions();

};

#endif

and in the Handler.cpp I have:

Handler.cpp - snippet:

bool operator<(const Attraction& a,const Attraction& b){
return a.visitors < b.visitors;
}

Here is the function that performs the sort after adding a value:

void Handler::addAttraction(string name, int visitors){

Attraction *attr;
attr=new Attraction();

attr->name=name;
attr->visitors=visitors;

attractions.push_back(attr);
sort(attractions.begin(),attractions.end());

}

However, whatever I do, it will never use the overloaded < operator
for sorting. What am I doing wrong? If I add the overloaded function
in the header it starts complaining because it will also be inserted
into the main program which is confusing since I have a guard around
the header file.



我怀疑基本问题是你在排序指针

而不是对象。换句话说,std :: sort适用于容器的value_type

,在你的情况下是Attraction *,而不是Attraction。


所以,做你需要以多态方式对Attraction对象进行操作吗?

如果没有,不要使用指针;只需将它分配到堆栈上,然后让

矢量复制(假设景点现在类型为

vector< Attraction>:


void Handler :: addAttraction(const string& name,const int visitor)

{

const Attraction attr = {name,visitors}}

attractions.push_back(attr);

sort(attractions.begin(),attractions.end());

}


另请注意,我将函数参数更改为const(请参阅
http://www.parashift.com/c++-faq-lit...rrectness.html )和

字符串到引用所以它没有额外的副本(参见
http://www.parashift.com/c++-faq-lite/references.html)


如果你确实需要多态地使用它并存储它在向量中,
更喜欢智能指针,例如std :: tr1 :: shared_ptr(又名

boost :: shared_ptr)或此常见问题解答中的智能指针之一和

以下人员:

http://www.parashift.com/c++-faq-lit...html#faq-16.22


BTW,你可以把只是运营商的功能原型<在

标题中你可以声明它内联摆脱

重复问题。


干杯! --M

I suspect the fundamental problem is that you are sorting pointers
rather than objects. In other words, std::sort works on the value_type
of the container, which in your case is Attraction*, not Attraction.

So, do you need to operator on the Attraction object polymorphically?
If not, don''t use pointers; just allocate it on the stack, and let
vector make a copy (assuming attractions is now of type
vector<Attraction>:

void Handler::addAttraction( const string& name, const int visitors )
{
const Attraction attr = { name, visitors };
attractions.push_back(attr);
sort(attractions.begin(),attractions.end());
}

Note also that I changed the function parameters to const (see
http://www.parashift.com/c++-faq-lit...rrectness.html) and the
string to a reference so it doesn''t make an additional copy (see
http://www.parashift.com/c++-faq-lite/references.html).

If you do need to use it polymorphically and to store it in a vector,
prefer smart pointers such as std::tr1::shared_ptr (aka
boost::shared_ptr) or one of the smart pointers found in this FAQ and
those following:

http://www.parashift.com/c++-faq-lit...html#faq-16.22

BTW, you could put just the function prototype for your operator< in
the header or you could declare it "inline" to get rid of the
duplication problem.

Cheers! --M


Frank写道:


Victor和mlimber解决了你的主要问题。我想在另一个项目上致电


Frank wrote:

Victor and mlimber have addressed your main issue. I''d like to address
one other item.

Handler.h:


#ifndef H_HANDLER //警卫

#define H_HANDLER


#include< iostream>

#include< vector>

using namespace std;
Handler.h:

#ifndef H_HANDLER //Guard
#define H_HANDLER

#include <iostream>
#include <vector>
using namespace std;



Do * NOT * put" using namespace std;"进入头文件。它可以

严重搞乱其他代码。在所有include指令之后,它应该只进入非头文件,



Do *NOT* put "using namespace std;" into a header file. It can
seriously mess up other code. It should only go into non-header files,
after all include directives.


这篇关于Operater&lt;重载结构问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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