2017-10-25 8 views
0

単純な行列クラスを作成しようとしていますが、ポイントはデータを格納するために標準ポインタを使用していることです.2つのことはわかりません どのように問題を解決できますかこのコンストラクタで:ここ行列クラス、可変長コンストラクタ

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; 
    ^~~~~~ 

全体の実装:

# 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" ; 
    } 
} 

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

注意を。メイン関数にセミコロンがないので、コンパイルされません。 -Wall -Wextra -pendanticでコンパイルすると、警告が表示されます。

+0

私は見る...?どうすれば修理できますか? –

+0

@MarcoGhiani何を修理しますか?エラー?コンパイラは何が間違っているのか、それをどのように解決するのかを伝えます(ほとんどのエラーは、何かを返さない関数のようです)。 – Darhuuk

関連する問題