Jump to content
  • Advertisement
Sign in to follow this  
Rattrap

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.

If you intended to correct an error in the post then please contact us.

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>
class CDoubleLinkListIterator
{
public:
	//  Other methods have been removed
	friend bool operator == (const CDoubleLinkListIterator<T>& lhs, const CDoubleLinkListIterator<T>& rhs);
};

template <typename T>
inline bool operator == (const CDoubleLinkListIterator<T>& lhs, const CDoubleLinkListIterator<T>& rhs)
{
	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>
class CDoubleLinkListIterator
{
public:
	//  Other methods have been removed
	friend bool operator == (const CDoubleLinkListIterator<T>& lhs, const CDoubleLinkListIterator<T>& rhs)
	{
		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? [edit] 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)
{
	CDoubleLinkList<int> llInt;

	CDoubleLinkListIterator<int> it = llInt.getFirst();
	it.add(0);
	CDoubleLinkListIterator<int> it2 = llInt.getLast();
	//  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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites

template <typename T>
class CDoubleLinkListIterator
{
public:
// Other methods have been removed
template<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 this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!