-
April 6th, 2011, 09:45 AM
#1
Vs6 to VS2008 Template Problem
Hi
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>:ata_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>:ata_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
-
April 6th, 2011, 09:50 AM
#2
Re: Vs6 to VS2008 Template Problem
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.
-
April 6th, 2011, 10:03 AM
#3
Re: Vs6 to VS2008 Template Problem
Originally Posted by rch83199
Hi
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.
-
April 6th, 2011, 10:09 AM
#4
Re: Vs6 to VS2008 Template Problem
Originally Posted by Lindley
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.
Template <class T>
bool CSingleDataElement<typename T>::Add(const data_type& value)
{
T* ptr;
if (m_value.GetSize() == m_max_values)
{
return false;
}
if ((ptr = new T) == NULL)
{
AfxThrowMemoryException();
}
if (ptr->Set((const char *)value) == false) // Creates error error C2664:
{
delete ptr;
return false;
}
m_value.Add(ptr);
m_present = true;
return true;
}
-
April 6th, 2011, 10:11 AM
#5
Re: Vs6 to VS2008 Template Problem
Originally Posted by Paul McKenzie
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
OK, I'll do that.
-
April 6th, 2011, 10:17 AM
#6
Re: Vs6 to VS2008 Template Problem
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.
-
April 6th, 2011, 10:20 AM
#7
Re: Vs6 to VS2008 Template Problem
Originally Posted by rch83199
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:
Code:
#include <vector>
template <class T>
{
std::vector<T> vecT;
std::vector<T>::iterator it; // error
};
This code will compile with VC 6. It will not compile with Visual Studio 2008, or any other ANSI compliant C++ compiler. The fix is this:
Code:
#include <vector>
template <class T>
{
std::vector<T> vecT;
typename std::vector<T>::iterator it; // ok
};
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.
Regards,
Paul McKenzie
-
April 6th, 2011, 01:54 PM
#8
Re: Vs6 to VS2008 Template Problem
Originally Posted by rch83199
OK, I'll do that.
Hello,
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.
TIAR
Rch83199
-
April 6th, 2011, 02:27 PM
#9
Re: Vs6 to VS2008 Template Problem
Can you explain what you're trying to do here?
Code:
template <class T>
bool CSingleDataElement<T>::Add(const data_type& value)
{
//...
if (ptr->Set((const char *)value) == false)
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.
Regards,
Paul McKenzie
-
April 6th, 2011, 02:36 PM
#10
Re: Vs6 to VS2008 Template Problem
Thanks for the review, will do as suggested.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|