C++ 将对象添加到C++;

C++ 将对象添加到C++;,c++,matrix,vector,C++,Matrix,Vector,我想用vector生成一个矩阵,我用类分数的对象代替int 然而,我正在尝试添加元素,但到目前为止没有运气 class Fraction { public: int num; int den; friend ostream& operator<< (ostream& os, Fraction& fr); void operator= (const Fraction& fr); }; ostream& oper

我想用vector生成一个矩阵,我用类
分数
的对象代替int

然而,我正在尝试添加元素,但到目前为止没有运气

class Fraction
{
public:
    int num;
    int den;

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

ostream& operator << (ostream& os, Fraction& fr)
{
     os << fr.num << "/" << fr.den;

     return os;
}

void Fraction::operator=(const Fraction& fr)
{
    num = fr.num;
    den = fr.den;
}

int main()
{
    Fraction fr;

    int colunas = 2, linhas = 2;

    vector<vector<Fraction>> mat(linhas, vector<Fraction>(colunas));

    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        for (int j = 0; j < linhas; ++j)
        {
            int x;
            vector<Fraction> temp;
            cin >> x;

            fr.num = x;
            fr.den = 1;

            temp.push_back(fr);

            mat.push_back(temp);
        }

        cout << endl;
    }

    int len = mat.size();

    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
            {
                cout << mat[ i ] [ j ].num << " ";
            }

       cout << endl;
    }
}
但它抛出了一个错误:

无匹配运算符=

编辑:遵循建议并相应更改代码

class Fraction
{
public:
    int num;
    int den;

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

ostream& operator << (ostream& os, Fraction& fr)
{
    os << fr.num << "/" << fr.den;

    return os;
}

void Fraction::operator=(const Fraction& fr)
{
    num = fr.num;
    den = fr.den;
}

int main()
{
    Fraction fr;

    int colunas = 2, linhas = 2;

    vector<vector<Fraction>> mat;//(linhas, vector<Fraction>(colunas));
    vector<Fraction> temp;
    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        for (int j = 0; j < linhas; ++j)
        {
            int x;

            cin >> x;

            fr.num = x;
            fr.den = 1;

            temp.push_back(fr);
        }

        mat.push_back(temp);

        cout << endl;
    }

    //cout << mat[0][1];
    //cin.get();
    //cin.get();

    int len = mat.size();
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
        {
            cout << mat[ i ] [ j ].num << " ";
        }

        cout << endl;
    }
}
输出

34 3
34 3 2 4
这不是我期望的输出,但是,前两个输入数字组成第一列,后两个组成第二列。所以输出应该是这样的:

预期产量

34 2
3 4

如果我访问
mat[0][1]
我希望它会给我
2/1
,但它会给我
3/1
这一行中使用的
向量
构造函数:

vector<vector<Fraction>> mat(linhas, vector<Fraction>(colunas));

如注释中所述,您不需要编写副本分配运算符。编译器将自动为您生成一个具有正确行为的


如果要避免创建具有未初始化值的
Fraction
对象,如在
mat
构造函数中所做的,则可以编写自己的构造函数(这将禁用隐式声明的构造函数),例如:


你的
垫子向后推(温度)
放错地方了。将其置于内部循环之外。
void操作符=(常量分数&fr)--此功能完全没有必要。编译器默认赋值运算符可以完美地工作,而无需引入用户定义的版本。永远不要编写编译器将为您提供的不必要的代码,无bug且经过优化。不要这样做:
void operator=(const-Fraction&fr)赋值应该是一个引用,而不是一个空值,这样您就可以像其他使用=。Paul在上面的评论指出,这个类可以很好地使用默认赋值。@PaulMcKenzie实际上我从来都不想要赋值运算符,我只是想看看直接将
temp
变量赋值到矩阵中我想要的位置是否能解决我的问题,当它给我
no matching operator=
错误时,我试着提供赋值运算符,你没有忽略
vector temp我说过你应该…我更改了代码,使其与你的最后一段代码相等,但它的行为仍然不像矩阵。@HBatalha然后请在问题更改后添加你的当前代码,并提供一个你使用的输入、你得到的输出和你想要的输出的示例。但不要删除或覆盖当前问题内容。
34 2
3 4
vector<vector<Fraction>> mat(linhas, vector<Fraction>(colunas));
vector<vector<Fraction>> mat;
class Fraction
{
public:
    int num;
    int den;

    Fraction(int num_, int den_) : num(num_), den(den_) {}

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

//...

        for (int j = 0; j < linhas; ++j)
        {
            int x;
            vector<Fraction> temp;
            cin >> x;

            temp.push_back(Fraction(x, 1));

            mat.push_back(temp);
        }
    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        vector<Fraction> temp;
        for (int j = 0; j < linhas; ++j)
        {
            int x;
            cin >> x;

            temp.push_back(Fraction(x,1));
        }
        mat.push_back(temp);

        cout << endl;
    }