【问题标题】:C++ Overloading output using vectorC++ 使用向量重载输出
【发布时间】:2018-10-04 04:56:28
【问题描述】:

o,我正在尝试为向量创建重载输出运算符。假设这个重载的输出运算符允许我以格式打印向量中的值

[数据]^[索引] 例如,如果索引 3 处的数据是 4,它应该打印 3^4。

但是,我似乎无法让它正常工作。我需要它遍历整个向量,但我似乎无法检测到我做错了什么。

这是标题中的函数。

friend ostream & operator << (ostream &out, const vector<int> &c);

这是我在源文件中的函数。

ostream & operator << (ostream &os, const vector<int> &c)
{
    for (int i = 0; i < c.size(); i++)
    {
        os << c.at[i];
        os << "^";
        os << i;

    }
    return os;

最后,这是我的主要内容

#include "Polynomial.h"
#include <string>
#include <vector>
#include <utility>


int main()
{
    vector<int> poly1(10);
    vector<int> poly2(10);
    int x;
    int y;

    int choice;
    bool done = true;

    std::cout << "What do you wish to do?" << std::endl;
    std::cout << "1. Add two polynomials" << std::endl;
    std::cout << "2. Multiply two polynomials" << std::endl;
    std::cout << "3. Evaluate one polynomial at a given value" << std::endl;
    std::cout << "4. Find Coefficent for a given polynomial and given exponent" << std::endl;
    std::cout << "5. Find the leading exponent for a given polynomial" << std::endl;
    std::cout << "6. Exit "<< std::endl;

    std::cin >> choice;

    if (choice < 1 || choice > 6)
    {
        do
        {
            std::cout << "Invalid entry: please reenter choice" << std::endl;
            std::cin >> choice;


        } while (choice < 1 || choice > 6);
    }


    if (choice == 1)
    {
        std::cout << "Please input the first polynomial in the form of: (non-zero coefficient, exponent) pairs" << std::endl;
        do
        {
            std::cin >> x >> y;
            poly1.at(y) = x;

            std::cout << "done?" << std::endl;

            std::cin >> done;
        } while (done == false);

        std::cout << poly1 << std::endl;
    }
    if (choice == 2)
    if (choice == 3)
    if (choice == 4)
    if (choice == 5)
    if (choice == 6)


    system("pause");

我相信我的问题存在于我的主文件或源文件中,尽管我很长时间没有使用过重载的输出运算符,所以我不确定需要修复什么。

【问题讨论】:

  • 你应该得到os &lt;&lt; c.at[i];的编译器错误

标签: c++ vector output operator-overloading overloading


【解决方案1】:

您的代码的问题出在其他地方,乍一看实际上很难找到。

这里:

os << c.at[i];

std::vector::at 是一个函数,它和std::vector operator [] 一样,但是它是一个函数,你不能这样使用它。将其更改为:

os << c.at(i); //or os << c[i];

需要注意的两件小事:

  1. 打印每个value^index 时最好打印一些分隔符(如-),因为这样很难阅读。
  2. 在这一行for(int i = 0; i &lt; c.size(); i++) 以避免收到'&lt;' : signed/unsigned mismatch 警告将其更改为for(unsigned int i = 0; i &lt; c.size(); i++)std::vector::size 返回 size_type,这是一个无符号整数类型。

【讨论】:

  • 非常感谢!
【解决方案2】:

您的代码没有问题,除了 at is 函数,因此您不能使用下标运算符,因此上述问题的正确代码是:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
ostream & operator << (ostream &os, const vector<int> &c)
{
    for (int i = 0; i < c.size(); i++)
    {
        os << c.at(i);
        os << "^";
        os << i;

    }
    return os;
}
int main()
{
  std::vector<int> v{1,2,3};
  std::cout<<v<<std::endl;
  return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2013-05-03
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多