类模板与多维std::向量

huangapple go评论75阅读模式
英文:

Class template with multi-dimensional std::vectors

问题

我正在尝试初始化一个接受任何类型的2D向量作为参数输入的Matrix类模板

```cpp
#include <iostream>
#include <vector>

template <class T>
class Matrix 
{
    std::vector<std::vector<T>> entries;

public:
    // Constructor with parameters
    Matrix(std::vector<std::vector<T>> Entries) { 
        entries = Entries;
    }
};

int main() 
{
    std::vector<std::vector<double>> current_entries = { 
        {1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}
    };
    Matrix<std::vector<std::vector<double>>> M1(current_entries); // line 19
    return 0;
}

但是这在第19行给出了错误:

error: no matching function for call to 
'Matrix<std::vector<std::vector<double> > >::Matrix(std::vector<std::vector<double> >&)'

我怀疑这与类型是std::vector有关,因为当我将所有的2D向量替换为int时,而不改变其他任何内容,它就可以工作:

#include <iostream>
#include <vector>

template <class T>
class Matrix 
{
    T entries;

public:
    // Constructor with parameters
    Matrix(T Entries) { 
        entries = Entries;
    }
};

int main() 
{
    double current_entries = 3;
    Matrix<double> M1(current_entries); // line 19
    return 0;
}
英文:

I am trying to initialize a Matrix-class template, that takes a 2D-vector of any type as a parameter input:

#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;class T&gt;
class Matrix 
{
    std::vector&lt;std::vector&lt;T&gt;&gt; entries;

public:
    // Constructor with parameters
    Matrix(std::vector&lt;std::vector&lt;T&gt;&gt; Entries) { 
        entries = Entries;
    }
};

int main() 
{
    std::vector&lt;std::vector&lt;double&gt;&gt; current_entries = { 
        {1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}
    };
    Matrix&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; M1(current_entries); // line 19
    return 0;
}

But this gives the error at line 19:

error: no matching function for call to 
&#39;Matrix&lt;std::vector&lt;std::vector&lt;double&gt; &gt; &gt;::Matrix(std::vector&lt;std::vector&lt;double&gt; &gt;&amp;)&#39;

I suspect it has something to do with the type being a std::vector, because when I replace all the 2D-vectors with ints, without changing anything else it works:

#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;class T&gt;
class Matrix 
{
    T entries;

public:
    // Constructor with parameters
    Matrix(T Entries) { 
        entries = Entries;
    }
};

int main() 
{
    double current_entries = 3;
    Matrix&lt;double&gt; M1(current_entries); // line 19
    return 0;
}

答案1

得分: 2

I suspect it has something to do with the type being a vector, [...]
我怀疑这与类型为vector有关,[...]

The error has nothing to do with the type being std::vector, rather you are instantiating the class with the wrong type.
错误与类型为std::vector无关,而是您正在使用错误的类型来实例化类。

Your class template argument at the line
您在以下行的类模板参数

Matrix<std::vector<std::vector<double>>> M1(current_entries);
Matrix<std::vector<std::vector<double>>> M1(current_entries);

is std::vector&lt;std::vector&lt;double&gt;&gt;, not double.
std::vector&lt;std::vector&lt;double&gt;&gt;,而不是double

Therefore, you instantiate a Matrix with entries of the following type:
因此,您使用以下类型来实例化Matrix

std::vector<std::vector< std::vector<std::vector<double>> >> entries;
std::vector<std::vector< std::vector<std::vector<double>> >> entries;

Obviously, this is not what you want. You need just Matrix&lt;double&gt;, by which the entries will be with the type std::vector&lt;std::vector&lt;double&gt;&gt;.
显然,这不是您想要的。您只需要Matrix&lt;double&gt;,这样entries的类型将为std::vector&lt;std::vector&lt;double&gt;&gt;

英文:

>I suspect it has something to do with the type being a vector, [...]

The error has nothing to do with the type being std::vector, rather you are instantiating the class with the wrong type.

Your class template argument at the line

Matrix&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; M1(current_entries);
//     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  --&gt; is the &quot;T&quot;

is std::vector&lt;std::vector&lt;double&gt;&gt;, not double. Therefore, you instantiate a Matrix with entries of the following type:

 std::vector&lt;std::vector&lt; std::vector&lt;std::vector&lt;double&gt;&gt; &gt;&gt;    entries;
 //                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----&gt; &quot;T&quot;

Obviously, this is not what you want. You need just Matrix&lt;double&gt;, by which the entries will be with the type std::vector&lt;std::vector&lt;double&gt;&gt;.

答案2

得分: 0

以下是您要翻译的内容:

错误的原因已经在注释中解释过了。T=vector&lt;vector&lt;float&gt;&gt; 将扩展为 vector&lt;vector&lt;vector&lt;vector&lt;float&gt;&gt;&gt;&gt;,我相信这不是您想要的结果。

您可以保留以下通用解决方案,然后 T 可以是您想要的矩阵

#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;class T&gt;
class Matrix{
    T entries;
    public:
    Matrix(T Entries) { // Constructor with parameters
      entries = Entries;
    }

};

using namespace std;

int main()
{
    std::cout &lt;&lt; &quot;Let's Start....&quot; &lt;&lt; std::endl;
    std::vector&lt;std::vector&lt;double&gt;&gt; current_entries ={{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
    Matrix&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; M1(current_entries); // line 19
    std::cout &lt;&lt; &quot;All is well &quot; &lt;&lt; std::endl;
    return 0;
}

或者,您还可以为 vector&lt;vector&lt;T&gt;&gt; 添加模板部分特化,并可以提供以下单独的实现。

#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;class T&gt;
class Matrix {
    T entries;

public:
    Matrix(T Entries) : entries(Entries) {
        // Constructor with parameters
    }
};

// Partial specialization for std::vector&lt;std::vector&lt;T&gt;&gt;
// It should always be implemented after above template
template &lt;class T&gt;
class Matrix&lt;std::vector&lt;std::vector&lt;T&gt;&gt;&gt; {
    std::vector&lt;std::vector&lt;T&gt;&gt; entries;

public:
    Matrix(std::vector&lt;std::vector&lt;T&gt;&gt; Entries) : entries(Entries) {
        // Constructor with parameters
    }
};

int main() {
    std::cout &lt;&lt; &quot;Let's Start....&quot; &lt;&lt; std::endl;
    std::vector&lt;std::vector&lt;double&gt;&gt; current_entries = {{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
    Matrix&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; M1(current_entries);
    std::cout &lt;&lt; &quot;All is well &quot; &lt;&lt; std::endl;
    return 0;
}

希望这有所帮助。如果您需要更多关于任何解决方案的说明,请在评论中提出。

英文:

Reason for error is already explained in comments. T=vector&lt;vector&lt;float&gt;&gt; will expand entries to vector&lt;vector&lt;vector&lt;vector&lt;float&gt;&gt;&gt;&gt; and I believe that is not the outcome you want.

You can keep generic solution as below and then T can be your desired matrix

#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;class T&gt;
class Matrix{
    T entries;
    public:
    Matrix(T Entries) { // Constructor with parameters
      entries = Entries;
    }

};

using namespace std;

int main()
{
    std::cout &lt;&lt; &quot;Lets&#39;s Start....&quot; &lt;&lt; std::endl;
    std::vector&lt;std::vector&lt;double&gt;&gt; current_entries ={{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
    Matrix&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; M1(current_entries); // line 19
    std::cout &lt;&lt; &quot;All is well &quot; &lt;&lt; std::endl;
    return 0;
}

Alternatively you can also have Template Partial Specialisation for vector&lt;vector&lt;T&gt;&gt; and a separate implementation can be provided as below.

#include &lt;iostream&gt;
#include &lt;vector&gt;

template &lt;class T&gt;
class Matrix {
    T entries;

public:
    Matrix(T Entries) : entries(Entries) {
        // Constructor with parameters
    }
};

// Partial specialization for std::vector&lt;std::vector&lt;T&gt;&gt;
// It should always be implemented after above template
template &lt;class T&gt;
class Matrix&lt;std::vector&lt;std::vector&lt;T&gt;&gt;&gt; {
    std::vector&lt;std::vector&lt;T&gt;&gt; entries;

public:
    Matrix(std::vector&lt;std::vector&lt;T&gt;&gt; Entries) : entries(Entries) {
        // Constructor with parameters
    }
};

int main() {
    std::cout &lt;&lt; &quot;Let&#39;s Start....&quot; &lt;&lt; std::endl;
    std::vector&lt;std::vector&lt;double&gt;&gt; current_entries = {{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
    Matrix&lt;std::vector&lt;std::vector&lt;double&gt;&gt;&gt; M1(current_entries);
    std::cout &lt;&lt; &quot;All is well &quot; &lt;&lt; std::endl;
    return 0;
}

Hope this helps. If you need more description about any of the solution, please ask in a comment.

huangapple
  • 本文由 发表于 2023年6月26日 15:09:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定