2016-12-17 3 views
-1

Ich möchte die T & Operator() (int x, int y) Funktion definieren, aber ich kann nicht verstehen, wie Sie es definieren. Ich habe diese Funktion in der Array.h Datei, so dass ich es in der Array.hpp Datei definieren muss, denke ich. Hat jemand eine Idee?Implementierung in Operatoren mit Vorlagen

#ifndef _ARRAY_ 
#define _ARRAY_ 

namespace math 
{ 

/*! 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;      
     unsigned int width,   
        height; 
     /* 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); 

     }; 
    } // namespace math 

#include "Array.hpp" 
#endif 
+0

Fragen Sie, wie man es außerhalb der Linie definiert? Oder wie man es umsetzt? – doctorlove

+0

@doctorlove Ich frage, wie man es definiert, danke – madrugadas25845

Antwort

0

wie diese, zum Beispiel.

template <typename T> 
class Array 
{ 
protected: 
    //! Flat storage of the elements of the array of type T 
    T * buffer; 
public:      
    unsigned int width, height; 
    /// non-constant element access 
    /// \param[in] x is the zero-based column index of the array. 
    /// \param[in] 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) 
    { 
    return (buffer+x*height)[y]; 
    } 
    /// constant element access 
    /// \param[in] x is the zero-based column index of the array. 
    /// \param[in] y is the zero-based row index of the array. 
    /// \return a const reference to the element at position (x,y) 
    T const & operator() (int x, int y) const 
    { 
    return const_cast<Array&>(*this)(x,y); // note: re-use the above 
    } 
};