我是C++中的新成員。我正在使用g ++編譯器。我試圖學習C++中STL庫的操作。在工作時,我在代碼中發現了一些問題。請解釋錯誤的原因以及如何照顧錯誤。如何使用STL迭代器處理結構
#include<iostream>
#include<list>
using namespace std;
typedef struct corrd{
int x;
int y;
}XY;
int main()
{
list<XY> values;
list<XY>::iterator current;
XY data[10];
for(int i=0;i<10;i++)
{
data[i].x = i+10;
data[i].y = i+20;
}
for(int i=0;i<10;i++)
{
values.push_front(data[i]);
}
current = values.begin();
while(current!=values.end())
{
cout<<"X coord:"<<*current->x<<endl;//error: invalid type argument of unary ‘*’ (have ‘int’
cout<<"Y coord:"<<*current->y<<endl;//error: invalid type argument of unary ‘*’ (have ‘int’
current++;
}
}
使用前不要聲明變量,聲明他們在* *第一次使用。 –