簡體   English   中英

如何使用模板調用內部類的構造函數?

[英]c++ How do i call a constructor of an interior class with templates?

我想通過調用Nach(3,a)來終止Nach對象,其中a是Mat對象。 此時有些事情失敗了。 (線:Graph :: Nach h = Graph :: Nach(3,a);)

Nach在Graph的內部類中定義。 圖使用兩個模板。 Nach代表鄰居,Mat代表材料,Masch代表機器。

#include <iostream>
#include <vector>
#include <string>

class Mat {

    public:
        int _id;
        int _type;
        double _amount;

        Mat (int id, int type, double amount );
};
Mat::Mat(int id, int type, double amount){
    std::cout<<"Mat constuktor used"<<std::endl;
    _id = id; _type = type; _amount = amount;
};




class Masch {
    public:
        int _id;
        int _rez;
        double _count;

        Masch (int id, int rez, double count );

};
Masch::Masch(int id, int rez, double count){
    std::cout<<"Masch constuktor used"<<std::endl;
    _id = id; _rez = rez; _count = count;
};


template <class V, class E>
class Graph {
    public:
        class Nach {
            public:
                int _id;
                Mat _e;

                Nach(int id, Mat e);
        };

        int num;
        std::vector<V> nodes;
        std::vector<std::vector<Nach>> _nach;

        void addVertex(V t);
        void addEdge(V a, V b, E e);

        Graph();


};
template <class V, class E> Graph<V,E>::Graph(){
    std::cout<<"Graph constuktor used"<<std::endl;
    num = 0;    
}
template <class V, class E> Graph<V,E>::Nach::Nach(int id, Mat e){
    std::cout<<"Nach constuktor used"<<std::endl;
    _id = id; _e = e;
}
template <class V, class E> void Graph<V,E>::addVertex(V t){
    nodes.push_back(t);
    _nach.push_back(std::vector<Nach>());
    num++;
}
template <class V, class E> void Graph<V,E>::addEdge(V a, V b, E e){
    int i = a._id;
    int j = b._id;
    //Graph<V, E>::Nach x(3, e);
    //_nach[j].push_back(Nach(i,e));

}

int main (){

    Mat a= Mat(0,1,0.1); 
    //Mat b= Mat(1,1,0.3);

    Masch c = Masch(0,0,0.1);

    Graph <Masch, Mat> g = Graph<Masch, Mat>();

    //std::cout << a+b <<std::endl;
    //std::cout << c <<std::endl;

    Graph<Masch, Mat>::Nach h = Graph<Masch, Mat>::Nach(3,a);

    g.addVertex(c);
    g.addVertex(c);

    //g.addEdge(c,c,a);


    return 0;

}

我希望創建一個Nach類的實例。

但這會引起Mat構造函數的調用錯誤。 我不知道“ Nach”的調用在何處調用“ Mat”

錯誤挑戰

Hello.cpp: In instantiation of ‘Graph<V, E>::Nach::Nach(int, Mat) [with V = Masch; E = Mat]’:
Hello.cpp:94:57:   required from here
Hello.cpp:65:65: error: no matching function for call to ‘Mat::Mat()’
 template <class V, class E> Graph<V,E>::Nach::Nach(int id, Mat e){
                                                                 ^
Hello.cpp:16:1: note: candidate: Mat::Mat(int, int, double)
 Mat::Mat(int id, int type, double amount){
 ^
Hello.cpp:16:1: note:   candidate expects 3 arguments, 0 provided
Hello.cpp:7:7: note: candidate: constexpr Mat::Mat(const Mat&)
 class Mat {
       ^
Hello.cpp:7:7: note:   candidate expects 1 argument, 0 provided
Hello.cpp:7:7: note: candidate: constexpr Mat::Mat(Mat&&)
Hello.cpp:7:7: note: candidate expects 1 argument, 0 provided

調用Nach構造函數時,每個成員都是默認構造的,然后執行構造函數主體。 Mat沒有默認的構造函數。 您看不到錯誤消息中提到的對Mat()的調用,因為該調用是由編譯器生成的。 解決方案是使用初始化列表而不是構造然后分配。 大多數時候,構造函數主體應該為空。

template <class V, class E>
Graph<V, E>::Nach::Nach(int id, Mat e)
  : _id{id}, _e{e} {}

這將直接構造成員,而不是首先默認構造它們,然后再分配給它們(就像使用Java或類似方法一樣)。 您應該始終使用初始化列表。

我還要提到其他幾件事。

  • 在模板參數列表中使用class有點過時。 允許向后兼容class ,因此應避免使用。 class最初是為了避免在語言中添加另一個關鍵字,后來又添加了typename
  • 以一個或多個_underscores_開頭的標識符通常不是一個好主意。 這是因為以下划線開頭的標識符是為實現保留的,因此可能會發生沖突。
  • 當使用初始化列表時,構造函數參數的名稱可能與成員名稱相同,因此允許以下內容並按預期運行。 幾乎沒有任何理由在成員名稱中添加下划線(或任何其他字符序列)以避免沖突。

這就是實現構造函數的方式。

template <typename V, typename E>
Graph<V, E>::Nach::Nach(const int id, const Mat e)
  : id{id}, e{e} {}

在創建Nach嵌套類時,需要在初始化構造函數內部的_e成員時創建Mat(具有默認構造函數)。

您應該嘗試重寫構造函數以復制傳遞的mat:

template <class V, class E> Graph<V,E>::Nach::Nach(int id, Mat e): _e(e) {
    // _e is now initialize from a e copy before entering here.
    std::cout<<"Nach constuktor used"<<std::endl;
    _id = id;  // this could be moved as welll
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM