2016-12-14 78 views
0

我想定義在Array.cpp文件getRawDataPtr()功能和我下面執行,我得到了2個錯誤模板實現與編譯器

  • 錯誤82錯誤C2653:「數學」:不是類或命名空間名稱
  • 錯誤84錯誤C2988:無法識別的模板聲明/定義

  //file Array.cpp 
      template<typename T> 
      void * const math::Array<T>::getRawDataPtr 
      { 
      return buffer; 
      } 

    //file Array.h 
    #ifndef _ARRAY_ 
    #define _ARRAY_ 

    namespace math 
{ 

    //--------------------------------------------------------------------------------------------- 
    // Do NOT modify this section. For the implementation, see comment below the class declaration 
    //--------------------------------------------------------------------------------------------- 

    /*! The Array class implements a generic two-dimensional array of elements of type T. 
    */ 
    template <typename T> 
    class Array 
    { 
    protected: 
    //! Flat storage of the elements of the array of type T 
    T * buffer;      

    //! The width of the array (number of columns) 
    unsigned int width,    

    //! The height of the array (number of rows) 
       height;    

    public: 

    /*! Reports the width (columns) of the array 
    * 
    * \return the width. 
    */ 
    unsigned int getWidth() const { return width; }  

    /*! Reports the height (rows) of the array 
    * 
    * \return the height. 
    */ 
    unsigned int getHeight() const { return height; }  

    /*! Obtains a constant pointer to the internal data. 
    * 
    * This is NOT a copy of the internal array data, but rather a pointer 
    * to the internally allocated space. 
    */ 
    void * const getRawDataPtr(); 

    /*! Returns a reference to the element at the zero-based position (column x, row y). 
    * 
    * \param x is the zero-based column index of the array. 
    * \param y is the zero-based row index of the array. 
    * 
    * \return a reference to the element at position (x,y) 
    */ 
    T & operator() (int x, int y);      

    /*! Returns a constant reference to the element at the zero-based position (column x, row y). 
    * 
    * \param x is the zero-based column index of the array. 
    * \param y is the zero-based row index of the array. 
    * 
    * \return a reference to the element at position (x,y) 
    */ 
    const T & operator() (int x, int y) const; 

    /*! Constructor with array size. 
    * 
    * No default constructor is provided as it makes no sense. 
    * 
    * \param w is the width (columns) of the array 
    * \param h is the height (rows) of the array 
    */ 
    Array(unsigned int w, unsigned int h); 

    /*! Copy constructor. 
    * 
    * No default constructor is provided as it makes no sense. 
    * 
    * \param source is the array to replicate. 
    */ 
    Array(const Array<T> & source);      

    /*! Copy assignment operator 
    * 
    * \param source is the array to replicate. 
    */ 
    Array & operator = (const Array<T> & source); 

    /*! Equality operator. 
    * 
    * \param right is the array to compare the current object to. 
    * 
    * \return true if the current array and the source have the same dimensions AND 
    * one by one their elements of type T are the same. 
    */ 
    bool operator == (const Array<T> & right) const;  

    /*! Changes the internal array data storage size. 
    * 
    * If the one or both of the given dimensions are smaller, the array should be clipped 
    * by discarding the remaining elements in the rows and/or columns outside the margins. 
    * If the new dimensions are larger, pad the old elements with default values of type T. 
    * If the array is not yet allocated (zero width and height), allocate the internal buffer and 
    * set the array size according to the given dimensions. 
    * 
    * \param new_width is the user-provided width to resize the array to. 
    * \param new_height is the user-provided height to resize the array to. 
    */ 
    void resize(unsigned int new_width, unsigned int new_height); 

    /*! Virtual destructor. 
    */ 
    virtual ~Array();          

    }; 

    //--------------------------------------------------------------------------------------------- 
    // Do NOT add the member function implementation here. Add all implementations in the Array.hpp 
    // file included below. The two files will be concatenated upon preprocessing. 
    //--------------------------------------------------------------------------------------------- 


    } // namespace math 

    #include "Array.hpp" 

    #endif//------------------------------------------------------------ 
+0

註釋行可以刪除..更好的可讀性。 – shas

+0

只需在您聲明數學命名空間及其內容後將其移動... –

+0

@AdrianoRepetti我應該移動?它由於某種原因不能識別名稱空間! – madrugadas25845

回答

0

我想你有一個正確包含的問題(見下文)以及顯而易見的事實,即你忘記了getRawDataPtr()的定義中的括號。我的意思是:你應該寫

 //    parentheses added -----> vv 
     void * const math::Array<T>::getRawDataPtr() 
     { 
     return buffer; 
     } 

對列入......你的「array.h」包括「array.hpp」,但getRawDataPtr()在「array.cpp」來實現。

你有沒有把「array.hpp」和「array.cpp」混淆?

我可以說的是,下面的代碼編譯沒有錯誤

namespace math 
{ 
    template <typename T> 
     class Array 
     { 
     protected: 
      T * buffer;      

      unsigned int width, height;    

     public: 

      unsigned int getWidth() const { return width; }  
      unsigned int getHeight() const { return height; }  
      void * const getRawDataPtr(); 
      T & operator() (int x, int y);      
      const T & operator() (int x, int y) const; 
      Array(unsigned int w, unsigned int h); 
      Array(const Array<T> & source);      
      Array & operator = (const Array<T> & source); 
      bool operator == (const Array<T> & right) const;  
      void resize(unsigned int new_width, unsigned int new_height); 
      virtual ~Array();          
     }; 
} // namespace math 

template<typename T> 
void * const math::Array<T>::getRawDataPtr() 
{ return buffer; } 

int main() 
{ } 
+0

中,所以定義應該在Array.hpp中? – madrugadas25845

+0

@ madrugadas25845 - 我想在「array.hpp」甚至是「array.h」;它是一個模板類,因此當使用'Array '時應該可以看到完整的實現; ''getRawDataPtr()'它非常簡短,我認爲可以在'Array '的主體中實現,就像'getWidth()'和'getHeight()'一樣。 – max66