嗨,任何人都可以幫忙嗎?我需要打印對象堆棧的頂部元素(在本例中爲點),我無法在線找到解決方案。我試圖改變頂部或直線的數據類型在cout中調用pointStack.top(),但我沒有運氣。注意。我沒有將彈出功能錯誤C2679是問題無法使用OOP打印堆棧項目C++
#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;
}
如果您提供[MCVE](https://stackoverflow.com/help/mcve),那就太好了。你沒有顯示「point.h」。 – javaLover
[error C2679:binary'<<':no operator found](http://stackoverflow.com/questions/22724064/error-c2679-binary-no-operator-found-which-takes-a-right-hand- operand-of)可能有幫助 – javaLover
確定它是非常基本的,但會做 –