I'm in the midst of porting an old VS6 project to VS2008/2010 and have hit a compilation problem associated with a template class. I would appreciate any comments or suggestions on reaching a resolution to the problem!
Regards
Rch83199
The error is as follows :-
1>DicomDataElement.cpp
1>c:\development\pacs\pacs\pacs\dicomdataelement.cpp(478) : error C2664: 'CStringDicomDataValue::Set' : cannot convert parameter 1 from 'CSingleDataElement<T>::data_type' to 'const char *'
1> with
1> [
1> T=const char *
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1> c:\development\pacs\pacs\pacs\dicomdataelement.cpp(465) : while compiling class template member function 'bool CSingleDataElement<T>::Add(const CSingleDataElement<T>::data_type &)'
1> with
1> [
1> T=CAgeStringDicomData
1> ]
1> c:\development\pacs\pacs\pacs\dicomdataelement.cpp(38) : see reference to class template instantiation 'CSingleDataElement<T>' being compiled
1> with
1> [
1> T=CAgeStringDicomData
1> ]
Last edited by rch83199; April 6th, 2011 at 03:25 PM.
Reason: change attachment
I haven't looked at the code, but make sure you have "typename" specified everywhere necessary. You'll probably need it in front of any reference to data_type.
I'm in the midst of porting an old VS6 project to VS2008/2010 and have hit a compilation problem associated with a template class. I would appreciate any comments or suggestions on reaching a resolution to the problem!
It would help if you tell us what to compile to duplicate the error. To do that, you should put the entire code in one source file, not several files, or create a real project using those files.
For example, where is this file?
Code:
DicomDataElement.cpp
We need this or a minimal example, to reproduce the compiler error.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 6th, 2011 at 10:05 AM.
I haven't looked at the code, but make sure you have "typename" specified everywhere necessary. You'll probably need it in front of any reference to data_type.
Thanks for the response. I've tried using typename but it doesn't seem to have the desired effect but I'll try again. The snip.c file in the attachment should read as below.
It would help if you tell us what to compile to duplicate the error. To do that, you should put the entire code in one source file, not several files, or create a real project using those files.
For example, where is this file?
Code:
DicomDataElement.cpp
We need this or a minimal example, to reproduce the compiler error.
after a quick look, the problem seems that the "typedef char * const data_type;" in the CStringDicomDataValue definition should be "typedef const char * data_type;" instead.
Thanks for the response. I've tried using typename
No. What Lindley was referring to is that templates require you to specify typename when you ambiguously use the scope resolution operator within a template:
The reason you need typename is that the compiler believes that "iterator" is a static member variable without the typename specifier. When you specify "typename", then you are telling the compiler that "iterator" is a type and not a static member variable.
Thanks for all the suggestions so far, much appreciated. I’ve tried applying them but to no avail.
As suggested I've created a project which exhibits the previously described error.
Set is already a templated function. However for some reason, you want to cast "value" to a concrete type. Depending on what data_type is, there is no guarantee this cast will work, and there you have it -- the cast to const char* fails.
Code:
typedef struct
{
unsigned short group;
unsigned short element;
};
From what I gather, this is what value probably is, and there is no way you can cast a const char* to that struct.
ANSI C++ is a type-safe language, and since it has been standardized since 1998, all of these lax and/or illegal casts and code that VC 6.0 accepted is no longer acceptable in ANSI C++.
But the flaw in your code is that you're taking a templated function (Set) and assuming you can cast whatever value_type is to const char*, instead of making the call "flow through" without any casts done. That is the bottom line as far as what's wrong with the code.
You need to revisit what has been coded and recode it to follow proper ANSI C++ before Visual Studio accepts it.
Bookmarks