간단한 매트릭스 클래스를 만들려고합니다. 표준 포인터를 사용하여 데이터를 저장하고 있습니다. 2 가지를 모릅니다. 어떻게 문제를 해결할 수 있습니까? 이 생성자의 :매트릭스 클래스, 가변 생성자
# include <iostream>
# include <utility>
# include <iomanip>
# include <vector>
# include <exception>
# include <iterator>
# include <cassert>
template <class U> class Matrix ;
template <class U>
std::ostream& operator<<(std::ostream& , Matrix<U>&);
template <class U>
std::ostream& operator<<(std::ostream& ,const Matrix<U>&);
//- class declararation
//
template <typename data_type = double>
class Matrix {
public:
//- friend function
template <class U>
friend std::ostream& operator<<(std::ostream& ,Matrix<U>&);
template <class U>
friend std::ostream& operator<<(std::ostream& ,const Matrix<U>&);
public:
constexpr Matrix(std::size_t r = 3 , std::size_t c = 3) noexcept;
constexpr Matrix(std::size_t r, std::size_t c, data_type val) noexcept;
template <typename ... Ts>
constexpr Matrix(std::size_t row , std::size_t col, Ts&&... args) noexcept;
virtual ~Matrix() noexcept ;
auto constexpr zeros() noexcept ;
auto constexpr initialize(data_type) noexcept ;
data_type& operator()(std::size_t, std::size_t);
//-------
private:
data_type *data ;
std::size_t row ;
std::size_t columns ;
auto constexpr isAllocate() noexcept { return data != nullptr ; }
};
// Matrix definition
//- standard constructor
template<typename data_type>
constexpr Matrix<data_type>::Matrix(std::size_t r, std::size_t c) noexcept : row{r}, columns{c},
data{new data_type[r*c]}
{
this->zeros();
}
template<typename data_type>
constexpr Matrix<data_type>::Matrix(std::size_t r, std::size_t c, data_type val) noexcept : row{r}, columns{c},
data{new data_type[r*c]}
{
this->initialize(val);
}
//--- construct by list of arguments
template<typename data_type>
template <typename ... Ts>
constexpr Matrix<data_type>::Matrix(std::size_t row ,
std::size_t col ,
Ts&&... args ) noexcept : row{row}, columns{col},
data{ new data_type[row*col] },
{
std::size_t i =0;
assert(sizeof...(args) == row*columns);
data[i++] = (std::forward<Ts>(args)...);
}
// destructor
template<typename data_type>
Matrix<data_type>::~Matrix() noexcept
{
if(isAllocate())
delete[] data;
}
//----------------------------------------------------------------------------------------------------
//
template <typename data_type>
auto constexpr Matrix<data_type>::zeros() noexcept {
if(isAllocate())
for(size_t i=0; i< row*columns ; i++)
data[i] = 0;
}
template <typename data_type>
auto constexpr Matrix<data_type>::initialize(data_type val) noexcept
{
if(isAllocate())
for(size_t i=0 ; i < row*columns; i++)
data[i] = val ;
}
//-----------
template <typename data_type>
data_type& Matrix<data_type>::operator()(std::size_t i, std::size_t j) { return data[(i-1) + row* (j-1)] ; }
// non member function
template <class U>
std::ostream& operator<<(std::ostream& out ,Matrix<U>& mat)
{
for(std::size_t r = 1; r < mat.row+1 ; r++)
{
for(std::size_t c = 1; c < mat.columns+1 ; c++)
{
out << mat(r,c) << ' ' ;
}
out << "\n" ;
}
}
template <class U>
std::ostream& operator<<(std::ostream& out , const Matrix<U>& mat)
{
for(std::size_t r = 1; r < mat.row+1 ; r++)
{
for(std::size_t c = 1; c < mat.columns+1 ; c++)
{
out << mat(r,c) << ' ';
}
out << "\n" ;
}
}
: 전체 구현 여기
In file included from matrixDrive.cpp:3:0:
matrix.H: In constructor ‘constexpr Matrix<U>::Matrix(std::size_t, std::size_t, Ts&& ...)’:
matrix.H:93:1: error: expected identifier before ‘{’ token
{
^
matrix.H: In instantiation of ‘constexpr Matrix<U>::Matrix(std::size_t, std::size_t, Ts&& ...) [with Ts = {double, double, double, double, double, double}; data_type = double; std::size_t = long unsigned int]’:
matrixDrive.cpp:11:68: required from here
matrix.H:96:19: error: cannot convert ‘<brace-enclosed initializer list>’ to ‘double’ in assignment
data[i++] = {std::forward<Ts>(args)...};
~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
matrixDrive.cpp: In function ‘int main()’:
matrixDrive.cpp:13:3: error: expected ‘,’ or ‘;’ before ‘return’
return 0;
^~~~~~
:
template<typename data_type>
template <typename ... Ts>
constexpr Matrix<data_type>::Matrix(std::size_t row ,
std::size_t col ,
Ts&&... args ) noexcept : row{row}, columns{col},
data{ new data_type[row*col] },
{
std::size_t i =0;
assert(sizeof...(args) == row*columns);
data[i++] = (std::forward<Ts>(args)...);
}
나는 오류가 발생했습니다
및 주요 기능 :
# include <iostream>
# include <iomanip>
# include "matrix.H"
using namespace std;
int main() {
Matrix<double> m1, m2(6,3);
Matrix<double> m3(3,2,1.12,2.434,3.546546,4.657,5.675675,6.542354)
return 0;
}
당신의 소중한 도움에 미리 감사드립니다!
코드 스 니펫이 오류와 일치하지 않습니다. 코드에서'std :: forward' 호출 주위에 둥근 괄호가 있습니다. 오류는 둥근 것입니다. 'std :: forward' 앞에'data_type'을 잊었습니까? – Darhuuk
무엇을 의미합니까? –
오 예 예. 둥근 고리와 중괄호로 모두 시도했습니다. –