error LNK2022: metadata operation failed - using Templates?
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