Errors in the code

Hi! There are some errors in my code that I don't know how to solve.
The rows 73, 74, 99 and 103 give me a "cannot convert 'MessWert' to 'double' in initialisation"
The row 113 gives "missing template rguments before 'werte'"
The row 114 gives "'werte' was not declared in this scope"
The row 115 gives "cannot convert 'const int' to 'const MessWert' for argument '1' to 'void ausgeben const MessWert*. int)'"

This is my 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 #include <cassert>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

struct MessWert
{
	double wert;
	string einheit;
};

void ausgeben(MessWert const *messwert, int format)
{
	assert(messwert);

	cout << messwert->wert << ' ' << messwert->einheit;

	switch (format) {
	case 1:
		cout << std::endl;
		break;
	case 2:
		cout.put(' ');
		break;
	case 3:
		cout << ' ' << endl;
		break;
	}
}

int normieren(MessWert &messwert)
{
	if (messwert.einheit == "A" || messwert.einheit == "V" ||
		messwert.einheit == "W")
	{
		return 0;
	}

	if (messwert.einheit.length() != 2 || messwert.einheit[1] != 'A' &&
	    messwert.einheit[1] != 'V' && messwert.einheit[1] != 'W')
	{
		return 42;
	}

	switch (messwert.einheit[0]) {
	case 'n':
		messwert.wert *= 0.000000001;
		break;
	case 'u':
		messwert.wert *= 0.000001;
		break;
	case 'm':
		messwert.wert *= 0.001;
		break;
	case 'k':
		messwert.wert *= 1000.;
		break;
	case 'M':
		messwert.wert *= 1000000;
		break;
	default:
		return 42;
	}

	messwert.einheit = messwert.einheit.substr(1);
	return 0;
}

int leistung(MessWert const &operand1, MessWert const &operand2,
	MessWert &ergebnis)
{
	MessWert foo{ operand1 };
	MessWert bar{ operand2 };

	if (normieren(foo) || normieren(bar))
		return 42;

	if (!(foo.einheit == "V" && bar.einheit == "A") &&
	    !(foo.einheit == "A" && bar.einheit == "V"))
	{
		return 42;
	}

	ergebnis.wert = foo.wert * bar.wert;
	ergebnis.einheit = "W";
	return 0;
}

int main()
{

	MessWert a{ 20, "uA" };
	MessWert b{ 10, "kV" };

	ausgeben(&a, 2);
	ausgeben(&b, 1);

	MessWert c{ a };
	normieren(c);
	ausgeben(&c, 2);

	MessWert d{ b };
	normieren(d);
	ausgeben(&d, 1);

	MessWert e;
	if (leistung(a, b, e)) {
		cerr << "Leistung konnte nicht berechnet werden :(\n\n";
		return 42;
	}

	vector<MessWert>  werte{ a, b, c, d, e };
	for (auto const &w : werte)
		ausgeben(&w, 1);
}
What compiler are you using?

https://ideone.com/burVmO

I suspect you're simply feeding modern C++ to an old compiler that doesn't understand C++11/14/17
Last edited on
This error seems to be compiler-specific.

Whein I compile with http://cpp.sh, I get these errors, but at http://coliru.stacked-crooked.com/a/608bfd04f3017e68 it runs fine.

It runs definitively without errors at g++ 7.4.0 and clang++ 6.0.0

Apparently, shortly after C++14 was finalized, this error was addressed (not being able to use the copy constructor correctly).
https://stackoverflow.com/questions/31776359/explicit-copy-constructor-and-uniform-initialization/31776937

I guess cpp.sh's compiler doesn't include the fix. Its about page says "The system uses GCC 4.9.2", but the SO post says GCC 5.2.0 (and 6.0.0 trunk) seems to implement the resolution in the defect report.
Last edited on
Topic archived. No new replies allowed.