CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2011
    Posts
    6

    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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vs6 to VS2008 Template Problem

    Quote Originally Posted by rch83199 View Post
    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.

  4. #4
    Join Date
    Apr 2011
    Posts
    6

    Re: Vs6 to VS2008 Template Problem

    Quote Originally Posted by Lindley View Post
    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;
    }

  5. #5
    Join Date
    Apr 2011
    Posts
    6

    Re: Vs6 to VS2008 Template Problem

    Quote Originally Posted by Paul McKenzie View Post
    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.

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    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.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Vs6 to VS2008 Template Problem

    Quote Originally Posted by rch83199 View Post
    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

  8. #8
    Join Date
    Apr 2011
    Posts
    6

    Angry Re: Vs6 to VS2008 Template Problem

    Quote Originally Posted by rch83199 View Post
    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

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    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

  10. #10
    Join Date
    Apr 2011
    Posts
    6

    Red face 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
  •  





Click Here to Expand Forum to Full Width

Featured