0

Given the following prerequisits:

shared.h

struct A {
    int x;
    int y;
}

typedef set<A> setOfA;

implementation1.cpp

#include "shared.h"
void Implementation1::someFunction()
{
    //...
    setOfA setinstance;
    //...
    Implementation2* i = new Implementation2();
    i->functionF(setinstance);
}

implementation2.h

#include "shared.h"
void Implementation2::functionF(setOfA&);

EDIT: now this should be clearer...

I want to pass a setOfA to another function of a different class - everything is compiling just fine. I get the following linker problem:

undefined reference to 'implementation::functionF(std::set<A, std::less<A>, std::allocator<A> >&)'

Just to get it right - the implementation could not be found, right? This cannot be a typedef problem, because everything compiles just fine... What am I missing here?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Eric
  • 1,594
  • 1
  • 15
  • 29

3 Answers3

5

The linker is unable to find the definition of void functionF(setOfA&);

Somewhere, you need:

void Implementation2::functionF(setOfA&) {
    ...
}

Just to elaborate on this slightly, shouldn't you have an implementation2.cpp file that implements the above code?

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
2

You didn't define a comparator for A, so a set of them can't exist.

Define a function:

bool operator<(A& const lhs, A& const rhs)

and ensure that it implements a strict weak ordering; in this case, probably:

bool operator<(A& const lhs, A& const rhs) {
    if (lhs.x != rhs.x)
       return (lhs.x < rhs.x);

    return (lhs.y < rhs.y);
}
Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

My major problem was that I was working in the wrong namespace. I agree to close this thread, because it's probably just helping me with my specific problem - on the other hand the hint to implement the operator< gave me the clue. For the sake of completeness, here's my operator< implementation.

bool operator<(const A& e) const {
if (x != e.x)
  return x < e.x;
else
  return y < e.y;
}
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
Eric
  • 1,594
  • 1
  • 15
  • 29