Function call from a class

Write your question here.

Whn i call this function in Bankaccounts.cpp - file from my main nothing happens. The compiler doesn´t show any errors. Can anybody help, I´dont know what I´m doing wrong.

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

// Prg2_Delmoment3_v003.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "Bankaccount.h"

using namespace std; 

int main()
{	
	while(true)
	{
	
// Creates a new account
		cout << "1 - Skapa ett nytt konto" << endl;

		int val;
		cin >> val;

		switch(val)
		{
			case 1:
				cout << endl << "L\x84gg in ett nytt konto" << endl;
				Bank nytt_konto();
			break;

            }

	return 0;
}

// Bankaccount.h - file

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

#ifndef BANKACCOUNTS_H
#define BANKACCOUNTS_H

using namespace std; 

//Konto klassen
class Konto
{
public:
	Konto() : nummer(0), innehavare(""), saldo(0), rantesats(0) {}
	Konto(const int _nummer, string &_innehavare, double _saldo, double _rantesats) : nummer(_nummer), innehavare(_innehavare), saldo(_saldo), rantesats(_rantesats) {}
	Konto(const Konto &rhs) : nummer(rhs.nummer), innehavare(rhs.innehavare), saldo(rhs.saldo), rantesats(rhs.rantesats) {}
	Konto operator=(const Konto &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		nummer = rhs.nummer;
		innehavare = rhs.innehavare;
		saldo = rhs.saldo;
		rantesats = rhs.rantesats;

		return *this;
		
	}
	
	//Metod: Sets the necessary info
	void SetInfo(const int _nummer, string &_innehavare, double _saldo, double _rantesats)
	{
		nummer = _nummer;
		innehavare = _innehavare;
		saldo = _saldo;
		rantesats = _rantesats;
	}

	// Acessor functions
	
	int Nummer() const { return nummer; }
	const string &Innehavare() const { return innehavare; }
	double Saldo() const { return saldo; }
	double Rantesats() const { return rantesats; }

//Member variables
private:
	int nummer;
	string innehavare;
	double saldo;
	double rantesats;
};

//Bankklassen

class Bank {
public:
	vector<Konto>konton;
	Bank(): konton(), antal_konton(0) {}
	Bank(vector<Konto> &_konton, int _antal_konton) : konton(_konton), antal_konton(_antal_konton) {} 
	Bank(const Bank &rhs) : konton(rhs.konton), antal_konton(rhs.antal_konton)  {}

	Bank operator=(const Bank &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}

		konton = rhs.konton;
		antal_konton = rhs.antal_konton;
		return *this;
	}

	void SetInfo(const vector<Konto> _konton, int _antal_konton)
	{
		konton = _konton;
		antal_konton = _antal_konton;
	}

	void Bank::nytt_konto(vector<Konto>&konton);

	vector<Konto> Konton() const { return konton; }
    vector<Konto>::size_type getSize() const { return konton.size(); }
	int Antal_konton() const { return antal_konton; }
	
private:
	int antal_konton;
}; 

#endif


#include "Bankaccount.h"

	// L\x84gger till ett nytt konto
	void Bank::nytt_konto(vector<Konto>&konton)
	{
		string fornamn;
		string efternamn;

		int nummer;
		string innehavare;
		double saldo;
		double rantesats;

//Write in an account number
		cout << "Skriv in kontonummer: " << flush << endl;
		
		// Enter values into vector
		cin >> nummer;

		for(unsigned int i=0; i < konton.size(); i++)
		{
			if (nummer == konton[i].Nummer())
			{
				cout << "Konto existerar redan." << endl << endl;
				return;
			}
		}

		// Flush makes sure that the buffer is cleared and the characters are written to their destination

//Enter name
		cout << "Skriv namn p\x86 innehavare: " << flush << endl << endl;
		
		// Enter values into vector
//Enter name - firstname
		cout << "Skriv in f\x94rnamn: " << endl;

		cin >> fornamn;

		cout << "Skriv in efternamn: " << endl;
//Enter name - lastname
		cin >> efternamn;

		innehavare = fornamn + " " + efternamn;

//Enter balance in account			
		cout << endl << "Skriv in saldot: " << flush << endl;
		
		// Enter values into vector
		cin >> saldo;

		// Flush makes sure that the buffer is cleared and the characters are written to their destination
		cout << endl << "Skriv in r\x84ntesatsen: " << flush << endl;

//Enter interest account has
		// Enter values into vector
		cin >> rantesats;

		cout << endl;
		cout << "Nytt konto skapat:" << endl;
		cout << "Kontonummer: " << nummer << endl;
		cout << "Innehavare: " << innehavare << endl;
		cout << "Saldo: " << saldo << endl;
		cout << "R\x84ntesats: " << rantesats << endl << endl;

		
		konton.push_back(Konto(nummer, innehavare, saldo, rantesats));
} 
Bank nytt_konto(); is a declaration of a function named nytt_konto, that returns a Bank and takes no arguments.

To call the void Bank::nytt_konto(vector<Konto>&) function you should first create a bank object somewhere in main (before the loop if you don't want a new bank each loop iteration).
1
2
// Creates a Bank variable named bank.
Bank bank;

You have declared the nytt_konto function to take a vector as argument. Are you sure that is not a mistake? Shouldn't you use the Bank::konton member variable instead?

If you remove the parameter from the nytt_konto function
 
void Bank::nytt_konto(vector<Konto>&konton)
you will be able to call the function like this:
 
bank.nytt_konto();
Thank you very much Peter87 - That solved my problem, I really appreciate your touch to it. I have tried for a week now and started to Think it was impossible to do this way. Thank you again!

/ P
Topic archived. No new replies allowed.