سلام
من یه مشکلی پیدا کردم تو visual studio ، مشکلم اینه که وقتی از template استفاده میکنم، و constructor رو که تو یه فایل جدا استفاده میکنم Error LNK 2019(unresolved external )l رو میده، کدی که من روش تست کردم اینجوریه:
// test1.h
#ifndef TEST1_H
#define TEST1_H
namespace TEST
{
template< class T >
class MyClass1;
template< class T >
class MyClass2
{
friend class MyClass1<T>;
private:
MyClass2<T> *x;
};
template< class T >
class MyClass1
{
public:
MyClass1();//{ a = NULL; }
private:
MyClass2<T> *a;
};
}
#endif
cpp.*
// test1.cpp
#include "stdafx.h"
#include "test1.h"
namespace TEST
{
template< typename T >
MyClass1<T>::MyClass1(){ a = NULL; }
}
تو MSDN که نگاه میکردم، نوشته بود اگه از template استفاده میکنید، member function ها رو نباید اکسپورت کنید، البته اول Error LNK 2001( unresolved external)l میداد ، بعد که من رفتم msdn رو دیدم اینا رو نوشته بود:
واسه LNK2001:
The definition of member template is outside the class. Visual C++ has a limitation in which member templates must be fully defined within the enclosing class. See KB article Q239436 for more information about LNK2001 and member templates.
بعد که رفتم تو more info، اینارو زده بود:
The compiler does not support the use of the "export" keyword as specified in the C++ standard below:
Section 14
Para# 7: Declaring a class template exported is equivalent to declaring all of its non-inline function members, static data members, member classes, member class templates and non-inline function member templates which are defined in that translation unit exported.
Para# 8: Templates defined in an unnamed namespace shall not be exported. A template shall be exported only once in a program. An implementation is not required to diagnose a violation of this rule. A non-exported template that is neither explicitly specialized nor explicitly instantiated must be defined in every translation unit in which it is implicitly instantiated (temp.inst) or explicitly instantiated (temp.explicit); no diagnostic is required. An exported template need only be declared (and not necessarily defined) in a translation unit in which it is instantiated. A template function declared both exported and inline is just inline and not exported.
من، وقتی تو همون فایل constructor رو تعریف کنم مشکلی ندارم، ولی اگه مثلا توی یه cpp.* دیگه تعریف کنم مشکل داشتم، بعد اونجایی رو که زیرش خط کشیدم دیدم، گفتم شاید اگه تو یه namespace تعریف کنم مشکلی ندارم، ولی بعد از اون error LNK2019 رو داد، بعد help اون رو که نگاه کردم، یه جورایی تو مایه های همون بود این رو نوشته بود:
LNK2019 can also be generated as a result of conformance work that was done for Visual Studio .NET 2003: template friends and specialization. In Visual Studio .NET 2003, a declaration of a friend function with the same name as a function template does not refer to that function template unless template arguments are explicitly specified in the friend declaration.
If you do not specify template arguments, the friend declaration declares a non-template function.
ولی من friend function استفاده نکردم که friend class استفاده کرم :|
البته من visual studio 2008 استفاده میکنم نه 2003 که اینجا زده، ولی همین مشکلا باز هست
کلا نمیدونم با این مشکل چی کار کنم، میشه همه رو یه جا تعریف کرد، ولی مثلا اگه بخوام موقع استفاده از template ها، implementation رو از interface جدا کنم، این اروره نمیذاره، اگه شما در این رابطه چیزی میدونین و کمکم کنید ممنون میشم.
ببخشید طولانی بود :|