Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Another problem with std::list<>::sort()

807575Mar 9 2005 — edited Sep 1 2005
Hello !

this compiles well with gcc, the code is taken from Bjarne Stroustrups homepage, with only minor modifications.

http://www.research.att.com/~bs/club.c

I think the problem is located here:

list<Person*> off;

off.sort();

If possible I'd like to see how this can be coded in sun studio 9.

I haven't contacted Stroustrup about this topic,
I think it must be possible to find a sun specific workaround to remedy this. Perhaps even easier.


Best regards

Morten Gulbrandsen

#include <list>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
#include <iterator>


using namespace std;

namespace 
{
  string noname = "";
  string noaddr = "";
}

struct Person 
{
  string name;
  string addr;

  Person(const string& n, const string& a = noaddr) :name(n), addr(a) { }
};



struct Club 
{
  string        name;
  list<Person*> members;
  list<Person*> officers;
  string        town;

  Club(const string& n) :name(n) {}
};



class Club_eq : public unary_function<Club, bool> 
{
  string s;

public:
  explicit Club_eq(const string& ss) : s(ss) { }
  bool operator()(const Club& c) const { return c.name==s; }
};



class Extract_officers 
{
	list<Person*>& lst;
public:
	explicit Extract_officers(list<Person*>& x) : lst(x) { }

	void operator() (const Club& c) const
	{ copy(c.officers.begin(), c.officers.end(), back_inserter(lst)); }
};

void extract(const list<Club>& lc, list<Person*>& off)
{
	for_each(lc.begin(), lc.end(), Extract_officers(off));
}




class Print_name 
{
	ostream& os;

public:
	Print_name(ostream& s) :os(s) { }
	void operator()(Person* p) { os << p->name << endl; }
};


void extract_and_print(const list<Club>& lc)
{
  cout << "\nPrint officers:\n";

  list<Person*> off;

  extract(lc, off);

  for_each(off.begin(), off.end(), Print_name(cout));
}

string nameof(const Club& c) { return c.name; }



void f(list<Club>& lc)
{
  cout << "\nPrint names:\n";

  transform(lc.begin(), lc.end(), ostream_iterator<string>(cout, "\n"), nameof);
}

bool operator==(const Person& x, const Person& y)
{
	
  return x.name == y.name;     
}

bool operator<(const Person& x, const Person& y)
{
     
  return x.name < y.name;	
}

bool Person_eq(const Person* x, const Person* y)
{
  return *x == *y;
}

bool Person_lt(const Person* x, const Person* y)
{
  return *x < *y;
}


void extract_and_print2(const list<Club>& lc)
{
  cout << "\nPrint officers in alphabetic order:\n";

  list<Person*> off;   
  extract(lc, off);

  // line 123: Error:
  off.sort(Person_lt);  // line 123: Error:

  list<Person*>::iterator p = unique(off.begin(), off.end(), Person_eq);
  for_each(off.begin(), p, Print_name(cout));
}



class located_in : public unary_function<Club, bool> 
{
  string town;

public:
  located_in(const string& ss) :town(ss) { }
  bool operator()(const Club& c) const { return c.town == town; }
};


void ff(list<Club>& lc)
{
  cout << "\nPrint clubs located in Kbh:\n";

  remove_copy_if(lc.begin(), lc.end(),
		 ostream_iterator<Club>(cout), not1(located_in("Kbh")));
}

ostream& operator<<(ostream& s, const Person& p)
{
  return s << "(" << p.name << ", " << p.addr << ")";
}

ostream& operator<<(ostream& s, const list<Person*>& c)
{
  for (list<Person*>::const_iterator p = c.begin(); p!=c.end(); ++p)
    s << **p << ", ";

  return s;
}

template<class T> ostream& operator<<(ostream& s, const list<T>& c)
{

  copy(c.begin(), c.end(), ostream_iterator<T>(s, ", "));

  return s;
}

ostream& operator<<(ostream& s, const Club& c)
{
  s << c.name << "{ ";
  s << "[off: " << c.officers << " ]";
  s << "[mem: " << c.members << " ]";
  s << c.town ;

  return s << " }\n";
}


void make_person_list(list<Person*>& lst,
		      const string& s1 = noname,
		      const string& s2 = noname,
		      const string& s3  = noname)
{
  if (&s1!=&noname) lst.push_back(new Person(s1));
  if (&s2!=&noname) lst.push_back(new Person(s2));
  if (&s3!=&noname) lst.push_back(new Person(s3));
}



int main()
{
  list<Club> snoot;
  snoot.push_back(Club("Horsy"));
  make_person_list(snoot.back().members,  "Major", "Tops");
  make_person_list(snoot.back().officers, "Major", "Tops");
  snoot.back().town = "Kbh";
  snoot.push_back(Club("Hunt"));
  make_person_list(snoot.back().members, "Elk", "Fox", "Hare");
  make_person_list(snoot.back().officers, "Riffle");
  snoot.back().town = "Kbh";
  snoot.push_back(Club("Over_eat"));
  make_person_list(snoot.back().members, "Dinner", "Breakfast", "Lunch");
  make_person_list(snoot.back().officers, "Dinner", "Lunch");
  snoot.back().town = "Paris";
  snoot.push_back(Club("Drink_and_be_merry"));
  make_person_list(snoot.back().members, "Beer", "Red", "Wiskey");
  make_person_list(snoot.back().officers, "Red");
  snoot.back().town = "Paris";

  cout << snoot;

  f(snoot);

  ff(snoot);

  extract_and_print(snoot);

  extract_and_print2(snoot);

  return 0;
}
bash-2.05$ CC -V
CC: Sun C++ 5.6 2004/06/02

bash-2.05$ CC -o std_list.out std_list.cpp

"std_list.cpp", line 123:

Error: Could not find a match for
std::list<Person*>::sort(bool(const Person*,const Person*)) 

needed in 
extract_and_print2(const std::list<Club>&).
1 Error(s) detected.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 29 2005
Added on Mar 9 2005
10 comments
265 views