2

o, I am attempting to create an overloaded output operator for a vector. This overloaded output operator is suppose to allow me to print the values in the vector in the form

[Data]^[Index] so for example if the data at index 3 is 4, it should print 3^4.

However, I can't seem to get it to work correctly. I need it to loop through the entire vector, but I can't seem to detect what I'm doing wrong.

Here is the function within the header.

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

Here is what I have for the function in the source file.

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;

Finally, here is my main

#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");

I believe that my issue lies somewhere within my main or within the source file, though I haven't worked with overloaded output operators in a very long time, so I'm not sure exactly what needs to be fixed.

1
  • You should have got a compiler error for os << c.at[i]; Commented Apr 24, 2018 at 4:05

2 Answers 2

3

There is no problem with your code except that at is function so you cannot use subscript operator with that,hence correct code for the above problem 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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

The problem with your code is somewhere else and it's actually hard to find in the first glimpse.

Here :

os << c.at[i];

std::vector::at is a function and it's the same as std::vector operator [], but it's a function and you can't use it like this. change it to :

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

Two minor things to take care of :

  1. it's better to print some delimiter(like -) when you print each value^index because it's hard to read this way.
  2. in this line for(int i = 0; i < c.size(); i++) to avoid getting '<' : signed/unsigned mismatch warning change it to for(unsigned int i = 0; i < c.size(); i++). std::vector::size returns size_type which is an unsigned integral type.

1 Comment

Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.