0

Hi can anyone help? I need to print the top element of a stack of Objects (in this case points) and i am unable to find a solution online. I have attempted changing the datatype of top or straight up calling pointStack.top() in the cout but i have had no luck. Note. I have not included the Pop function as error C2679 is the issue

#include <iostream>
#include <stack>

#include "point.h"

using namespace std;

int main(){
    stack<Point> pointStack;

    Point p;
    int i;
    int counter = 0;

    for (i = 0; i < 10; i++){
        p.pCreate();
        Point p1(p.getXPos(), p.getYPos());
        pointStack.push(p1);
        counter++;
    }

    while (!pointStack.empty()){
        Point top = pointStack.top();
        cout << top; // error C2679
        cout << pointStack.top(); // also error C2679
    }

    system("PAUSE");
    return 0;
}

#ifndef __Point__
#define __Point__

using namespace std;

class Point{
private:
int x, y;
public:
Point();
Point(int x, int y);
int getYPos(){ return y; }
int getXPos(){ return x; }
void pCreate();
};
#endif

Point::Point(){
x = 0, y = 0;
}

Point::Point(int a, int b){
x = a;
y = b;
}
void Point::pCreate(){
x = -50 + rand() % 100;
y = -50 + rand() % 100;
}
Rowan Berry
  • 171
  • 7
  • 1
    It would be nice if you provide [MCVE](https://stackoverflow.com/help/mcve). You didn't show "point.h". – javaLover Apr 04 '17 at 03:23
  • [error C2679: binary '<<' : no operator found](http://stackoverflow.com/questions/22724064/error-c2679-binary-no-operator-found-which-takes-a-right-hand-operand-of) may help – javaLover Apr 04 '17 at 03:25
  • Ok it's pretty basic but will do – Rowan Berry Apr 04 '17 at 03:27
  • It's pretty unlikely it will bite you, but you never want to use double underscores in a C++ identifier outside of a Standard library implementation. [There's actually rules against it](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier), so `__Point__` is illegal. – user4581301 Apr 04 '17 at 03:44
  • There's no OOP in here. – n. m. could be an AI Apr 04 '17 at 04:17

2 Answers2

2

According to your description, I think you forget to overload the << operator, you should add a operator overload function for you Point class, check here.

For example:

class Point{
...
public:
    friend std::ostream& operator<< (std::ostream& stream, const Point& p) 
        {cout<<p.getx<<p.gety<<endl;}
...
};

Plus, you forget to pop the element from the stack in your while statement, this will lead to infinite loop.

Community
  • 1
  • 1
Jiahao Cai
  • 1,222
  • 1
  • 11
  • 25
  • thank you, i viewed the MS example on the web now that i know what i was looking for, added "friend ostream& operator<< (ostream& stream, const Point& p);" "ostream& operator<<(ostream& os, const Point& p) { os << p.x << "," << p.y; return os; }" To code with great success – Rowan Berry Apr 04 '17 at 13:50
  • @RowanBerry My pleasure :) – Jiahao Cai Apr 04 '17 at 13:52
0
cout<<top; 

doesn't work because point is a class created by you, the compiler cant print it. You should print the individual element of point by yourself. Like

     cout<<point.getx<<point.gety<<endl;

Or create an overload function for operator << in your class which does similar thing mentioned above.

Chandini
  • 540
  • 2
  • 11