C2664: Types in Templated Functions

Laurie Stearn 91 Reputation points
2023-02-08T13:00:28.27+00:00

In the header

struct ItemInfo
{
	ItemInfo();
	~ItemInfo();
};


struct ItemList
{
    tList<ItemInfo>      itemInfoList;
    tArray<ItemInfo *>   somethingElse;  
};


class Data
{
    ItemList		itemList;
    const ItemInfo* Ref(const char* modName);
}

Also in the header, there is a class containing a template Find function with a referential argument,- the rest is noise but included for illustration.

class Big
{
...
	template <class Op>
	T * Find(Op& op) const
	{
		const _Node* pCur = Head(); 

		bool bFound = false;
		while (pCur && !bFound)
		{
			if (!pCur->Item())
				pCur = pCur->Next();
			else
			{
				bFound = op.FinderFunction(pCur->Item());
				if (!bFound)
					pCur = pCur->Next();
			}
		}
		return (bFound && pCur) ? pCur->Item() : NULL;
	}
}

In the cpp there is:

class Finder{
	const char * string;
	{
	public:	Finder(const char * str) : string(str) { }

	bool FinderFunction(ItemInfo* itemInfo)
	{
		return _stricmp(itemInfo->name, string) == 0;
	}
};

The issue is with the Find function in the next block.

const ItemInfo * Data::Ref(const char * itemName)
{
	return itemList.itemInfoList.Find(Finder(itemName));
}

The error in VS2019 is:

: error C2664: 'T *tList<T>::Find<Finder>(Op &) const': cannot convert argument 1 from 'Finder' to 'Op &' 1> with 1> [ 1> T=itemInfo, 1> Op=Finder 1> ] 1> and 1> [ 1> Op=`Finder 1> ]

This code once compiled in earlier revisions of C++, it's easy to see why it won't now, but have no idea how to change it to make the conflicting types compatible.

Thanks

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,581 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,519 questions
{count} votes

Accepted answer
  1. Viorel 111.8K Reputation points
    2023-02-08T14:57:34.0033333+00:00

    Try adding const: T* Find( const Op& op ) const and probably bool FinderFunction( const ItemInfo * itemInfo ) const.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. khawlah Alshubati 10 Reputation points
    2023-02-08T13:13:52.79+00:00

    The error message is indicating that the argument being passed to the Find function is of type Finder, but the function is expecting an argument of type Op & (a reference to an Op object).

    To resolve this issue, you'll need to modify the definition of the Find function to accept a Finder object instead of an Op & object. This could be done by changing the function's signature from T *Find(Op &) const to T *Find(Finder) const.

    Additionally, it's also possible that you may need to modify the implementation of the Find function to work with a Finder object instead of an Op & object. For example, if the implementation was previously using the Op object to compare elements in a list, you may need to modify the implementation to use the Finder object instead

    1 person found this answer helpful.
    0 comments No comments

  2. khawlah Alshubati 10 Reputation points
    2023-02-08T13:14:44.5033333+00:00

    The error message is indicating that the argument being passed to the Find function is of type Finder, but the function is expecting an argument of type Op & (a reference to an Op object).

    To resolve this issue, you'll need to modify the definition of the Find function to accept a Finder object instead of an Op & object. This could be done by changing the function's signature from T *Find(Op &) const to T *Find(Finder) const.

    Additionally, it's also possible that you may need to modify the implementation of the Find function to work with a Finder object instead of an Op & object. For example, if the implementation was previously using the Op object to compare elements in a list, you may need to modify the implementation to use the Finder object instead

    0 comments No comments