1

I want to read a data from text file using structure and load it into vector.

so I wrote a following to do this but I failed to compile. I don't know what's wrong.

< what I did >

  1. text file contains data as follows;

835,5,0,0,1,1,8.994,0

(integer array[3], integer,integer,integer,integer,integer, float, Boolean)

2.I declared a structure contains following data types to load data into vector;

struct unfinished { int ans[3]; // contains answer int max; int st; int ba; int outs; int tri; float elapsed; bool fin; }; 

3.I wrote a code to read a data as follows;

#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <fstream> #include <vector> using namespace std; struct unfinished { int ans[3]; int max; int st; int ba; int outs; int tri; float elapsed; bool fin; }; vector<unfinished> read_rec(istream & is) { vector<unfinished> rec; int ans[3]; int max, st, ba, outs, tri; float elap; bool fini; while (is >> ans[0] >> ans[1] >> ans[2] >> max >> st >> ba >> outs >> tri >> elap >> fini) { rec.emplace_back(ans[0], ans[1], ans[2], max, st, ba, outs, tri, elap, fini); } return rec; } int main(void) { ifstream infile("unfin_rec.txt"); auto unfin = read_rec(infile); vector<unfinished>::const_iterator it; for (it = unfin.begin(); it != unfin.end(); it += 1) { cout << it->ans[0] << it->ans[1] << it->ans[2] << "," << it->max << "," << it->st << "," << it->ba <<","<<it->outs<<","<<it->tri<<","<<it->elapsed<<","<<it->fin<< endl; } system("pause"); return 0; } 

I failed to compile this code. error message was: >c:\program files (x86)\microsoft visual studio 12.0\vc\include\xmemory0(600): error C2661: 'unfinished::unfinished' : no overloaded function takes 10 arguments

again, I couldn't figure out what this message means. Please help!

thanks,

seihyung

3
  • Add unfinished at the end of struct definition ( } unfinished; ) Commented Apr 3, 2015 at 14:27
  • Praneeth, I tried what you said but it didn't work. Commented Apr 3, 2015 at 14:31
  • Possible duplicates: stackoverflow c++ read file structure, in other words, search the internet and StackOverflow before posting questions. Commented Apr 3, 2015 at 14:42

1 Answer 1

1

You need to add a constructor which takes 10 arguments and fills in the members of the struct, like so:

struct unfinished { unfinished(int a0, int a1, int a2, int m, int s, int b, int o, int t, float e, bool b): max(m), st(s), ba(b), outs(o), tri(t), elap(e), fini(b) { ans[0]=a0, ans[1]=a1, ans[2]=a2; } .... }; 
Sign up to request clarification or add additional context in comments.

1 Comment

haavee, I added a constructor to struct and error message has gone. but nothing poped up on the screen. in other words, I need to check code.... anyway, your advice worked and thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.