Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VS.NET - non-type template classes with FLOAT

Posted on 2003-11-12
3
Medium Priority
?
277 Views
Last Modified: 2010-04-02
Hi,

I am having a problem compiling one of my projects (VS.NET, project imported from VC6), because there is a problem with how VS.NET handles FLOATS in non-type template classes. I have checked out MSDN : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/C2993.asp BUT no joy as it says if it is a class template which mine is, there is no easy workaround.

So to my question, does anyone know of a way to solve this ? I am thinking that i will have to write a new class to include the float parameters only, and take them out of the template class. Is this the correct way ? For your info, below is my template class.

template<class T, TypeID_gem_t TypeID, T Invalid>
class CGEM_ArrayItem : public CGEM_DataItem
{      
      LPCSTR      m_pszName;
      T*            m_pVal;
      size_t      m_cElemCount;
      TypeID_gem_t m_type;

 public:
       CGEM_ArrayItem(const TGEM_DATA_DEF& DataDef)  
             : m_cElemCount(DataDef.ItemCount),

              m_type(TypeID)
      {
            m_pVal = new T[m_cElemCount];

            if(m_pVal == 0)
                  m_cElemCount = 0;
            else
                  for(int i = 0; i < m_cElemCount; i++)
                              m_pVal[i] = Invalid;

            m_pszName = ::_strdup(DataDef.pszItemName);

            TGEM_DATA d;
            d.val = DataDef.IniVal;
            d.ParType = DataDef.ParType;

            Set(d);
      }

      ~CGEM_ArrayItem()
      {
            delete[] m_pVal;
            
            ::free(m_pszName);
      }


      static SAFEARRAY* GetSafeArray(const TGEM_DATA& data)
      {
            switch(data.ParType)
            {
                  case GEM_I8_ARRAY:      return data.val.I8_array;
                  case GEM_I16_ARRAY:      return data.val.I16_array;                  
                  case GEM_I32_ARRAY:      return data.val.I32_array;
                  case GEM_I64_ARRAY:      return data.val.I64_array;

                  case GEM_SF_ARRAY:      return data.val.SF_array;      //singlefloat      
                  case GEM_DF_ARRAY:      return data.val.DF_array;      //doublefloat            

                  case GEM_U8_ARRAY:      return data.val.U8_array;                  
                  case GEM_U16_ARRAY:      return data.val.U16_array;                  
                  case GEM_U32_ARRAY:      return data.val.U32_array;                  
                  case GEM_U64_ARRAY:      return data.val.U64_array;                  
                  case GEM_B_ARRAY:      return data.val.B_array;                  

                  default:
                        ATLASSERT(false);
                        return NULL;
            }            
      }


      HRESULT Set(const TGEM_DATA& data)
      {
            HRESULT hr = E_INVALIDARG;
            SAFEARRAY *psa = GetSafeArray(data);

            if(psa != NULL)            
            {
                  ULONG size = sizeof T;
                  ATLASSERT(SafeArrayGetElemsize(psa) == sizeof T);
                  
                  void *pSrc;
                  hr = SafeArrayAccessData(psa, &pSrc);

                  if(SUCCEEDED(hr))
                  {
                        long plUbound;

                        hr = SafeArrayGetUBound(psa, 1, &plUbound);

                        if(SUCCEEDED(hr) && ++plUbound == m_cElemCount)
                              memcpy(m_pVal, pSrc, plUbound * sizeof T);
                        else
                              hr = E_INVALIDARG;

                        SafeArrayUnaccessData(psa);
                  }
            }
                  
            return hr;
      }
      


      TypeID_gem_t      GetType() { return m_type; }
      Data_gem_t*            GetData() { return m_pVal; }
      LPCSTR                  GetName() { return m_pszName; }
      Integer32_gem_t GetElementCount() { return m_cElemCount; }

      void Invalidate()
      {
            if(m_pVal)
            {                  
                  for(int i = 0; i < m_cElemCount; i++)
                  {
                        m_pVal[i] = 0;
                  }
            }
      }
};

Any help would be most appreciated in this porting exercise from VS6 to VS.NET C++.

Kind Regards
Jon.




0
Comment
Question by:upliftin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 800 total points
ID: 9734842
I think you have to pass it as a constructor parameter :-(

e.g.
--------8<--------
#include <iostream>

template<class T> class TC {
        const T initialiser;
public:
        TC(T initialiser) : initialiser(initialiser) {}
        void print() const { std::cout << "Initialiser is " << initialiser << '\n';}
};

int main()
{
        TC<double> tc(123.456);
        tc.print();
}
--------8<--------
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249035
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: rstaveley {http:#9734842}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

730 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