用c ++编译构造函数的问题

时间:2014-09-17 22:43:03

标签: c++ compilation

我在这个问题上遇到了不好的时间,因为我无法解决它。我设计了一个矩阵模板类和一个像素类,现在我想加入它们,但我还没能完成任务。

我遇到的错误如下:

pgm_matrix.cpp: In constructor ‘PGMmatrix::PGMmatrix(unsigned int, unsigned int)’:
pgm_matrix.cpp:13:23: error: expression list treated as compound expression in initializer [-fpermissive]
          Matrix<Pixel>* m(l, h);
                               ^
pgm_matrix.cpp:13:23: error: invalid conversion from ‘unsigned int’ to ‘Matrix<Pixel>*’ [-fpermissive]

我的Pixel.hpp:

#ifndef __PIXEL_HPP__
#define __PIXEL_HPP__

class Pixel
{
    private:
            int posx_;
            int posy_;
            unsigned int intensity_;

        public:
            Pixel();
            Pixel(int, int, unsigned int);  
            Pixel(Pixel const &);
            ~Pixel();

            bool operator==(const Pixel &)const;
            bool operator!=(const Pixel &)const;
            Pixel const &operator=(Pixel const &);
            void operator()(int, int, unsigned int);

            int get_pixel_pos_x() const;
            int get_pixel_pos_y() const;
            unsigned int get_pixel_intensity() const;
    };

    #endif

我的matrix.hpp:

#ifndef __MATRIX_HPP__
#define __MATRIX_HPP__

template<typename T>
class Matrix
{
    private:
        T** matrix_;
        unsigned int rows_;
        unsigned int cols_;

    public:
        Matrix();
        Matrix(unsigned int, unsigned int);
        Matrix(Matrix const &);
        ~Matrix();

        bool operator==(const Matrix &)const;
        bool operator!=(const Matrix &)const;
        Matrix const &operator=(Matrix const &);
        T &operator()(unsigned int, unsigned int);
        T const &operator()(unsigned int, unsigned int) const;

        T& data(unsigned int, unsigned int);
        T const &data(unsigned int, unsigned int) const;

        unsigned int const get_rows()const;
        unsigned int const get_cols()const;

};

template<typename T>
Matrix<T>::Matrix() : matrix_(0), rows_(0), cols_(0)
{
}

template<typename T>
Matrix<T>::Matrix(unsigned int rows, unsigned int cols)
{
    matrix_ = new T*[rows];
    for(unsigned int i=0; i < rows; i++)
        matrix_[i] = new T[cols];
    rows_ = rows;
    cols_ = cols;
}

template<typename T>
Matrix<T>::Matrix(Matrix<T> const & m_orig)
{
    rows_ = m_orig.rows_;
    cols_ = m_orig.cols_;
    matrix_ = new T*[rows_];
    for(unsigned int i=0; i < rows_; i++)
    {
        matrix_[i] = new T[cols_];
        for(unsigned int j=0; j < cols_; j++)
            matrix_[i][j]=m_orig.matrix_[i][j];
    }
}

template<typename T>
Matrix<T>::~Matrix()
{
    for(unsigned int i=0; i < rows_; i++)
        delete matrix_[i];
    delete matrix_;
    rows_ = 0;
    cols_ = 0;
}

template<typename T>
bool Matrix<T>::operator==(const Matrix & m)const
{
    if(m.cols_ != cols_ || m.rows_ != rows_)
        return false;
    for(unsigned int i=0; i < rows_; i++)
    {
        for(unsigned int j=0; j < cols_; j++)
        {
            if(m.matrix_[i][j] != matrix_[i][j])
                return false;
        }
    }
    return true;
}

template<typename T>
bool Matrix<T>::operator!=(const Matrix & m)const
{
    if( m == *this)
        return false;
    return true;
}

template<typename T>
Matrix<T> const &Matrix<T>::operator=(Matrix const & m_orig)
{
    if(this != &m_orig)
    {
        for(unsigned int k=0; k < rows_; k++)
            delete matrix_[k];
        delete matrix_;

        rows_ = m_orig.rows_;
        cols_ = m_orig.cols_;
        matrix_ = new T*[rows_];
        for(unsigned int i=0; i < rows_; i++)
        {
            matrix_[i] = new T[cols_];
            for(unsigned int j=0; j < cols_; j++)
                matrix_[i][j]=m_orig.matrix_[i][j];
        }
    }
    return *this;
}

template<typename T>
T &Matrix<T>::operator()(unsigned int i, unsigned int j)
{
    return matrix_[i][j];
}

template<typename T>
T const &Matrix<T>::operator()(unsigned int i, unsigned int j) const
{

    return matrix_[i][j];
}

template<typename T>
T& Matrix<T>::data(unsigned int i, unsigned int j)
{
    return matrix_[i][j];
}

template<typename T>
T const &Matrix<T>::data(unsigned int i, unsigned int j) const
{
    return matrix_[i][j];
}

template<typename T>
unsigned int const Matrix<T>::get_rows() const
{
    return rows_;
}

template<typename T>
unsigned int const Matrix<T>::get_cols() const
{
    return cols_;
}


#endif

最后失败的代码:

#include "pgm_matrix.hpp"
#include "matrix.hpp"
#include "pixel.hpp"


PGMmatrix::PGMmatrix(unsigned int l, unsigned int h)
{
    large_  = l;
    height_ = h;
    Matrix<Pixel>* m(l, h);
    matrix_ = m;//here is the problem
}

int main()
{
    PGMmatrix p(2,2);
    return 0;
}

PGM_matrix.hpp:

#ifndef __PGM_MATRIX_HPP__
#define __PGM_MATRIX_HPP__
#include "matrix.hpp"
#include "pixel.hpp"

class PGMmatrix
{
    private:
        Matrix<Pixel>* matrix_;
        unsigned int large_;
        unsigned int height_;

    public:
        PGMmatrix();
        PGMmatrix(unsigned int, unsigned int);
        PGMmatrix(PGMmatrix const &);
        ~PGMmatrix();

        bool operator==(const PGMmatrix &) const;
        bool operator!=(const PGMmatrix &) const;
        PGMmatrix const &operator=(PGMmatrix const &);
        void operator()(int, int, Pixel);

};
#endif

编译我做:

g++ pixel.o pgm_matrix.cpp -o pgm

如何解决我的问题?

1 个答案:

答案 0 :(得分:1)

尝试使用'new'在堆上创建新实例。

...

PGMmatrix::PGMmatrix(unsigned int l, unsigned int h)
{
  large_  = l;
  height_ = h;
  matrix_ = new Matrix<Pixel>(l, h); // Use new, to create a new instance on the heap
}

...

或者最好不要使用指针。

...

 class PGMmatrix
 {
 private:
   Matrix<Pixel> matrix_;

...