Operator overloading causes cout to not accept string inputs

Sep 25, 2021 at 11:16pm
Hello, I have a program where I made an object called Poly to represent a polynomial. There is also a friend function which overloads the operator << so that cout will output a polynomial expression when fed a Poly object. The program is 500 lines long, so I am just showing the relevant parts. The problem lies in main() where what prints to the terminal is different before and after declaring some Poly objects. Why does the problem happen and how do I fix it?

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

class Poly{
private:
//some member variables here
public:
//member function declarations
//some constructors and a destructor
//some member functions here
//some friend functions here
//overloaded << friend function:

friend ostream& operator<<(ostream& out, const Poly &aPoly);
}

int main(){

//the problem is here

cout << "works good"; //works fine

Poly p1, p2;

cout << "nothing now" //nothing prints to the terminal

return 0;
}

//class member function definitions
//16 member functions
//17th is the overloaded friend function:

//17
ostream& operator<<(ostream& out, const Poly &aPoly){


out << aPoly.coeff[0] << "x^" << aPoly.coeff[1];

for(int i = 3; i <= aPoly.arraySize; i += 2){

out << " + " << aPoly.coeff[i-1] <<"x^" << aPoly.coeff[i];

}

return(out);
}











Sep 26, 2021 at 2:52am
I can't replicate the issue, it doesn't seem that the issue is in the code you've given. I'd recommend showing the entire Poly class.


Remember to use code tags so we can read your code, like this:


Code Tags Look Like:

[code]
//Your code here
[/code]



This code works just fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Poly 
{
private:
	int ID;
public:

	static int a;

	Poly()
	{
		ID = a++;
	}

	friend ostream& operator<<(ostream& out, const Poly& aPoly);
};

int Poly::a = 0;

int main()
{

	//the problem is here

	cout << "works good\n"; //works fine

	Poly p1, p2;

	cout << p1 << '\n' << p2 << '\n';

	cout << "nothing now"; //nothing prints to the terminal

	return 0;
}

ostream& operator<<(ostream& out, const Poly& aPoly) 
{
	out << aPoly.ID;

	return(out);
}
Sep 26, 2021 at 8:26am
@math1212,

The snippet of code that you give doesn't even use the << operator for a polynomial, so it wouldn't have anything to do with it.

Copy your code to a temporary program and reduce it to the minimum compileable example that will reproduce your problem. There is nothing in what you have shown that would cause a problem.

I would hazard a guess that you are going beyond array bounds somewhere, but you haven't shown enough code to diagnose that.
Sep 26, 2021 at 8:49am
I would hazard a guess that you are going beyond array bounds somewhere, but you haven't shown enough code to diagnose that.

I don't know how I didn't see this but there's a clear issue in the code here:

1
2
3
4
	for (int i = 3; i <= aPoly.arraySize; i += 2)
	{
		out << " + " << aPoly.coeff[i - 1] << "x^" << aPoly.coeff[i];
	}


Assuming that the member arraySize is equal to aPoly.size(), the <= should be replaced with just <.
Sep 26, 2021 at 8:50pm
@math1212

You say that your int main() does this:

1
2
3
4
5
6
int main()
{
   cout << "works good"; //works fine
   Poly p1, p2;
   cout << "nothing now"; //nothing prints to the terminal
}


If that is your int main() then it doesn't call << for a Poly object, so it is irrelevant whether that output routine is correct or not (although @zapshe conjectures that it wouldn't be if it were used).

But as it stands you are calling a Poly constructor with no arguments.

And you haven't shown us what exactly is in your Poly class, or what a Poly constructor with no arguments would do.

So, it's a tad difficult to answer your question.
Oct 1, 2021 at 11:46am
1
2
3
4
5
6
7
8
ostream& operator<<(ostream& out, const Poly &aPoly) {
    out << aPoly.coeff[0] << "x^" << aPoly.coeff[1];

    for(int i = 3; i <= aPoly.arraySize; i += 2)
        out << " + " << aPoly.coeff[i-1] <<"x^" << aPoly.coeff[i];

    return out; // just return the stream
}
Topic archived. No new replies allowed.