I've seen several previous posts related to LNK2022 errors using
templates, but I have not seen a resolution (one other person said the
error goes away if he does not make his class a template, but that's
not a preferred solution for me, as it wasn't for him) .

I have a template LinkedList class that I can use successfully within
the .Net form.h file, but if I try to access it from within another
class (only in that class, no longer in the form) it compiles fine but
then I get two link errors:

City.obj : error LNK2022: metadata operation failed (80131187) :
Inconsistent method declarations in duplicated types (types: node;
methods: .ctor): (0x06000003).
City.obj : error LNK2022: metadata operation failed (801311D6) :
Differing number of methods in duplicated types (node): (0x02000004).

I've commented out most of the lines of code so I know that this error
occurs when I include a method call from with the City object to the
linked list method insertAtHead.

The linkedlist class (reduced for this example) is:

#pragma once

template <typename Item>
ref class LinkedList
{
private:
ref class node
{
public:
node() { data = Item(); link=nullptr; }
node(const Item% init_data, node ^init_link)
{ data = init_data; link = init_link; }
Item data;
node ^link;
};
public:
typedef node ^NodePtr;
LinkedList(void);
void insertAtHead(const value_type% entry);
private:
NodePtr head;
};
and in the cpp file:
template <typename Item>
LinkedList<Item>::LinkedList(void)
{
head = nullptr;
}
template <typename Item>
void LinkedList<Item>::insertAtHead(const Item% entry)
{
head = gcnew node(entry, head);
}

In the City class, I have:
#pragma once
#include "LinkedList.cpp"
ref class City {
public:
City(System::String^ cityName);
void addDest(City^ dest);
private:
System::String^ name;
LinkedList<City^> destList;
};

and in the City.cpp file:
void City::addDest(City^ dest) {
destList.insertAtHead(dest);
}

If I comment out destList.insertAtHead(dest); I no longer get the
linker errors.

As stated above, the LinkedList files (unmodified) work if I put the
declaration of the linked list and calls to insertAtHead in my Form1.h
file. Why should it make a difference if they are in my City class
instead?

Any suggestions would be greatly appreciated!

cyndi

Re: error LNK2022: metadata operation failed - using Templates? by Vinzenz

Vinzenz
Wed Jul 19 07:44:56 CDT 2006

> As stated above, the LinkedList files (unmodified) work if I put the
> declaration of the linked list and calls to insertAtHead in my Form1.h
> file. Why should it make a difference if they are in my City class
> instead?
>
> Any suggestions would be greatly appreciated!
>

Hi cyndi,

You cannot put template implementations in a seperated files without
including the seperated file to the header.
The result if you're doing something like this, you have expirienced ;)

So do it in this way:
// LinkedList.h
template <typename Item>
ref class LinkedList
{
private:
ref class node
{
public:
node() { data = Item(); link=nullptr; }
node(const Item% init_data, node ^init_link)
{ data = init_data; link = init_link; }
Item data;
node ^link;
};
public:
typedef node ^NodePtr;
LinkedList(void)
{
head = nullptr;
}
void insertAtHead(const Item % entry)
{
head = gcnew node(entry, head);
}
private:
NodePtr head;
};


And drop the LinkedList.cpp

The rest can be keeped.

And you'll see it will work ;)

Regards,
Vinzenz

Re: error LNK2022: metadata operation failed - using Templates? by cyndi

cyndi
Fri Jul 21 14:25:17 CDT 2006

Hi Vinzenz,

Thanks for the suggestion. I tried putting all the linkedlist method
definitions in the linkedlist header and even put the linkedlist header
directly in the city class. Unfortunately the linker gives the same
messages. I'm not sure what else to try at this point.

Cyndi


Re: error LNK2022: metadata operation failed - using Templates? by Tom

Tom
Mon Jul 24 04:45:02 CDT 2006

cyndi wrote:
> Hi Vinzenz,
>
> Thanks for the suggestion. I tried putting all the linkedlist method
> definitions in the linkedlist header and even put the linkedlist header
> directly in the city class. Unfortunately the linker gives the same
> messages. I'm not sure what else to try at this point.

Do you have another version of LinkedList lying around? That might
produce the errors you're seeing.