[英]Reading to object from .in file
你好我有一個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
正如你所看到的那樣它開始以第二個位置開始閱讀並以第一個結束。
讀取f_in >> N
的行首先將計數讀入變量,但輸入文件缺少該計數。 (或者更確切地說,它將第一個'3'視為計數,這意味着元組實際上以'1 2 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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.