2017-10-25 1 views
0

간단한 매트릭스 클래스를 만들려고합니다. 표준 포인터를 사용하여 데이터를 저장하고 있습니다. 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;  
} 

당신의 소중한 도움에 미리 감사드립니다!

+0

코드 스 니펫이 오류와 일치하지 않습니다. 코드에서'std :: forward' 호출 주위에 둥근 괄호가 있습니다. 오류는 둥근 것입니다. 'std :: forward' 앞에'data_type'을 잊었습니까? – Darhuuk

+0

무엇을 의미합니까? –

+0

오 예 예. 둥근 고리와 중괄호로 모두 시도했습니다. –

답변

0

parameter pack expansion하려는 방식으로 작동하지 않습니다. 인수의 전체 목록을 확장하여 에 할당하려고합니다.

Create std::array from variadic template의 대답은뿐만 아니라 여기서 일하는 : 당신이 게시 코드 버그의 무리를 가지고

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); 

     std::initializer_list<data_type> il ({ std::forward<Ts>(args)... }); 
     std::copy(il.begin(), il.end(), data); 
} 

참고. main 함수에 세미콜론이 없기 때문에 컴파일되지 않습니다. -Wall -Wextra -pendantic으로 컴파일하면 얻을 수있는 경고도 있습니다.

+0

알 겠어 ...? 어떻게 수리 할 수 ​​있습니까? –

+0

@ MarcoGhiani 무엇을 수리하나요? 오류? 컴파일러는 무엇이 잘못되었는지, 그리고 대부분 어떻게 해결해야 하는지를 알려줍니다 (대부분의 오류는 반환하는 것이 아닌 함수로 보입니다). – Darhuuk