gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:41:20 25 4
gpt4 key购买 nike

这个问题让我很不爽,因为我无法解决它。我设计了一个矩阵模板类和一个像素类,现在我想加入它们,但我无法完成任务。

我遇到的错误如下:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com