C ++错误,而使用< set> [英] C++ Errors while using <set>
问题描述
我保持实例化的错误。尝试修复它,不管是什么,我似乎无法实现SET。尝试使用向量,但我不知道如何正确实现一个等式运算符和失败。
一开始我使用向量,但是很难使用, x和y值是唯一的。意思是,我会读一个数据的文本文件。 点1将从点2进入不同的向量,然后单独存储数据。但不知何故,在文本文件中有Point1,[3,10]的重复值。我不想插入另一个相同的记录,并希望它们是独一无二的。所以我尝试使用集合,但我不能真正插入数据。 错误是: 在成员函数'bool std :: les <_Tp> :: operator()(const _Tp& const Point.h Main.cpp 如果你想把Point放在一个集合中,那么设置需要一种方法来比较一个Point和另一个Point,这样它就可以按顺序排列。您必须声明此函数 这样做就可以实例化 通常的实现是 这个命令并不重要, d可能已经意识到你需要 I kept geting instantiated errors. Try fixing it, no matter what, I can't seem to implement SET. Tried using vectors but I have no idea how to implement an equality operator properly and failed too. At first I was using vectors but it was hard to use while trying to keep x and y values unique. Meaning, I would read a text file of data.
So Point 1 will go to different vector from Point 2 and then storing the data seperately. But somehow, in the text file there are repeat values of Point1, [3, 10]. I do not want to inset another same record and want them to be unique. So i tried using set but I could not really insert in the data. Errors are: In member function 'bool std::les<_Tp>::operator()(const _Tp&, const
_Tp&) const [with _Tp = Point]': STL_TREE.h:980: instantiated from 'std::pair::iterator, bool> std::_Rb_tree<_Key,.... STL_SET.h:307: instantiated from 'std::pair, _Compare, typename
_Alloc::rebind<_Key>::other>::const_interator, bool> std::set..... main.cpp:141: instantiated from here stl_function.h:227: error: no
match for 'operator<' in '_x <_y' Point.h Main.cpp
If you want to put Point in a set, then set needs a way to compare one Point with another so it can put them in order. You must declare this function Do that and you will be able to instantiate The usual implementation would be It doesn't really matter what the order is (I assume otherwise you'd have probably realised you needed 这篇关于C ++错误,而使用< set>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Point1,[3,10]
p>
_Tp&)const [with _Tp = Point]':STL_TREE.h:980:实例化从'std :: pair :: iterator,bool> std :: _ Rb_tree< _Key,... STL_SET.h:307:实例化从'std :: pair,_Compare,typename
_Alloc :: rebind< _Key> :: other> :: const_interator,bool> std :: set ..... main.cpp:141:从这里实例化stl_function.h:227:error:no
匹配'operator<'in '_ x _y'
#ifndef POINT_H
#define POINT_H
#include< iostream>
#include< ostream>
#include< fstream>
#include< sstream>
#include< vector>
#include< math.h>
#include< set>
#include< string.h>
#include< string>
#include< stdio.h>
using namespace std;
class Point
{
friend ostream& operator<<(ostream& const; const Point&);
protected:
int x,y;
public:
// c'tor
Point(int xx,int yy);
// setters
void setX(int x1);
void setY(int y1);
// getters
int getX()const {return x;};
int getY()const {return y;};
};
Point :: Point(int xx,int yy):x(xx),y(yy)
{
}
$ b b void Point :: setX(int x1)
{
x = x1;
}
void Point :: setY(int y1)
{
y = y1;
}
#endif
#includePoint.h
#include< iostream>
#include< ostream>
#include< fstream>
#include< sstream>
#include< set>
#include< math.h>
#include< set>
#include< string.h>
#include< string>
#include< stdio.h>
#include< map>
#include< algorithm>
#include< map>
using namespace std;
class Main
{
public:
void mainMenu();
void stringToUpper(string& s);
};
void stringToUpper(string& s)
{
for(unsigned int l = 0; l< s.length(); l ++)
{
s [l] = toupper(s [1]);
}
}
void Main :: mainMenu()
{
cout<1)类型文件名和读取数据< ; endl;
cout<<2)查看数据<< endl;
cout<<Q)输入'Q'退出<< endl<< endl;
}
int main()
{
Main main;
char menuChoice;
bool quit = false;
int count = 0;
set< Point> p;
while(!quit)
{
main.mainMenu();
cout<< 请输入您的选择:;
cin>> menuChoice;
menuChoice = toupper(menuChoice);
switch(menuChoice)
{
case'1':
{
string line,name;
char filename [50];
int x,y;
cout<< 请输入文件名:;
cin>>文件名;
ifstream myfile(filename);
if(myfile.is_open())
{
while(myfile.good())
{
getline(myfile,line);
{
for(unsigned int i = 0; i< line.size(); ++ i)
{
if(line [i] =='[' || line [i] ==']'|| line [i] ==',')
{
line [i] ='
}
istringstream(line);
在>> name>> x>> y;
}
if(name ==Point)
{
p.insert(Point(x,y))}
}
count ++;
}
myfile.close();
cout<<计数;
}
else cout<< 无法打开文件;
}
break;
case'2':
{
cout<<view data<< endl;
}
case'Q':
cout<<你已经选择退出了!<< endl;
quit = true;
exit(0);
default:
cout<<Invalid entry<< endl<< endl;
break;
}
}
}
bool operator<(const Point& p1,const Point& p2)
{
...
}
std :: set< Point>
bool operator<(const Point& p1,const Point& p2)
{
return p1.x& p2.x || p1.x == p2.x&&& p1.y< p2.y;
}
运算符<
更快),只要你遵守 strict weak ordering 的规则。 Point1, [3, 10]
Point2, [10, 10]
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
#include <set>
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;
class Point
{
friend ostream& operator<<(ostream&, const Point&);
protected:
int x, y;
public:
//c'tor
Point(int xx, int yy);
//setters
void setX(int x1);
void setY(int y1);
//getters
int getX() const {return x;};
int getY() const {return y;};
};
Point::Point(int xx, int yy) : x(xx), y(yy)
{
}
void Point::setX(int x1)
{
x=x1;
}
void Point::setY(int y1)
{
y=y1;
}
#endif
#include "Point.h"
#include <iostream>
#include <ostream>
#include <fstream>
#include <sstream>
#include <set>
#include <math.h>
#include <set>
#include <string.h>
#include <string>
#include <stdio.h>
#include <map>
#include <algorithm>
#include <map>
using namespace std;
class Main
{
public:
void mainMenu();
void stringToUpper(string &s);
};
void stringToUpper(string &s)
{
for(unsigned int l=0; l < s.length(); l++)
{
s[l] = toupper(s[l]);
}
}
void Main::mainMenu()
{
cout<<"1) type filename and read data"<<endl;
cout<<"2) View data"<<endl;
cout<<"Q) Enter 'Q' to quit"<<endl<<endl;
}
int main()
{
Main main;
char menuChoice;
bool quit=false;
int count = 0;
set<Point> p;
while ( !quit)
{
main.mainMenu();
cout << "Please enter your choice : ";
cin>>menuChoice;
menuChoice = toupper(menuChoice);
switch(menuChoice)
{
case '1':
{
string line, name;
char filename[50];
int x,y;
cout << "Please enter filename : ";
cin >> filename;
ifstream myfile(filename);
if (myfile.is_open())
{
while ( myfile.good() )
{
getline(myfile, line);
{
for (unsigned int i = 0; i < line.size(); ++i)
{
if (line[i] == '[' || line[i] == ']' || line[i] == ',')
{
line[i] = ' ';
}
istringstream in(line);
in >> name >> x >> y;
}
if(name == "Point")
{
p.insert(Point(x,y)) }
}
count++;
}
myfile.close();
cout << count;
}
else cout<< "Unable to open file";
}
break;
case '2':
{
cout<<"view data"<<endl;
}
case 'Q':
cout<<"You have chosen to quit!"<<endl;
quit=true;
exit(0);
default:
cout<<"Invalid entry"<<endl<<endl;
break;
}
}
}
bool operator<(const Point& p1, const Point& p2)
{
...
}
std::set<Point>
bool operator<(const Point& p1, const Point& p2)
{
return p1.x < p2.x || p1.x == p2.x && p1.y < p2.y;
}
operator<
sooner) as long as you obey the rules of strict weak ordering.