By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

STL set (compilartion error) how to resolve this

P: n/a
Hi,
I want to create a set of cells (where it can be ints or doubles ).
The equatily condition is a.x==b.x and a.y==b.y.

I tried compiling this and it failed. Can someone help me.

#include<set>
#include<cstdio>
#include<iostream>
using namespace std;

struct cell {
int x,y;
};
cell a;

bool myeq(const cell &a,const cell &b)
{
if(a.x==b.x && a.y==b.y)
return true;
return false;
}

int main()
{
set <cell,myeqs;

s.insert(a);
return 0;
}

Jul 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Geo

te********@gmail.com wrote:
Hi,
I want to create a set of cells (where it can be ints or doubles ).
The equatily condition is a.x==b.x and a.y==b.y.

I tried compiling this and it failed. Can someone help me.

#include<set>
#include<cstdio>
#include<iostream>
using namespace std;

struct cell {
int x,y;
};
cell a;

bool myeq(const cell &a,const cell &b)
{
if(a.x==b.x && a.y==b.y)
return true;
return false;
}

int main()
{
set <cell,myeqs;

s.insert(a);
return 0;
}
In what way did it 'fail to compile', since you have provided no
output, does that mean you computer exploded as you trried to compile
it ?

Jul 4 '06 #2

P: n/a

Geo wrote:
te********@gmail.com wrote:
Hi,
I want to create a set of cells (where it can be ints or doubles ).
The equatily condition is a.x==b.x and a.y==b.y.

I tried compiling this and it failed. Can someone help me.

#include<set>
#include<cstdio>
#include<iostream>
using namespace std;

struct cell {
int x,y;
};
cell a;

bool myeq(const cell &a,const cell &b)
{
if(a.x==b.x && a.y==b.y)
return true;
return false;
}

int main()
{
set <cell,myeqs;

s.insert(a);
return 0;
}

In what way did it 'fail to compile', since you have provided no
output, does that mean you computer exploded as you trried to compile
it ?
$ g++ foo.cpp
foo.cpp: In function `int main()':
foo.cpp:22: error: type/value mismatch at argument 2 in template
parameter list
for `template<class _Key, class _Compare, class _Allocclass std::set'
foo.cpp:22: error: expected a type, got `myeq'
foo.cpp:22: error: invalid type in declaration before ';' token
foo.cpp:24: error: request for member `insert' in `s', which is of
non-class type `int'
$

Jul 4 '06 #3

P: n/a
te********@gmail.com wrote:
Hi,
I want to create a set of cells (where it can be ints or doubles ).
The equatily condition is a.x==b.x and a.y==b.y.

I tried compiling this and it failed. Can someone help me.

#include<set>
#include<cstdio>
#include<iostream>
using namespace std;

struct cell {
int x,y;
};
cell a;

bool myeq(const cell &a,const cell &b)
{
if(a.x==b.x && a.y==b.y)
return true;
return false;
}

int main()
{
set <cell,myeqs;

s.insert(a);
return 0;
}
You've got two separate problems. Firstly, set requires a strict weak
ordering function, not an equality function. So you need:

bool myless(const cell &a,const cell &b)
{
return a.x<b.x || (a.x==b.x && a.y<b.y);
}

Next, the second template parameter of set is a type, not a function. So
either make myless into a type:

struct myless
{
bool operator()(const cell &a,const cell &b) const
{
return a.x<b.x || (a.x==b.x && a.y<b.y);
}
};

or use the function:

set<cell,bool(*)(const cell &a,const cell &b)s(myless);

I think you'll prefer the former. The final option is to define
operator< for cell.

Tom
Jul 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.