开发者

C++: Compilation Errors for Custom Coded Generic Container?

The following is based on the code that I posted on this thread. Aside from the obvious bugs, I get the following compilation errors? Any idea why?

The odd thing is that this only occurs for the template class. If I add another non template class to the same .h and .cpp file of the template class and try to instantiate just the non temp开发者_JS百科late class, no error is generated.

Scenario A:

I get the compilation error " error C2659: '=' : overloaded function as left operand " for the following code:

StdVector<int> a();
StdVector<int> b();
a = b;

Scenario B:

I get the compilation error " error C2664: '__thiscall StdVector::StdVector(const class StdVector &)' : cannot convert parameter 1 from 'class StdVector (__cdecl *)(void)' to 'const class StdVector &' " for the following code:

StdVector<int> a();
StdVector<int> b(a);

Scenario C:

I get the compilation error "error LNK2001: unresolved external symbol "public: __thiscall StdVector::~StdVector(void)" (??1?$StdVector@H@@QAE@XZ) " for the following code:

StdVector<int> a;


StdVector<int> a();
StdVector<int> b();

These are function declarations. Omit the parenthesis to declare a default-constructed instance.


You are declaring a and b to be functions returning StdVector. I assume that's not what you want. What does it mean to assign a function to a function? Maybe you meant:

StdVector<int> a;


As the previous answers have said, in scenarios A and B you are actually declaring functions and not instantiating object instances. This is known as "C++'s most vexing parse". See this question for more discussion.

It is hard to tell why scenario C doesn't work without seeing the actual source code for your class, but the linker is telling you that it is unable to find the destructor for StdVector. It says it is an "unresolved external symbol", which means that when it was compiling it didn't have the definition and it either assumed, or was explicitly told, that the definition is in some other file. When it came time to link all of the object files into a single executable, it couldn't find the object code for the destructor, and that is why you are getting that error. That doesn't give you an easy solution for the problem, but at least you have an idea where to start looking.


If I am not mistaken, templates cannot be exported. That is why I am getting the linker error in Scenario C.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