矩阵运算、构造函数问题

发布于 2024-11-03 10:27:49 字数 2078 浏览 2 评论 0 原文

我想提出另一个关于矩阵运算的问题...

template <typename T>
struct TMatrix
{
    typedef std::vector < std::vector <T> > Type;
};


template <typename T>
class Matrix
{
    private:
            typename TMatrix <T>::Type items;       
            const unsigned int rows_count;          
            const unsigned int columns_count;   

    public:
             template <typename U>
             //Error, see bellow please
             Matrix ( const Matrix <U> &M ): 
                  rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( M.getItems()){} 
             unsigned int getRowsCount() const {return rows_count;}
             unsigned int getColumnsCount() const {return columns_count;}
             typename TMatrix <T>::Type const & getItems () const {return items;}
             typename TMatrix <T>::Type & getItems ()  {return items;}  

编译代码,编译器在这里停止:

Matrix ( const Matrix <U> &M ):  
      rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( M.getItems()){}  //Error

并显示以下错误:

Error   79  error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'const std::vector<_Ty>' to 'const std::allocator<_Ty> &'

但我不知道,为什么...再次感谢您的帮助...

已更新问题:

编译代码

template <class T>
template <typename U>
Matrix <T> :: Matrix ( const Matrix <U> &M )
: rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( M.getItems().begin(), M.getItems().end()){}

,结果如下:

Error   132 error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : 
cannot convert parameter 1 from 'const std::vector<_Ty>' to 'const std::allocator<_Ty> &'
c:\program files\microsoft visual studio 10.0\vc\include\xmemory    208

I would like to put another question about matrix operations...

template <typename T>
struct TMatrix
{
    typedef std::vector < std::vector <T> > Type;
};


template <typename T>
class Matrix
{
    private:
            typename TMatrix <T>::Type items;       
            const unsigned int rows_count;          
            const unsigned int columns_count;   

    public:
             template <typename U>
             //Error, see bellow please
             Matrix ( const Matrix <U> &M ): 
                  rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( M.getItems()){} 
             unsigned int getRowsCount() const {return rows_count;}
             unsigned int getColumnsCount() const {return columns_count;}
             typename TMatrix <T>::Type const & getItems () const {return items;}
             typename TMatrix <T>::Type & getItems ()  {return items;}  

Compiling the code, the compiler stops here:

Matrix ( const Matrix <U> &M ):  
      rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( M.getItems()){}  //Error

and shows the following error:

Error   79  error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'const std::vector<_Ty>' to 'const std::allocator<_Ty> &'

But I do not know, why... Thanks again for your help...

Updated question:

Compiling the code

template <class T>
template <typename U>
Matrix <T> :: Matrix ( const Matrix <U> &M )
: rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( M.getItems().begin(), M.getItems().end()){}

with the following result:

Error   132 error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : 
cannot convert parameter 1 from 'const std::vector<_Ty>' to 'const std::allocator<_Ty> &'
c:\program files\microsoft visual studio 10.0\vc\include\xmemory    208

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-11-10 10:27:49

您的模板化构造函数 Matrix::Matrix(const Matrix &M) 旨在根据给定的 构造一个 Matrix >矩阵。它通过调用初始化列表中的构造函数 vector>(vector>) 来实现此目的。

问题是 std::vector 不提供混合类型构造函数。

我不知道如何在初始化列表中解决这个问题。您可以在构造函数的主体中执行此操作。以下是对公共界面的更新,以实现此目的:

Matrix() : rows_count(), columns_count(), items() {}
template <typename U>
Matrix ( const Matrix <U> M ):
    rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( ) {
    for(int i = 0; i < M.getItems().size(); i++) {
        items.push_back(std::vector<T>(M.getItems()[i].begin(),M.getItems()[i].end()));
    }
}

Your templated constructor Matrix<T>::Matrix<U>(const Matrix<U> &M) is designed to construct a Matrix<T> given a Matrix<U>. It does this by invoking the constructor vector<vector<T>>(vector<vector<U>>) in the initializer list.

The problem is that std::vector does not provide a mixed-type constructor.

I don't know how to solve this in the initializer list. You might do it in the body of the constructor. Here are updates to your public interface to allow this:

Matrix() : rows_count(), columns_count(), items() {}
template <typename U>
Matrix ( const Matrix <U> M ):
    rows_count ( M.getRowsCount() ), columns_count ( M.getColumnsCount() ), items ( ) {
    for(int i = 0; i < M.getItems().size(); i++) {
        items.push_back(std::vector<T>(M.getItems()[i].begin(),M.getItems()[i].end()));
    }
}
瑕疵 2024-11-10 10:27:49

您忘记在顶部添加 #include 并在文件末尾添加 }; 。当我将这些添加到代码中时,它在我的计算机上编译得很好。

You forgot to #include <vector> at the top and a }; at the end of the file. When I add these to the code, it compiles just fine on my computer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文