STL set (compilartion error) how to resolve this

T

temper3243

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,myeq> s;

s.insert(a);
return 0;
}
 
G

Geo

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,myeq> s;

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 ?
 
N

niklaus

Geo said:
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 _Alloc> class 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'
$
 
T

Tom Widmer

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,myeq> s;

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top