Vector in class

Can anybody help me straighten this code out? I really need it to work and I am stuck. How can I write it so it works?

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
 class Bank {

public:
	Bank(int m_size): konton(m_size, 0) {}
    	unsigned int getSize() const { return konton.size(); }
	Bank(const Bank& _konton,) : konton(_konton){}
	Bank(const Bank &rhs) : konton(rhs.konton) {}
	Bank operator=(const Bank &rhs)
	{
		if (&rhs == this)
		{
			return *this;
		}
		
		konton = rhs.konton;
		return *this;		
	}
	
	void SetInfo(const int _konton)
	{
		konton = _konton;
	}

	int Konton() const { return konton; }
	
private:
	vector<Bank> konton;
};

#endif 
closed account (EwCjE3v7)
I have added a default constructor, and have edited a bit of the code, the pieces are commented.

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
class Bank {
public:
        Bank() = default; // default contrcutor
	Bank(int m_size): konton(m_size, 0) {}
	Bank(vector<Bank> &_konton) : konton(_konton){} // I'm sure you meant vector<Bank> instead of Bank
	Bank(const Bank &rhs) : konton(rhs.konton) {}

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

		konton = rhs.konton;
		return *this;
	}

	void SetInfo(const vector<Bank> _konton) // You are taking a vector<Bank>, not an int
	{
		konton = _konton;
	}

	vector<Bank> Konton() const { return konton; } // You are returning a vector<Bank> not an int
        vector<Bank>::size_type getSize() const { return konton.size(); }


private:
	vector<Bank> konton;
};
Last edited on
Just to add something. Im Also from Sweden @OP. Start programming in English. Do not program in any other language. Unless absolutely necessary. Which its not most of the time/ever.
Thanks man! Yeah you are right. English is preferable when programming but thats to my lessons in cpp and we have them in Swedish. Thanks for helping me. Ive done classes before but not with vectors and couldnt find any examples on the net. Thank you TarikNeaj!!

/ P
My lessons were not in English either, which I thought was Stupid as hell. The terms and syntax were spoken in Swedish, but that didint help jack shit cuz u cant google them in Swedish. I wish universities didint do these dumb stuff.
Last edited on
A tiny little question about the logic:
1
2
3
4
5
6
7
8
9
class Bank {
  vector<Bank> konton;
  // other code
};

int main() {
  Bank foo(7);
  return 0;
}

What did we do? We create a Bank "foo". The foo has one "konton" that contains 7 Banks. Each of those Banks is initialized with value 0, so each of them should have a "konton" vector of Banks, although the size of those vectors ought to be 0.

The methods of your Bank do not allow it, but in principle you could have a Bank that has Banks, which have Banks, which have Banks, which ...

At the same time, the only thing that a Bank can have is Banks. Nothing else. Economic bubble.

How can I write it so it works?

Define "works". We have no idea what the structure should achieve.
I have a class Konto (accounts in English) and want to add several accounts in vector<Bank>&konton in Bank.cpp - I can´t get it to work and I don´t 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
//Konto.h
// contains information for account (konto in swedish)

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::skrivut(vector<Konto>&kontoClass);

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

//Bank.h
//contains a vector with several accounts when added in Bank.cpp

class Bank {
public:
	Bank() = default; // default contrcutor
	Bank(int m_size): konton(m_size, 0), antal_konton(0) {}
	Bank(vector<Bank> &_konton, int _antal_konton) : konton(_konton), antal_konton(_antal_konton) {} // I'm sure you meant vector<Bank> instead of Bank
	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<Bank> _konton, int _antal_konton) // You are taking a vector<Bank>, not an int
	{
		konton = _konton;
		antal_konton = _antal_konton;
	}

	void Bank::nytt_konto(vector<Bank>&Konton);

	vector<Bank> Konton() const { return konton; } // You are returning a vector<Bank> not an int
    vector<Bank>::size_type getSize() const { return konton.size(); }


private:
	vector<Bank> konton;
	int antal_konton;
};

//Bank.cpp - Function Definitions
#include "Bank.h"

	// Lägger till ett nytt konto
	// Adds new konto (account) to bank konton (accounts) vector - several account in a bank vector
	void Bank::nytt_konto(vector<Bank>&konton)
	{
		// This get me error message C2082
		Konto* konton = new Konto();

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

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

		// Flush makes sure that the buffer is cleared and the characters are written to their destination
		cout << "Skriv namn på innehavare: " << flush << endl;
		
		// Enter values into vector
		cin >> innehavare;

		cout << "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 << "Skriv in räntesatsen: " << flush << endl;
		
		// Enter values into vector
		cin >> rantesats;

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

		cout << endl;
	}
