1

我似乎在比较一组对象时遇到问题。
将项目添加到集合中一切正常,但是当我尝试使用时,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<<"\n\n\n aaa"<<hellyeah.getname()<<" \n\n";
    //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

剧院.h

#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:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp||In constructor 'theater::theater()':|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|35|warning: unused variable 'rows'|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|36|warning: unused variable 'seats'|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|37|warning: unused variable 'price'|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp||In member function 'std::set<customer, std::less<customer>, std::allocator<customer> > theater::getpatrons()':|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.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)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_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)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_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:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|132|warning: unused variable 'i'|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|143|warning: no return statement in function returning non-void|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|155|error: non-member function 'bool operator()(const customer&, const customer&)' cannot have cv-qualifier|
C:\Users\Matt\Dropbox\school\data_structures\data_structures_final_project\msdtest\theater.cpp|155|error: 'bool operator()(const customer&, const customer&)' must be a nonstatic member function|
||=== Build finished: 3 errors, 5 warnings ===|
4

1 回答 1

2

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

要使用您所拥有的,请执行此操作

customer help_customer;
help_customer.set_name("help");
it = patrons.find(help_customer);
于 2012-12-01T01:46:17.237 回答