【问题标题】:C++ Error in overloading << with using vectors使用向量重载 << 时的 C++ 错误
【发布时间】:2015-09-21 15:26:38
【问题描述】:

我试图通过使用向量和

在我的头文件中,我有:

#ifndef homework7_MatrixViaVector_h
#define homework7_MatrixViaVector_h

#include<iostream>
#include<fstream>
#include<string>
#include <cstdlib>
#include <vector>

using namespace std;
template <class T>

class MatrixViaVector{

public:
    MatrixViaVector();
    MatrixViaVector(int m,int n);
    template <class H>
    friend ostream& operator <<(ostream& outs, const MatrixViaVector<H> &obj);
private:
    int m,n;
    vector<vector<T>> matrix;
};

#endif

在我的测试文件中,我有:

    #include "MatrixViaVector.h"

    template <class T>
    MatrixViaVector<T>::MatrixViaVector(){

        //creates a 3 by 3 matrix with elements equal to 0


        for (int i=0;i<3;i++){
            vector<int> row;
            for (int j=0;j<3;j++)
                row.push_back(0);
            matrix.push_back(row);
        }
    }

template <class T>
MatrixViaVector<T>::MatrixViaVector(int m,int n)//creates a m by n matrix????
{
    //creates a matrix with dimensions m and n with elements equal to 0

    for (int i=0;i<m;i++){
        vector<int> row;
        for (int j=0;j<n;j++)
            row.push_back(0);
        matrix.push_back(row);
    }
}

    template <class T>
    ostream& operator <<(ostream& outs, const MatrixViaVector<T> & obj)
    {
        //obj shud have the vector and therefore I should be able to use its size

        for (int i = 0; i < obj.size(); i++){
            for (int j = 0; j < obj.capacity(); j++)
                outs << " "<< obj.matrix[i][j];
            outs<<endl;
        }
        outs<<endl;
        return outs;
    }

    int main()
    {

    MatrixViaVector <int> A;
    MatrixViaVector <int> Az(3,2);//created an object with dimensions 3by2????
    cout<<A<<endl;
    cout<<Az<<endl;//this prints out a 3 by 3 matrix which i dont get????????
    }

【问题讨论】:

    标签: c++ vector operator-overloading


    【解决方案1】:

    您的MatrixViaVector&lt;&gt; 没有函数size(),但如果您打算使用向量的大小,请执行以下操作:

    改变这个sn-p:

    for (int i = 0; i < obj.size(); i++){
            for (int j = 0; j < obj.capacity(); j++)
                outs << " "<< obj.matrix[i][j];
            outs<<endl;
        }
    

    for (std::vector<int>::size_type i = 0; i < obj.matrix.size(); i++){
            for (std::vector<int>::size_type j = 0; j < obj.matrix.size(); j++)
                outs << " "<< obj.matrix[i][j];
            outs<<endl;
        }
    

    std::vector::size()std::vector::capacity() 是两个不同的函数,请检查它们的区别。

    【讨论】:

    • 如果尺寸不同怎么办。就像我们有一个 3 x 2 而不是 3 x 3 那么 for 循环将如何变化?
    • @guploo 不会有任何变化,因为 vector 的边界正在使用 vector::size() 函数进行检查
    • 我用我的新错误编辑了我的帖子,但我尝试创建一个 3 x 2 矩阵,但如果我尝试输出它,它仍然会输出一个 3 x 3 矩阵
    • @guploo 最好提出一个新问题或在帖子末尾使用子标题 EDIT 标记您的编辑,我不确定您编辑了什么
    • 我所做的只是为矩阵创建了另一个构造函数,该构造函数适用于 m x n 维,我创建了另一个对象,但我会在我的帖子中标记这些更改
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2019-03-24
    • 2020-06-30
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-23
    • 2014-10-29
    相关资源
    最近更新 更多