Ich habe heut etwas mit variablen FUnktionsparameterm herumgespielt und bekomme es nicht hin , float parameter variabel zu übergeben!
Hier meine header Datei:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
|
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <Windows.h>
using namespace std;
|
Hier die main.cpp mit float:
|
C-/C++-Quelltext
|
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
|
#include "Main.h"
float addiere(int anzahl, ...);
int main() {
cout << "#### Vectoren, Try & Catch, Variable Funktionen ####" << endl;
vector<float> v1(3);
vector<float> v2(3);
vector<float>::iterator i;
v1[0] = 0.3;
v1[1] = 0.4;
v1[2] = 0.5;
v2[0] = 3.2;
v2[1] = 5.9;
v2[2] = 12.31;
// Fehler abfangen falls bei Vektoroperationen etwas schief laeuft
//Try catch ist fuer bewusste Fehler, throws und nicht fuer Debugging
try {
cout << "Inhalt von Vektor nr. 1:" << endl << endl;
string vorlage = "Wert %i: %f";
int counter = 0;
for(i = v1.begin(); i< v1.end(); i++)
{
cout << "Inhalt von Element nr. " << counter << ":" << *i << endl;
counter++;
}
cout << "Erzeugen wir eine exception, Zugriff auf element 3!";
cout << "Element 3: " << v1.at(3);
} catch(exception &e) {
cout << endl << "Fehler: " << e.what() << endl;
cout << "Typ: " << typeid (e).name() << endl;
}
cout << "1+2 = " << addiere(2,static_cast<float>(1),static_cast<float>(2));
system("Pause");
}
float addiere(int anzahl, ...) {
va_list arguments;
float sum = 0;
float in;
va_start(arguments,anzahl);
for(int x = 0; x < anzahl; x++)
{
in = va_arg(arguments, float);
sum += in;
cout << "Dyn Arg: " << in << endl;
}
va_end(arguments);
return sum;
}
|
Erzeugt die Ausgabe:
#### Vectoren, Try & Catch, Variable Funktionen ####
Inhalt von Vektor nr. 1:
Inhalt von Element nr. 0:0.3
Inhalt von Element nr. 1:0.4
Inhalt von Element nr. 2:0.5
Erzeugen wir eine exception, Zugriff auf element 3!
Fehler: invalid vector<T> subscript
Typ: class std::out_of_range
Dyn Arg: 0
Dyn Arg: 1.875
1+2 = 0Drücken Sie eine beliebige Taste . . .
Wenn ich nun das ganze in double ändere: main.cpp:
|
C-/C++-Quelltext
|
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
|
#include "Main.h"
double addiere(int anzahl, ...);
int main() {
cout << "#### Vectoren, Try & Catch, Variable Funktionen ####" << endl;
vector<float> v1(3);
vector<float> v2(3);
vector<float>::iterator i;
v1[0] = 0.3;
v1[1] = 0.4;
v1[2] = 0.5;
v2[0] = 3.2;
v2[1] = 5.9;
v2[2] = 12.31;
// Fehler abfangen falls bei Vektoroperationen etwas schief laeuft
//Try catch ist fuer bewusste Fehler, throws und nicht fuer Debugging
try {
cout << "Inhalt von Vektor nr. 1:" << endl << endl;
string vorlage = "Wert %i: %f";
int counter = 0;
for(i = v1.begin(); i< v1.end(); i++)
{
cout << "Inhalt von Element nr. " << counter << ":" << *i << endl;
counter++;
}
cout << "Erzeugen wir eine exception, Zugriff auf element 3!";
cout << "Element 3: " << v1.at(3);
} catch(exception &e) {
cout << endl << "Fehler: " << e.what() << endl;
cout << "Typ: " << typeid (e).name() << endl;
}
cout << "1+2 = " << addiere(2,static_cast<double>(1),static_cast<double>(2));
system("Pause");
}
double addiere(int anzahl, ...) {
va_list arguments;
double sum = 0;
double in;
va_start(arguments,anzahl);
for(int x = 0; x < anzahl; x++)
{
in = va_arg(arguments, double);
sum += in;
cout << "Dyn Arg: " << in << endl;
}
va_end(arguments);
return sum;
}
|
Kommt eine sinnvolle Ausgabe zustande:
#### Vectoren, Try & Catch, Variable Funktionen ###
Inhalt von Vektor nr. 1:
Inhalt von Element nr. 0:0.3
Inhalt von Element nr. 1:0.4
Inhalt von Element nr. 2:0.5
Erzeugen wir eine exception, Zugriff auf element 3!
Fehler: invalid vector<T> subscript
Typ: class std::out_of_range
Dyn Arg: 1
Dyn Arg: 2
1+2 = 3Drücken Sie eine beliebige Taste . . .
Hat jemand eine Idee woran das liegen kann ?