472,119 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Problem accessing variable in inherited template class

I'm trying to set up an inheritance tree that also uses templates. This is an attempt to simplify some previous code that was filled with redundancies. I'm working with g++ 3.4.6. The code is split over four files: two headers and two implamentation files, but uses the include trick mentioned on another thread to connect the header and implamentation files together. The base class header is

Expand|Select|Wrap|Line Numbers
  1. #ifndef _BASEDATA_H_
  2. #define _BASEDATA_H_
  3.  
  4. #include <vector>
  5. #include <string>
  6. #include <fstream>
  7.  
  8. template <typename _Type> class baseData
  9. {
  10. public:
  11.   baseData();
  12.   virtual ~baseData() {}
  13.  
  14. public:
  15.  
  16.   /**
  17.    * get human readable status
  18.    */
  19.    std::string status();
  20.  
  21.   /**
  22.    * access data vector
  23.    */
  24.   std::vector<_Type>* access() {return &m_data;}
  25.  
  26.   /**
  27.    * reset data vector
  28.    */
  29.    void reset();
  30.  
  31.   /**
  32.    * overload [] operator
  33.    */
  34.    _Type& operator[](long i) {return m_data[i];}
  35.  
  36. protected:
  37.   std::vector<_Type> m_data;
  38.   unsigned int       m_status;
  39. };
  40.  
  41. #ifndef USE_EXPORT_KEYWORD
  42. #include "basedata.cpp"
  43. #endif
  44.  
  45. #endif
  46.  
while the corresponding cpp file is

Expand|Select|Wrap|Line Numbers
  1. #ifndef USE_EXPORT_KEYWORD
  2. #define export
  3. #endif
  4.  
  5. export
  6. template <typename _Type> baseData<_Type>::baseData()
  7. {
  8.   m_status = 0;
  9. }
  10.  
  11. export
  12. template <typename _Type> std::string baseData<_Type>::status()
  13. {
  14.   std::string reason;
  15.   switch (m_status)
  16.   {
  17.     case 1:
  18.       reason = "Could not open input file";
  19.       break;
  20.     case 2:
  21.       reason = "Could not open output file";
  22.       break;
  23.     case 3:
  24.       reason = "Requested data size larger than maximum allocatable size";
  25.       break;
  26.     case 4:
  27.       reason = "No raw data to synchronize to";
  28.       break;
  29.     default:
  30.       reason = "OK";
  31.       break;
  32.   };
  33.   return reason;
  34. }
  35.  
  36. export
  37. template <typename _Type> void baseData<_Type>::reset()
  38. {
  39.   m_data.resize(0);
  40. }
  41.  
  42. #ifndef USE_EXPORT_KEYWORD
  43. #undef export
  44. #endif
  45.  
Similarly, the inheriting class' header is

Expand|Select|Wrap|Line Numbers
  1. #ifndef _MATRIXDATA_H_
  2. #define _MATRIXDATA_H_
  3.  
  4. #include <vector>
  5. #include <string>
  6.  
  7. #include "basedata.h"
  8.  
  9. template <typename _Type> class matrixData : public baseData< std::vector<_Type> >
  10. {
  11.  
  12. public:
  13.   matrixData();
  14.   ~matrixData() {}
  15.  
  16. public:
  17.   /**
  18.    * Initialize data set to zeros
  19.    */
  20.    int initialize(int nRows, int nCols, int row_0 = 0, int col_0 = 0);
  21.  
  22.   /**
  23.    * access data size
  24.    */
  25.    long rows() {return baseData<_Type>::access()->size();}
  26.    long rows(long newRows);
  27.  
  28.    long cols() {return (*(baseData<_Type>::access()))[0].size();}
  29.    long cols(long newCols);
  30.  
  31.    long size() {return rows()*cols();}
  32.    long size(long newSize);
  33.  
  34. private:
  35.  
  36.   int       m_colMax;
  37.   int       m_colMin;
  38.   int       m_rowMax;
  39.   int       m_rowMin;
  40. };
  41.  
  42. #ifndef USE_EXPORT_KEYWORD
  43. #include "matrixdata.cpp"
  44. #endif
  45.  
  46. #endif
  47.  
