具有C++设置错误的用户对象

User objects with C++ set error

本文关键字:用户 对象 错误 C++ 设置 具有      更新时间:2023-10-16

我似乎在比较集合中的对象时遇到了问题。
将项目添加到集合中一切正常,但是当我尝试使用该set::find时,我收到以下错误。我尝试研究但无法解决此问题

剧院.cpp

#ifndef _THEATER_CPP
#define _THEATER_CPP
#include <iostream>
#include <string>
#include <set>
#include "theater.h"
//struct comparebyname {
//bool operator ()(customer const& a, customer const& b) const
//{
 // return a.getname() < b.getname();
//}
//};

//class comp
//{
//public:
//bool operator()(const customer a1,const  customer a2) const
//{
//return a1.getname()<a2.getname();
//}
//};

//using namespace std;
//initializes a default theater to be changed
theater::theater()
{
string moviename ="theater";
int rows = 0;
int seats = 0;
double price = 0.00;
set<int> emptyseats;
set<customer> patrons;

};


//accessors
string theater::getname()
{
return moviename;
}
int theater::getrows()
{
return rows;
}
int theater::getseats()
{
return seats;
}
double theater::getprice()
{
return price;
}

int theater::getbestemptyseat()
{
set<int>::iterator it;
int i;
it = emptyseats.begin();
i = *it;
emptyseats.erase(it);
return i;
}
int theater::numberseatsavailable()
{
return emptyseats.size();
}

//mutators
void theater::setname(string s)
{
moviename=s;
}
void theater::setrows(int i)
{
    rows = i;
}
void theater::setseats(int i)
{
seats = i;
}
void theater::setprice(double d)
{
price=d;
}
void theater::makeseatempty(int i)
{
emptyseats.insert(i);
}


void theater::addpatron(customer  c)
{
patrons.insert(c);
}
void theater::removepatron(customer c)
{
patrons.erase(c);
}

set<customer> theater::getpatrons()
{
customer hellyeah;
set<customer>::iterator it;
int i;
//for ( it=patrons.begin();it!=patrons.end();it++)
//{
  //  hellyeah=*it;
//    cout<<"nnn aaa"<<hellyeah.getname()<<" nn";
    //set<customer>::iterator it;
//
// }
it=patrons.find("help");
}


//bool operator ==(customer const& a, customer const& b)
//{
//  return a.getname() == b.getname();/
//
//}
bool operator ()(customer const& a, customer const& b) const
{
return a.name < b.name;
}
bool operator <(customer const& a, customer const& b)
{
return a.getname() < b.getname();
}
bool operator >(customer const& a, customer const& b)
{
    return a.getname() > b.getname();
}
bool operator ==(customer const& a, customer const& b)
{
    return a.getname() == b.getname();
}
#endif

剧院。

#ifndef _THEATERT_H
#define _THEATER_H
#include "customer.h"
//#include <string>

using namespace std;

class theater
{
public:

    // constructors
    theater();  // default constructor
    // destructor
    //~myObject();
    //accessors
    string getname();
    int getrows();
    int getseats();
    double getprice();
    set<customer> getpatrons();
    //technically a mutator?
    int getbestemptyseat();
    int numberseatsavailable();

    //mutators
    void setname(string);
    void setrows(int);
    void setseats(int);
    void setprice(double);
    void makeseatempty(int);
    void addpatron(customer);
    void removepatron(customer);


private:

    string moviename;
    int rows;
    int seats;
    double price;
    set<int> emptyseats;
    //comp com1;
    set<customer> patrons;

};

#endif

客户.cpp

#ifndef _CUSTOMER_CPP
#define _CUSTOMER_CPP
#include <iostream>
#include <string>
//#include<set>
#include "customer.h"
//using namespace std;
//initializes a default customer to be changed
customer::customer()
{
    string name ="theater";
    bool under12 = false;
    int partysize=0;
    };


//accessors
string customer::getname() const
{
return name;
}
bool customer::getunder12()
{
    return under12;
}
int customer::getpartysize()
{
    return partysize;
}


//mutators
void customer::setname(string s)
{
    name=s;
}
void customer::setunder12(bool b)
{
    under12 = b;
}
void customer::setpartysize(int i)
{
    partysize = i;
}
//Operators
/*
bool operator <(customer const& a, customer const& b) const
{
    return a.getname() < b.getname();
}
bool operator ==(customer const& a, customer const& b) const
{
    return a.getname() == b.getname();
}
*/
//bool operator==(const Point& rhs) const
//{
//    return ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y());
//}
//bool operator<(const Point& rhs) const
//{
//    return (int)x_coordinate < (int)rhs.get_x();
//}

#endif



#ifndef _CUSTOMERT_H
#define _CUSTOMER_H
//#include <string>

using namespace std;
class customer
{
public:

    // constructors
    customer();  // default constructor
    // destructor
    //~myObject();
    //accessors
    string getname() const;
    bool getunder12();
    int getpartysize();
    //void getseatlocation

    //mutators
    void setname(string);
    void setunder12(bool);
    void setpartysize(int);
    //void addseatlocation(int);
//operators
    //bool operator <(customer) const;
    bool operator ==(customer) const;


private:

    string name;
    bool under12;
    int partysize;
    //vector<int> seatlocation;


};

#endif

我收到的错误:

C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp||In constructor 'theater::theater()':|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|35|warning: unused variable 'rows'|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|36|warning: unused variable 'seats'|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|37|warning: unused variable 'price'|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp||In member function 'std::set<customer, std::less<customer>, std::allocator<customer> > theater::getpatrons()':|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|142|error: no matching function for call to 'std::set<customer, std::less<customer>, std::allocator<customer> >::find(const char [5])'|
c:program files (x86)codeblocksmingwbin..libgccmingw324.4.1includec++bitsstl_set.h|547|note: candidates are: typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator std::set<_Key, _Compare, _Alloc>::find(const _Key&) [with _Key = customer, _Compare = std::less<customer>, _Alloc = std::allocator<customer>]|
c:program files (x86)codeblocksmingwbin..libgccmingw324.4.1includec++bitsstl_set.h|551|note:                 typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator std::set<_Key, _Compare, _Alloc>::find(const _Key&) const [with _Key = customer, _Compare = std::less<customer>, _Alloc = std::allocator<customer>]|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|132|warning: unused variable 'i'|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|143|warning: no return statement in function returning non-void|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|155|error: non-member function 'bool operator()(const customer&, const customer&)' cannot have cv-qualifier|
C:UsersMattDropboxschooldata_structuresdata_structures_final_projectmsdtesttheater.cpp|155|error: 'bool operator()(const customer&, const customer&)' must be a nonstatic member function|
||=== Build finished: 3 errors, 5 warnings ===|

要搜索集合,您必须传入 Customer 的实例,而不是字符串。如果你只想传入一个字符串,你需要使用 std::map

要使用您拥有的内容,请执行以下操作

customer help_customer;
help_customer.set_name("help");
it = patrons.find(help_customer);