-2

I can´t get the code to be vieved here for some unknown reason so I post my code with JavaScript snippets. 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? I did put my code into JavaScript snippets but it´s not working. It is C++ code and i need a vector in a class.

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
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
patriic48
  • 7
  • 3
  • 2
    *How* does it not work? Do you get compiler errors (then add them)? Do you get crashes (then run in a debugger)? Unexpected results (then add the expected and actual results for some input)? Something else (then elaborate)? – Some programmer dude Apr 02 '15 at 19:47
  • 1
    `konton = _konton` makes no sense at all. `konton` is a vector; `_konton` is an `int`. `int Konton() const { return konton; }` has the same problem, but in reverse. – WhozCraig Apr 02 '15 at 19:48
  • Though my guess is compilation errors, because the class member variable `konton` is a *vector* and you try to convert it to an `int` in the `Konton` function, and assign an `int` to the vector from the `SetInfo` function. Also the initialization in the constructor will not work, as you have a vector of `Bank` structures and you try to initialize the vector with the *integer* `0`. Finally, your two copy-constructures are exactly equal., just that the first one will not work (`_konton` is a `Bank` eference, not a vector). – Some programmer dude Apr 02 '15 at 19:48
  • Add compilation errors – Daniel Cardenas Apr 02 '15 at 20:10

1 Answers1

0

I cleaned a bit your mess and changed a bit metods, hope you gonna understand. But first of all take a look at rule of three/five/zero, because this code is mostly about it. Down below I pointed out some important things. It's not perfect, but should be got place to start.

class Bank {

public:
    //
    // Default constructor
    // You don't need to define size of vector
    Bank(): konton() {}
    // Copy constructor
    Bank(const Bank& other)
            : konton(vector<int> (other.konton)) {
    }
    // Copy assignment operator
    Bank operator=(const Bank &rhs)
    {
        if (&rhs == this) return *this;
        konton = rhs.konton;
        return *this;
    }
    // You missing destructor ~Bank();
    //
    // Also you can add two more functions:
    //   -  Move constructor Bank(const Bank&& other);
    //   -  Move assignment operator Bank& operator=(Bank&& other);


    // This just add new element at the end of konton
    void addKonto(const int _konto)
    {
        konton.push_back(_konto);
    }
    // And this remove last
    void removeKonto()
    {
        konton.pop_back();
    }

    // Getters
    vector<int> getKonton() const { return konton; }
    unsigned int getSize() const { return konton.size(); }

private:
    // I guess you mean to store accounts numbers in the Bank class
    // To make it simple I made it int but, it can by any other your specific data type
    vector<int> konton;
};

PS. Consider using IDE with on-the-fly syntax checking.

Edit: Here you have some more about this.

Community
  • 1
  • 1
  • For some reason I dont understand my answer to your answer came up above yours. I´m new to this forum so forgive me. – patriic48 Apr 04 '15 at 12:29