while the associated cpp file is

Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. #ifndef USE_EXPORT_KEYWORD
  6. #define export
  7. #endif
  8.  
  9. export
  10. template <typename _Type> matrixData<_Type>::matrixData()
  11. {
  12.   m_colMax = m_colMin = 0;
  13.   m_rowMax = m_rowMin = 0;
  14. }
  15.  
  16. export
  17. template <typename _Type> int matrixData<_Type>::initialize(int nRows, int nCols, int row_0, int col_0)
  18. {
  19.   int i, j;
  20.   int dataSize;
  21.   unsigned int highMask = 0xFFFF0000;
  22.   unsigned int lowMask = 0x0000FFFF;
  23.  
  24.   m_rowMax = nRows;
  25.   m_colMax = nCols;
  26.   m_rowMin = row_0;
  27.   m_colMin = col_0;
  28.  
  29.   /*
  30.      Note that in matrix[i][j], i denotes the number of rows in the column, while j denotes the number of columns
  31.      in the matrix. Therefore, want to deal with the datasize so that the high part is the number of rows, while
  32.      the low part is the number of columns.
  33.   */
  34.  
  35.   dataSize = (((m_rowMax - m_rowMin) << 16) & highMask) + ((m_colMax - m_colMin) & lowMask);
  36.   if (size(dataSize) < 0)
  37.   {
  38.     m_status = 3;
  39.     return -1;
  40.   }
  41.  
  42.   _Type value = (_Type) NULL;
  43.   for (i = m_rowMin;i < m_rowMax;i++)
  44.     for (j = m_colMin;j < m_colMax;j++)
  45.     {
  46.       if (m_data.begin()+i == m_data.end())
  47.       {
  48.         std::vector<_Type> newColumn;
  49.         m_data.push_back(newColumn);
  50.         if (m_data[i].begin()+j == m_data[i].end())
  51.           m_data[i].push_back(value);
  52.       }
  53.       else
  54.         m_data[i][j] = value;
  55.     }
  56.  
  57.   return size();
  58. }
  59.  
  60. export
  61. template <typename _Type> long matrixData<_Type>::size(long newSize)
  62. {
  63.   unsigned long highMask = 0xFFFF0000;
  64.   unsigned long lowMask = 0x0000FFFF;
  65.   unsigned long rowSize, columnSize;
  66.   unsigned long i;
  67.  
  68.   rowSize = (newSize & highMask) >> 16;
  69.   columnSize = newSize & lowMask;
  70.   if (rowSize > m_data.max_size())
  71.   {
  72.     m_status = 3;
  73.     return -1;
  74.   }
  75.   m_data.resize(rowSize);
  76.   for (i = 0;i < rowSize;i++)
  77.   {
  78.     if (columnSize > m_data[i].max_size())
  79.     {
  80.       m_status = 3;
  81.       return -1;
  82.     }
  83.     m_data[i].resize(columnSize);
  84.   }
  85.  
  86.   return size();
  87. }
  88.  
  89. export
  90. template <typename _Type> long matrixData<_Type>::rows(long newRows)
  91. {
  92.   if (newRows > m_data.max_size())
  93.   {
  94.     m_status = 3;
  95.     return -1;
  96.   }
  97.   m_data.resize(newRows);
  98.  
  99.   return m_data.size();
  100. }
  101.  
  102. export
  103. template <typename _Type> long matrixData<_Type>::cols(long newCols)
  104. {
  105.   if (newCols > m_data[0].max_size())
  106.   {
  107.     m_status = 3;
  108.     return -1;
  109.   }
  110.  
  111.   int i;
  112.   for (i = 0;i < m_data.size();i++)
  113.     m_data[i].resize(newCols);
  114.  
  115.   return m_data[0].size();
  116. }
  117.  
  118. #ifndef USE_EXPORT_KEYWORD
  119. #undef export
  120. #endif
  121.  
When I try to compile this, I get the following error message:

error: 'm_status' was not declared in this scope

Previously, I had a number of (nearly) identical classes that inherited from baseData, where the main difference was in what _Type was declared to be. In that case, I had no problem accessing m_status. While I'm fairly sure this indicates a template verses non-template difference, I don't see how to fix this so that I can reduce my redundant classes. Any help would be appreciated.

Thanks,
Dan
Jun 12 '08 #1
3 2912
Banfa
9,065 Expert Mod 8TB
On which line of code in which file do you get the error?
Jun 13 '08 #2
Line 38 of the matrixData class cpp code
Jun 13 '08 #3
RRick
463 Expert 256MB
I don't know if this will fix your error, but first you must combine the header and source code for each object into a single header file. Most compilers (including GNU and windows) can not deal with them separately.

If you simply append the two files together, remember to make each method inline.

This might solve the problem.
Jun 17 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

25 posts views Thread by John Harrison | last post: by
5 posts views Thread by Michael Bruschkewitz | last post: by
8 posts views Thread by David Crocker | last post: by
reply views Thread by 42 | last post: by
19 posts views Thread by jan.loucka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.