473,406 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 3059
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

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

Similar topics

25
by: John Harrison | last post by:
This code fails to compile on Comeau C++ and VC++ 7.1 (with language extensions disabled) template <class T> struct B { T b; }; template <class T>
5
by: Michael Bruschkewitz | last post by:
Hello, I'm somehow annoyed. This works: using namespace std; // <-- Don't like this std::basic_filebuf<char> fb; fb.open("out.txt",std::ios_base::out); std::basic_ofstream<char> ofs;...
8
by: David Crocker | last post by:
The following won't compile under Comeau in strict mode, nor under MS Whidbey beta in ANSI mode. Can anyone tell me why, and which sections of the ISO standard apply? The error messages from Comeau...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
8
by: Altman | last post by:
I have a class that I want many others inherited off of. I have created a variable in the parent class that I want the inherited classes to set. But I do not want to set the variable in the...
4
by: hweekuan | last post by:
Hi, I got an error with gnu C++ but not with intel C++ (icc). The distilled code is given below, a variable "T" is stored as a const T& in the base class and when the derived class try to access...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
3
by: liam_herron | last post by:
Here is my code: #include <iostream> template<typename T> class RawPtr { protected:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.