老大们帮忙调下小程序!!

rand520 2006-07-23 10:00:53
我做迷宫问题,想把坐标压入栈中 有两个数据所以做了个结构
但编译时出现问题帮忙看一下 谢谢
#include<iostream>
#include<stack>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;
struct correct{//结构含两个坐标
int x;
int y;
};
void main(){
ifstream in("abc.txt");
vector<vector<int> > matrix;
vector<int> vec;
int element;
correct cor;
stack<int> sta;
for(string s;getline(in,s);){//把abc.txt中内容读入二维向量matrix
for(istringstream sin(s);sin>>element;)vec.push_back(element);
matrix.push_back(vec);
vec.clear();
}
for(int i=0;i<matrix.size();i++){//把文档中内容压栈
cor.x=matrix[i][0];
cor.y=matrix[i][1];
sta.push(cor);//问题出现在这一行。
}
while(!sta.empty()){//输出栈中内容
cout<<sta.top()<<endl;
sta.pop();
}
}
错误提示如下:
--------------------Configuration: text - Win32 Debug--------------------
Compiling...
text.cpp
C:\Documents and Settings\江校\桌面\程序\科大复试题\text\text.cpp(26) : error C2664: 'push' : cannot convert parameter 1 from 'struct correct' to 'const int &'
Reason: cannot convert from 'struct correct' to 'const int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.

text.exe - 1 error(s), 0 warning(s)
能不能帮忙看看错在哪?还有用什么方法把坐标压如栈中比较好?
谢谢!!!
...全文
114 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rand520 2006-07-24
  • 打赏
  • 举报
回复
哈哈 我也经常遇见这种问题 郁郁中~~~~~
liking100 2006-07-23
  • 打赏
  • 举报
回复
ostream& operator << (ostream& out,const correct &output)
{
out<<output.x<<"\n";
out<<output.y<<"\n";

return out;
}

把这个加进去就可以解决这个问题了:
error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'struct correct' (or there is no acceptable conversion)


因为correct并非内置类型,想直接用cout输出,需要重载运算符号<<

汗,又被人赶在前面了,呵呵
rand520 2006-07-23
  • 打赏
  • 举报
回复
努力学习是肯定的 不过要考研现在要干的
就是被数学 英语折磨!!!!!
哎 考上了就有时间了~~~~
郁郁中!!
rand520 2006-07-23
  • 打赏
  • 举报
回复
给分了 我亏啊 55555555555555555
我赚点分不容易啊!!!
眼泪花花的~~~
chenhu_doc 2006-07-23
  • 打赏
  • 举报
回复
恩,rand520(々翔)????

呵呵,不错,加油学习,多来csdn帮帮其他人咯。。。。。
rand520 2006-07-23
  • 打赏
  • 举报
回复
晕 你居然在我前面
做人要厚道~~~~~~~
chenhu_doc 2006-07-23
  • 打赏
  • 举报
回复
第一个是定义stack的每个元素的类型,应该是struct...
因为:sta.push(cor);//问题出现在这一行。 //把cor一个struct correct类型的变量压入stack中。。。

第二个: 由于sta中的元素是 struct correct类型,所以输出的时候。。
cout<<sta.top().x << " "<< sta.top().y<<endl; //要解到每个基本的类型。。。
rand520 2006-07-23
  • 打赏
  • 举报
回复


#include<iostream>
#include<stack>
#include<vector>
#include<sstream>
#include<fstream>
using namespace std;
struct correct{
int x;
int y;
};
void main(){
ifstream in("abc.txt");
vector<vector<int> > matrix;
vector<int> vec;
int element;
correct cor;
stack<correct> sta;
for(string s;getline(in,s);){
for(istringstream sin(s);sin>>element;)vec.push_back(element);
matrix.push_back(vec);
vec.clear();
}
for(int i=0;i<matrix.size();i++){
cor.x=matrix[i][0];
cor.y=matrix[i][1];
sta.push(cor);
}
while(!sta.empty()){
cout<<sta.top().x<<" "<<sta.top().y<<endl;
sta.pop();
}
}
哈哈 自己解决了别说我不厚道 没分了啊 结贴啦
chenhu_doc 2006-07-23
  • 打赏
  • 举报
回复
1. stack<struct correct> sta;

2. while(!sta.empty()){//输出栈中内容
cout<<sta.top().x << " "<< sta.top().y<<endl;
rand520 2006-07-23
  • 打赏
  • 举报
回复
我知道为什么了 但如何输出呢?
rand520 2006-07-23
  • 打赏
  • 举报
回复
我傻了
stack<int> sta;这一行应该改成stack<correct> sta;
不过又有问题了
cout<<sta.top()<<endl;这一行
--------------------Configuration: text - Win32 Debug--------------------
Compiling...
text.cpp
C:\Documents and Settings\江校\桌面\程序\科大复试题\text\text.cpp(29) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'struct correct' (or there is no acceptable conversion)
Error executing cl.exe.

text.exe - 1 error(s), 0 warning(s)
经常出现这个问题 为什么?

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