friend function, unresolved external symbol

I got 2 classes Point and Rect, all in one header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template<typename T>
class Point
{
private:
    T x;
    T y;
public:
...//ctor's & standard operators
};

template<typename T>
class Rect
{
private:
    Point<T> ul;//as upper left
    Point<T> lr;//as lower right
public:
...//ctor's & standard operators
...
    friend bool IsInsideRect(const Point<T>& p, const Rect<T>& r);
};

template<typename T>
bool IsInsideRect(const Point<T>& p, const Rect<T>& r)
{
        // i have implemented >, <, >=, <= operators for Point
	return ((p > r.ul) && (p < r.lr));
}


test in main:
1
2
3
4
5
6
7
8
9
...
Point<LONG> pt(5, 5);
Rect<LONG> rc(Point<LONG>(0, 0), Point<LONG>(20, 20));

if(IsInsideRect(pt, rc))
{
    MessageBox(0, "POINT IS INSIDE", "MESSAGE", 0);
}
...


Output error:

error LNK2019: unresolved external symbol "bool __cdecl IsInsideRect(class Point<long> const &,class Rect<long> const &)" (?IsInsideRect@@YA_NABV?$Point@J@@ABV?$Rect@J@@@Z) referenced in function _WinMain@16

how do i resolve this issue?
If it will be easy for you to help me i could post all relevant/working code to compile yourself?
closed account (yUq2Nwbp)
savavampir and what you think what type should be T in template function IsInsideRect??..I haven't compiler in this computer but i think in this case you must mention type like for example this IsInsideRect<type>()......
1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
class Rect
{
private:
    Point<T> ul;//as upper left
    Point<T> lr;//as lower right
public:
...//ctor's & standard operators
...
    friend bool IsInsideRect(const Point<T>& p, const Rect<T>& r);
};


That declaration of IsInsideRect above does not make it a template function - so this definition

1
2
3
4
5
6
template<typename T>
bool IsInsideRect(const Point<T>& p, const Rect<T>& r)
{
        // i have implemented >, <, >=, <= operators for Point
    return ((p > r.ul) && (p < r.lr));
}



will not match

EDIT:
So bascially you need to make the friend declaration a template.
Last edited on
Still can't resolve this, this i have tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<typename T>
class Rect
{
...
	template<typename U>
	friend bool IsInsideRect(const Point<U>& p, const Rect<U>& r);
};

template<typename U>
bool IsInsideRect(const Point<U>& p, const Rect<U>& r)
{
	return ((p > r.ul) && (p < r.lr));
}
...
//main.cpp
Point<LONG> pt(5, 5);
Rect<LONG> rc(Point<LONG>(0, 0), Point<LONG>(20, 20));

if(IsInsideRect<LONG>(pt, rc))
{
    MessageBox(0, "POINT IS INSIDE", "MESSAGE", 0);
}


and still same error. Can't see what's wrong???
Last edited on
Here is example code (i just made do with default constructors - to make things easier to see)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
template<typename T>
class Point
{
    private:
    T x;
    T y;
    
    public:
    bool operator > (const Point& right) const //**note const**
    {
        //just return true - for test purposes
        return true;
     }
     
    bool operator < (const Point& right) const //**note const**
    {
        //just return true - for test purposes
        return true;
    }
};


template<typename T>
class Rect
{
private:
    Point<T> ul;//as upper left
    Point<T> lr;//as lower right
    
    template <typename U>
    friend bool IsInsideRect(const Point<U>& p, const Rect<U>& r);
};


template<typename U>
bool IsInsideRect(const Point<U>& p, const Rect<U>& r)
{
        
    return ((p > r.ul) && (p < r.lr));
}



int main()
{
    Point<LONG> pt;
    Rect<LONG> rc;

    if(IsInsideRect(pt, rc))
    {
        cout << "POINT IS INSIDE", "MESSAGE";
    }

}
Thanks, that works. The difference that i had in my code is that my Point (>, <, <=...operators) was declared as friend's and definition was in cpp file. Now when i bring it in class body without change to IsInsideRect function it works.
Topic archived. No new replies allowed.