# Friend function trouble

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This is probably another one of those I'm just missing something really obvious questions but I continuely get link errors if I have the code as the following
template <typename T>
{
public:
//  Other methods have been removed
};

template <typename T>
{
if(&lhs == &rhs)
{
return true;
}

if((lhs.m_pNode == 0) || (rhs.m_pNode == 0))
{
return false;
}

return ((*(lhs.m_pNode)) == (*(rhs.m_pNode)));
}


but it does compile if I do this
template <typename T>
{
public:
//  Other methods have been removed
{
if(&lhs == &rhs)
{
return true;
}

if((lhs.m_pNode == 0) || (rhs.m_pNode == 0))
{
return false;
}

return ((*(lhs.m_pNode)) == (*(rhs.m_pNode)));
}
};


I will note that I am using Visual Studio 2005 Beta 2 (not express version). Any ideas?  The link error is as follows error LNK2019: unresolved external symbol "bool __cdecl operator==(class CDoubleLinkListIterator<int> const &,class CDoubleLinkListIterator<int> const &)" (??8@YA_NABV?\$CDoubleLinkListIterator@H@@0@Z) referenced in function _main main.obj
int main(void)
{

//  You can assume that up through this works
//  And that .getFirst() and .getLast() return an iterator that point
//  to the first and last node in the link list (respetively).

if(it == it2)
{
std::cout << "They match" << std::endl;
}

return 0;
}



##### Share on other sites
Visual studio is kind of funny about inline functions. If you want a function to be inline, it has to defined *inside* of the class, not out side of it. Otherwise, you will get link errors.

##### Share on other sites
I removed the inline for the external defintion, and it still does it. As far as I know inlined function need to be in the header, not neccessarily in the class defintion. I have other functions that are actual class methods that are defined outside (aka inline CDoubleLinkListNode::CDoubleLinkListNode() ) that work just fine.

##### Share on other sites
template <typename T>class CDoubleLinkListIterator{public:	//  Other methods have been removedtemplate<typename T1>	friend bool operator == (const CDoubleLinkListIterator<T1>& lhs, const CDoubleLinkListIterator<T1>& rhs);};

It will think the function is already defined somewhere otherwise, andwont instantiate the template.

##### Share on other sites
OK. So is this unique to templates then?

Since the way I did it is the way basically how every book I have ever seen friends does it, but they aren't using templates in their example.

In what way?

##### Share on other sites
This code is right out of the msdn documentation

class Point{    friend void ChangePrivate( Point & );public:    Point( void ) : m_i(0) {}    void PrintPrivate( void ){cout << m_i << endl; }private:    int m_i;};void ChangePrivate ( Point &i ) { i.m_i++; }

With the execption of mine using templates, is exactly the same. So is the behavior you discribed unique to templates?

Yep.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002274
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!