c++ - 在 C++ 中使用构造函数编译问题

标签 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

我的矩阵.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

我该如何解决我的问题?

最佳答案

尝试使用“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_;

...

关于c++ - 在 C++ 中使用构造函数编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25901514/

相关文章:

c++ - 从源代码构建 Qpid Messaging API(C++,绑定(bind))[Qpid C++ 1.37.0] - 缺少文件

c++ - 乱序执行的获取-释放对

c++ - 模板接口(interface)返回类型

c++ - 将原始数据从内存复制到文件(cuda)

c++ - 现在每个 Linux 发行版都附带 gcc/g++ 4.* 吗?

xcode - 手动编译 Metal 着色器

c++ - GCC 5 的模板参数阴影

java - 将 multidex apk 部署到 Fabric Beta 时出现 android ART NoClassDefFoundError

c++ - 什么可以编译得更快?每种方法的源代码和 header ,或者单个文件中的任何内容?

assembly - LLVM 和编译器命名法