[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Template troubles

Posted on 2006-05-24
7
Medium Priority
?
339 Views
Last Modified: 2013-11-20
I want to write a template function that can accept any specialization of CArray and operate on its elements.  I have tried several function declarations, but none work:

1. template<class ARRAY> void Foo(ARRAY &array);

Produces: error C2893: Failed to specialize function template ... parameter: CArray<XTYPE, XTYPE&> &

2. template<class TYPE> void Foo(CArray<TYPE, TYPE&> &array);

Produces: fatal error C1001: INTERNAL COMPILER ERROR

3. void Foo(CArray<BASECLASS, BASECLASS&> &array); //Attempting to exploit the fact that all needed array types have a common superclass

Compiler refuses the conversion CArray<XTYPE, XTYPE&> to CArray<BASECLASS, BASECLASS&>

I use Visual C++ 6.0.  Can anyone suggest a correction to any of these approaches, a simpler way, or a workaround to the ICE?
0
Comment
Question by:Alkali_Guy
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 880 total points
ID: 16758461
if CArray takes a class parameter like this:

template <class T> class CArray {....};

then you can extend it like this:

template <class T> class MyCArray : public CArray<T>
{
  bool MyFunction(const T&);  //etc etc
};


-MAHESH
0
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16758501
or
template<class T> void SomeArrayFcn( CArray< T, T>& array ) {…};
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 16761106
With your first suggestion, the linker just spews errors at me, even though I placed #includes in the appropriate places:

E.g.
error LNK2001: unresolved external symbol "public: __thiscall CIDArray<struct CATEGORY,struct CATEGORY &>::CIDArray<struct CATEGORY,struct CATEGORY &>(void)" (??0?$CIDArray@UCATEGORY@@AAU1@@@QAE@XZ)

My class declaration for the above is:
template<class TYPE, class ARG_TYPE> class CIDArray : public CArray<TYPE, ARG_TYPE> {...}

My function definition for the ctor is:
template<class TYPE, class ARG_TYPE> CIDArray<TYPE, ARG_TYPE>::CIDArray() {...}

With your second suggestion, the compiler returns that T is 'ambiguous,' being possibly either XTYPE or XTYPE&.  When I change <T, T> to <T, T&>, the compiler gets ICE'd.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:jkr
ID: 16761860
How are you using that?

#include <AFXTEMPL.H>

template<class TYPE> void Foo(CArray<TYPE, TYPE&> &array) {

//...
}

void main () {

CArray<int,int&> test;

Foo<int>(test);

}

compiles find with /MT being set.
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 16763057
I'm in a strange spot, since I've written template functions taking the CList<> template as a parameter and encountered no problems.  Could it be a problem that the structures I use inherit some of their elements from a base class?  Perhaps I should try just separating the two into different structures.

The linker errors arise from extending CArray<>.  I'll check for misplaced #includes.
0
 
LVL 86

Expert Comment

by:jkr
ID: 16763248
BTW, if you get an "Internal Compiler Error", a "Rebuild All" usually fixes that. If not, even adding some unrelated dummy code around the problematic section (I am not joking here, even empty lines can help) could fix the problem also.
0
 
LVL 5

Author Comment

by:Alkali_Guy
ID: 16765395
OK, I worked around the internal compiler error by using mahesh1402's first suggestion of subclassing CArray.  The linker errors happened because I got fouled up in the mess described here:

http://www.comeaucomputing.com/techtalk/templates/#whylinkerror
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question