problem with extracting data from file

Hello, I'm trying to extract data from a text file and writing them to a vector of testLine objects

but getting errors:
filesbehandlung.cpp:47:3: error: unknown type name 'testLine'
testLine t{ (greeting, fruit, v1, v2) }
^
filesbehandlung.cpp:47:16: warning: expression result unused [-Wunused-value]
testLine t{ (greeting, fruit, v1, v2) }
^~~~~~~~
filesbehandlung.cpp:47:26: warning: expression result unused [-Wunused-value]
testLine t{ (greeting, fruit, v1, v2) }
^~~~~
filesbehandlung.cpp:47:33: warning: expression result unused [-Wunused-value]
testLine t{ (greeting, fruit, v1, v2) }
^~
filesbehandlung.cpp:47:42: error: expected ';' at end of declaration
testLine t{ (greeting, fruit, v1, v2) }
^
;
filesbehandlung.cpp:72:46: warning: expression result unused [-Wunused-value]
vector<testLine> test{dateiLesen<vector> > (test,"textfile.txt")};
^~~~
filesbehandlung.cpp:72:24: error: reference to overloaded function could not be resolved; did you mean to call it?
vector<testLine> test{dateiLesen<vector> > (test,"textfile.txt")};
^~~~~~~~~~~~~~~~~~
filesbehandlung.cpp:26:21: note: possible target for call
template<class T> T dateiLesen(string file) {
^
4 warnings and 3 errors generated.
here is my source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
#include <sstream>
#include <vector>
#include <numeric>

using std::string;
using std::ifstream;
using std::ofstream;

using namespace std;

template<class T> T from_string(const string s) {
	std::stringstream ss{s};
	
	T val{};
	
	if(!(ss>>val)) {
		 cerr<<"failed to convert from string";
	}
	return val;
}
template<class T> T dateiLesen(string file) {
	ifstream is{file.c_str()};
	std::istream_iterator<string> file_it{is};
	std::istream_iterator<string> eof{};
	
	T testl{};
	while(file_it!= eof && is){
		string greeting{*file_it};
		file_it++;
		
		string fruit{*file_it};
		file_it++;
		
		string val1{*file_it};
		file_it++;
		int v1{ from_string<int>(val1.substr(1, string::npos))};
		
		string val2{*file_it};
		file_it++;
		int v2{ from_string<int>(val2.substr(1, string::npos))};
		
		testLine t{ (greeting, fruit, v1, v2) }
		testl.push_back(t);
		
		return testl;
	}
}

enum Greeting{
	hello =1, goodbye
};
class testLine{

public:
	testLine() : greeting{hello}, fruit{}, value1{}, value2{} {	
	}
	testLine(Greeting g, string f,int v1,int v2): greeting{g}, fruit{f}, value1{v1}, value2{v2} {}	
private:
	Greeting greeting;
	string fruit;
	int value1{};
	int value2{};
	
};

int main(int argc, char *argv[]) {
	vector<testLine> test{dateiLesen<vector> > (test,"textfile.txt")};
	return 0;
		
	
}

txt file contains :
hello apple 3 5
goodbye pineapple 5 2
hello banana 6 20

thanks for your help
Last edited on
The compiler starts at the top and reads downwards. It needs to know about an object before you can use it.

It reaches this line:
testLine t{ (greeting, fruit, v1, v2) }
in which you are attempting to create an object of type testLine, but the compiler has never heard of it. Doesn't know what a "testLine" object is.

Put your defintion of testLine before you use one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <iostream>
#include <string>
#include <iterator>
#include <sstream>
#include <vector>
#include <numeric>

using std::string;
using std::ifstream;
using std::ofstream;

using namespace std;
enum Greeting{
	hello =1, goodbye
};
class testLine{

public:
	testLine() : greeting{hello}, fruit{}, value1{}, value2{} {	
	}
	testLine(Greeting g, string f,int v1,int v2): greeting{g}, fruit{f}, value1{v1}, value2{v2} {}	
private:
	Greeting greeting;
	string fruit;
	int value1{};
	int value2{};
	
};


template<class T> T from_string(const string s) {
	std::stringstream ss{s};
	
	T val{};
	
	if(!(ss>>val)) {
		 cerr<<"failed to convert from string";
	}
	return val;
}
template<class T> T dateiLesen(string file) {
	ifstream is{file.c_str()};
	std::istream_iterator<string> file_it{is};
	std::istream_iterator<string> eof{};
	
	T testl{};
	while(file_it!= eof && is){
		string greeting{*file_it};
		file_it++;
		
		string fruit{*file_it};
		file_it++;
		
		string val1{*file_it};
		file_it++;
		int v1{ from_string<int>(val1.substr(1, string::npos))};
		
		string val2{*file_it};
		file_it++;
		int v2{ from_string<int>(val2.substr(1, string::npos))};
		
		testLine t{ (greeting, fruit, v1, v2) }
		testl.push_back(t);
		
		return testl;
	}
}


int main(int argc, char *argv[]) {
	vector<testLine> test{dateiLesen<vector> > (test,"textfile.txt")};
	return 0;
	
}

errors: filesbehandlung.cpp:65:16: warning: expression result unused [-Wunused-value]
testLine t{ (greeting, fruit, v1, v2) }
^~~~~~~~
filesbehandlung.cpp:65:26: warning: expression result unused [-Wunused-value]
testLine t{ (greeting, fruit, v1, v2) }
^~~~~
filesbehandlung.cpp:65:33: warning: expression result unused [-Wunused-value]
testLine t{ (greeting, fruit, v1, v2) }
^~
filesbehandlung.cpp:65:12: error: no matching constructor for initialization of 'testLine'
testLine t{ (greeting, fruit, v1, v2) }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
filesbehandlung.cpp:19:7: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'testLine' for 1st argument
class testLine{
^
filesbehandlung.cpp:19:7: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const testLine' for 1st argument
class testLine{
^
filesbehandlung.cpp:22:2: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
testLine() : greeting{hello}, fruit{}, value1{}, value2{} {
^
filesbehandlung.cpp:24:2: note: candidate constructor not viable: requires 4 arguments, but 1 was provided
testLine(Greeting g, string f,int v1,int v2): greeting{g}, fruit{f}, value1{v1}, value2{v2} {}
^
filesbehandlung.cpp:65:42: error: expected ';' at end of declaration
testLine t{ (greeting, fruit, v1, v2) }
^
;
filesbehandlung.cpp:74:46: warning: expression result unused [-Wunused-value]
vector<testLine> test{dateiLesen<vector> > (test,"textfile.txt")};
^~~~
filesbehandlung.cpp:74:24: error: reference to overloaded function could not be resolved; did you mean to call it?
vector<testLine> test{dateiLesen<vector> > (test,"textfile.txt")};
^~~~~~~~~~~~~~~~~~
filesbehandlung.cpp:44:21: note: possible target for call
template<class T> T dateiLesen(string file) {
^
4 warnings and 3 errors generated.

Line 64 should be testLine t{ greeting, fruit, v1, v2 };.

Line 73: Can you explain what you're trying to do here?
Topic archived. No new replies allowed.