你好我有一个3个int的列表,间隔2个空格,我想读取它们并创建一个对象。我将从数据结构,类和前/后数据中粘贴我的代码。我找不到错误的原因。欢迎任何帮助:
Class.h:
class RAngle{
private:
int x,y,l,b;
int solution,prec;
RAngle(){
x = y = solution = prec = b = l = 0;
}
RAngle(int i,int j,int k){
x = i;
y = j;
l = k;
solution = 0; prec=0; b=0;
}
friend ostream& operator << (ostream& out, const RAngle& ra){
out << ra.x << " " << ra.y<<" " << ra.l <<endl;
return out;
}
friend istream& operator >>( istream& is, RAngle& ra){
is >> ra.x;
is >> ra.y;
is >> ra.l;
return is ;
}
};
dataStructure.h:
template <class T>
class List
{
private:
struct Elem
{
T data;
Elem* next;
};
Elem* first;
void push_back(T data){
Elem *n = new Elem;
n->data = data;
n->next = NULL;
if (first == NULL)
{
first = n;
return ;
}
Elem *current;
for(current=first;current->next != NULL;current=current->next);
current->next = n;
}
main.cpp中:
void readData(List <RAngle> &l){
*RAngle r;
int N;
ifstream f_in;
ofstream f_out;
f_in.open("ex.in",ios::in);
f_out.open("ex.out",ios::out);
f_in >> N;
for(int i=0;i<13;++i){
f_in >> r;
cout << r;
l.push_back(r);
}
f_in.close();
f_out.close();
}*
输入数据:
3 1 2
1 1 3
3 1 1
输出(读取时打印):
1 2 1
1 3 3
1 1 3
正如你所看到的那样,它开始以第二个位置开始阅读,并以第一个结束。
答案 0 :(得分:1)
读取f_in >> N
的行首先将计数读入变量,但输入文件缺少该计数。 (或者更确切地说,它将第一个'3'视为计数,这意味着元组实际上以'1 2 1 ...'开头。)
答案 1 :(得分:1)
正如你所看到的那样,它开始以第二个位置开始阅读,并以第一个结束。
那是因为你提取了第一个值。
在readData
函数中:
f_in >> N; // This line will extract the first value from the stream.
// the loop starts at the second value
for(int i=0;i<13;++i){
f_in >> r;
cout << r;
l.push_back(r);
}