Function call from class

I try to call a function that needs an argument and I can´t figure out what argument that is. Can anybody help?

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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  // Prg2.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()
{	
	Konto konto;
	Bank bank;

	while(true)
	{
	
		cout << "1 - Skapa ett nytt konto" << endl;
		cout << "2 - Skriv ut en lista \x94ver konton" << endl;
		cout << "5 - Avsluta programmet" << endl;

		int val;

		cin >> val;

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

			case 2:
				cout << endl << "Skriv ut en lista \x94ver konton" << endl;
				konto.skriv_kontolista();
				break;
	
		}


	}

	return 0;
}

// Bankaccount

#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;
	}

	void Konto::skriv_kontolista(vector<Konto>&konton);

	// 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() const { return konton; }
    vector<Konto>::size_type getSize() const { return konton.size(); }
	int Antal_konton() const { return antal_konton; }
	
private:
	int antal_konton;
}; 

//Function Declarations

//BubbleSort - Sorterar vektorn
void BubbleSort(vector<Konto>&konton);
// @param konton - Vektorn som ska sorteras ska sorteras efter namn

//Swap - Skapar tillf\x84lliga variabler f\x94r namn
void swap(Konto &p, Konto &q);


#endif

//Bankccount.cpp - Function Definitions

#include "Bankaccount.h"

	// L\x84gger till ett nytt konto
	void Bank::nytt_konto()
	{
		string fornamn;
		string efternamn;

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

		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
		cout << "Skriv namn p\x86 innehavare: " << flush << endl << endl;
		
		// Enter values into vector
		cout << "Skriv in f\x94rnamn: " << endl;

		cin >> fornamn;

		cout << "Skriv in efternamn: " << endl;

		cin >> efternamn;

		innehavare = fornamn + " " + efternamn;
			
		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 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;

		// This get me error message C2664
		konton.push_back(Konto(nummer, innehavare, saldo, rantesats));
} 

//Skriver ut samtliga attribut
void Konto::skriv_kontolista(vector<Konto>&konton)
{		
	//Sort order, innehavare
	for (unsigned int i = 0; i < konton.size(); i++)
	{
		BubbleSort(konton);
		cout << "Kontonummer: " << konton[i].Nummer() << endl;
		cout << "Innehavare: " << konton[i].Innehavare() << endl;
		cout << "Saldo: " << konton[i].Saldo() << endl;
		cout << "R\x84ntesats: " << konton[i].Rantesats() << endl << endl;
	}
}

//Function Definitions

void BubbleSort(vector<Konto>&konton)
{
	// The outer loop, going through all list
    for(int i = 0; i < konton.size(); i++)
	{
		//Inner loop, going through element by element
		int nrLeft = konton.size() - i; // To se how many has been gone through
		for (int j = 0; j < nrLeft - 1; j++)
		{
			
			if (konton[j].Innehavare() > konton[j+1].Innehavare()) // Compare the elements
			{
				swap(konton[j], konton[j+1]); 
			}
		}
	}
}

void swap(Konto &p, Konto &q) 
{ 
	Konto temp = p; 
	p = q;
	q = temp;
}



Generates following:
Prg2.cpp: error C2660: 'Konto::skriv_kontolista' : function does not take 0 arguments
Your code has:
1
2
3
4
class Konto {
public:
  void skriv_kontolista( vector<Konto> & konton );
}:

Is it not clear what that function (written by you) requires?

However, why is that function a member of Konto? It does not access the object. It does not need access to private members either.
Exactly, the problem when I take away the vector in
1
2
3
4
class Konto {
public:
  void skriv_kontolista( vector<Konto> & konton );
}:


that i can´t use the functio size() to loop through the elements in Konto Class that are stored in vector<Konto> & konton.

vector<Konto>&konton is in Bank class and containing accounts that you add with

1
2
3
case 1:
	cout << endl << "L\x84gg in ett nytt konto" << endl;
	bank.nytt_konto();


from void Bank::nytt_konto(); in Bank class via void Bank::nytt_konto() in Bankaccount.cpp. This function is working properly and adds new accounts in the vector.

I want to print the values in the vector with the different values inserted in Bank::nytt_konto(), number, owner, balance, interest (nummer, innehavare, saldo, rantesats) with void Konto::skriv_kontolista(vector<Konto>&konton); in Bankaccount.cpp via void skriv_kontolista(vector<Konto>&konton); in Konto class and

that is case 2 in Prg2.cpp

1
2
3
4
case 2:
	cout << endl << "Skriv ut en lista \x94ver konton" << endl;
	konto.skriv_kontolista(); // Here it seems to be problems
break;


I can´t get it to work and can´t find any examples on how to do.




Topic archived. No new replies allowed.