2016-03-24 5 views
1

私はCの経験がある程度ありました.より一般的には時間でしたが、現在はC++を学んでいます。これはテンプレートに関する演習です。メインプログラムとC++でオブジェクトを正しくインスタンス化する

template<typename T> 
struct mat4 { 

    T a1, a2, a3, a4; 
    T b1, b2, b3, b4; 
    T c1, c2, c3, c4; 
    T d1, d2, d3, d4; 

    mat4 (T a1 = T(), T a2 = T(), T a3 = T(), T a4 = T(), 
      T b1 = T(), T b2 = T(), T b3 = T(), T b4 = T(), 
      T c1 = T(), T c2 = T(), T c3 = T(), T c4 = T(), 
      T d1 = T(), T d2 = T(), T d3 = T(), T d4 = T()) : 
     a1(a1), b1(a2), c1(a3), d1(a4), 
     a2(b1), b2(b2), c2(b3), d2(b4), 
     a3(c1), b3(c2), c3(c3), d3(c4), 
     a4(d1), b4(d2), c4(d3), d4(d4) 
    { 
     std::cout << "at mat4 consctructor" << std::endl; 
    } 

    friend std::ostream& operator<<(std::ostream& out, const mat4 &that) 
    { 
     return out << that.a1 << that.a2 << that.a3 << that.a4 << std::endl << 
        that.b1 << that.b2 << that.b3 << that.b4 << std::endl << 
        that.c1 << that.c2 << that.c3 << that.c4 << std::endl << 
        that.d1 << that.d2 << that.d3 << that.d4; 
    } 
}; 

struct type { 

    type(double data = 0) : data(data) 
    { 
     std::cout << "at type constructor" << std::endl; 
    } 

    friend std::ostream& operator<<(std::ostream& out, const type &that) 
    { 
     return out << "[" << std::setfill('0') << std::setw(7) << 
      std::fixed << std::setprecision(2) << that.data << "]"; 
    } 

    private: 
     double data; 
}; 

そして、基本的なテンプレート:私は単純なクラス持っ

int main(int argc, char *argv[]) 
{ 
    mat4<type> mat1(1, 0, 0, 0, 
        0, 1, 0, 0, 
        0, 0, 1, 0, 
        0, 0, 0, 1); 

    std::cout << mat1 << std::endl; 

    mat4<type> mat2(); 

    std::cout << mat2 << std::endl; 

    mat4<type> mat3; 

    std::cout << mat3 << std::endl; 

    return 0; 
} 

を、MAT1とMAT3が正しくインスタンス化され、しかし、mat2はぎこちなく振る舞います。おそらくそれは小さな問題ですが、私はかなり難しいです。生産ouputをは次のとおりです。

at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at mat4 consctructor 
[0001.00][0000.00][0000.00][0000.00] 
[0000.00][0001.00][0000.00][0000.00] 
[0000.00][0000.00][0001.00][0000.00] 
[0000.00][0000.00][0000.00][0001.00] 
1 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at type constructor 
at mat4 consctructor 
[0000.00][0000.00][0000.00][0000.00] 
[0000.00][0000.00][0000.00][0000.00] 
[0000.00][0000.00][0000.00][0000.00] 
[0000.00][0000.00][0000.00][0000.00] 

MAT2の出力が1ているときに、うまく、MAT1とMAT3出力を見ての通り。どうして?

オブジェクトを正しくインスタンス化していますか?自動メモリでオブジェクトを初期化する適切な方法は何ですか?スコープベースのリソース管理にはこれが重要であると私は理解しています。

私は初心者レベルのC++スキルを身に付けています。私は何が間違っているのか、そしてなぜそれを理解したいのですか? あらかじめご注意いただきありがとうございます。あなたは警告をコンパイル有効にした場合

+4

問題は実際にはテンプレートには関係しませんが、 'T t();'を書くたびに 't'という名前の' T'型のオブジェクトは作成されません。代わりに 'T 'を返すゼロパラメータを取る関数を宣言する – user463035818

+1

単純な解は、関数宣言と間違えることのない' T t {}; '(中括弧)になりました。 – MSalters

+0

私は混乱を見ています...クイックレスポンスのためにありがとう!また、これが詐欺であることを私に知らせるために。私は本当に悪い検索用語を使用していた、私が望んでいたものの近くに何かを見つけていなかったと思います... – DVNO

答えて

5

は、あなたが得る:あなたが1を得た理由である、それを出力する際に​​、かなりそのtrue除いて、すべてを説明

main.cpp:59:20: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse] 
    mat4<type> mat2(); 
        ^~ 
main.cpp:59:20: note: remove parentheses to declare a variable 
    mat4<type> mat2(); 
        ^~ 
main.cpp:61:18: warning: address of function 'mat2' will always evaluate to 'true' [-Wpointer-bool-conversion] 
    std::cout << mat2 << std::endl; 
       ~~ ^~~~ 

1に変換されます。

+0

私は理解しています。今から警告フラグを使ってコンパイルすることを忘れないでください!速やかなご返信ありがとうございます... – DVNO

関連する問題