I don´t know what I´m doing wrong.
1
2
3
4
5
6
7
8
9
10
11
12
struct Bar;

struct Foo {
  vector<Foo> legion; // legion contains Foos, not Bars. Why?

  void gaz( vector<Foo> & legion ) // masks member variable Foo::legion
  {
    Bar * legion; // masks function parameter legion and member variable Foo::legion
    legion.push_back( Bar(42) ); // pointer does not have push_back()
  }

};

Your should read and learn to interprete those compiler messages.
Thanks Keskiverto! I´ve tried to implement it but still no working result. I have sen Foo and Bars in a lot of examples but don´t know what they are or how to use them. I don´t know how to implement this in the code above so it works. I really am trying but it is so hard.
The foo, bar, gaz are just made up names used in example. The code that I did post is your code with your errors.

First, your code says that a bank has banks. Your bank has no accounts. Would it be more logical, if a bank had accounts?
1
2
3
class Bank {
  std::vector<Konto> konton;
}


What is the "nytt_konto" supposed to do? Would it be intuitive, if the member function of a Bank creates a new account into that bank and to nowhere else?
1
2
3
4
5
6
7
8
9
void Bank::nytt_konto() {
  int nummer;
  std::string innehavare;
  int saldo;
  int rantesats;
  if ( std::cin >> nummer >> innehavare >> saldo >> rantesats ) {
    konton.emplace_back( nummer, innehavare, saldo, rantesats );
  }
}



PS. The std::endl calls std::flush, so separate flushing is unnecessary.

PS2. I do believe that in banking you have to record each transaction. In a transaction assets are transferred from one account to an another, so each transaction modifies two accounts (which may be in same or different banks). In your code you do allow setting arbiratry saldo to a new account. Where did that money come from? Can I open an account in your bank with a starting saldo of ten billion? I would withdraw the assets immediately in order to not burden your bank too much ...


Oops. That gets us back to the Bank::nytt_konto. It reads the saldo and rantesats as int. The Konto, however expects double values. That is a type mismatch.
1
2
3
4
5
6
7
8
9
void Bank::nytt_konto() {
  int nummer;
  std::string innehavare;
  double saldo;
  double rantesats;
  if ( std::cin >> nummer >> innehavare >> saldo >> rantesats ) {
    konton.emplace_back( nummer, innehavare, saldo, rantesats );
  }
}



PS3. Do not use floating point values (double) with currency. Floats are not accurate. If your account already has a centillion and and give you another centillion one krone at a time, you will still have just one centillion due to how doubles behave. On the other hand, negative saldo should not be possible, so an unsigned integral type is more logical than an unsigned.
Last edited on
Well it is for my school, I really appreciate the help.

I have made corrections but there is still some compiling problems that constantly repeats and I can´t get i sorted out. I can´t find out how the error messages can be dealt with.

bank.h(18): error C2065: 'Konto' : undeclared identifier
bank.h(18): error C2923: 'std::vector' : 'Konto' is not a valid template type argument for parameter '_Ty'

// Bank.h

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#pragma once
#include "Konto.h"

#ifndef BANK_H
#define BANK_H

using namespace std;

class Bank {
public:
Bank(): konton(), antal_konton(0) {}
Bank(vector<Konto> &_konton, int _antal_konton) : konton(_konton), antal_konton(_antal_konton) {} // I'm sure you meant vector<Bank> instead of Bank
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) // You are taking a vector<Bank>, not an int
{
konton = _konton;
antal_konton = _antal_konton;
}

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

vector<Konto> Konton() const { return konton; } // You are returning a vector<Bank> not an int
vector<Konto>::size_type getSize() const { return konton.size(); }
int Antal_konton() const { return antal_konton; }

private:
std::vector<Konto> konton;
int antal_konton;
};

#endif

Your inclusion guards are in odd place. That might explain why class Konto is unknown even though you do include the Konto.h, which should define the class Konto.

What is antal_konton? The vector konton knows its size.

Copying a Bank is trivial and therefore you should not write the copy constructor at all.
Topic archived. No new replies allowed.