Sign in to follow this  

Friend function trouble

This topic is 4598 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
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

This topic is 4598 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this