-1

I am a intro student to programming and learning C++ currently. I am working on a project that I need to build a main program that will pass parameters into two other classes to execute the code.

So I have three files:

main.cpp ( this has #include "bank.cpp", #include "bank.cpp"

bank.cpp ( this has nothing)

bank.hpp (this one has #include "bank.cpp" )

So now in the main.cpp I want to be able to call an instance of the bank and then add money to it. So I have something like:

bank.addMoney (which bank.addMoney is in bank.cpp)

Also the instance of the bank is in the hpp file which is like:

class bank{
    private:
        bool isBroken = false;
        double savings = 0;

    public:
}

When I compile the main program I get this error:

‘bank’ does not name a type
 bank.defaultsettings(bool isBroken, double savings){

And then list that for all my .functions, Any guidance in what I did wrong would be appreciated.

MAIN FUNCTION:

#include <iostream>
using namespace std;

#include "bank.hpp"
#include "bank.cpp"

int main(){
    cout << "Let's save some money!!!" << endl;
    //bank.addMoney(isBroken, 10);
    cout << "You've added $10to the bank" << endl;
}
CooperH
  • 66
  • 7
  • 5
    Never `#include` `.cpp` files. – user0042 Nov 24 '17 at 18:12
  • What's your `main` function? – Exercise To The Reader Nov 24 '17 at 18:13
  • They asked us to include a header gaurd in the .hpp file, is that not what the #include is? – CooperH Nov 24 '17 at 18:13
  • main Function has been added to the original post – CooperH Nov 24 '17 at 18:14
  • 2
    That's not what header guard is... – HolyBlackCat Nov 24 '17 at 18:15
  • `.cpp` files generally include any headers necessary to define what they need to interface with or what they need to implement. `.h` files *do not* include `.cpp` implementations. That wrecks everything. – tadman Nov 24 '17 at 18:15
  • You didn't initialise a `bank` object. You can't change a member variable of an object that doesn't exist. Also it seems that `bank::addMoney` is not declared in the header file – Exercise To The Reader Nov 24 '17 at 18:16
  • So the main program needs the include of just the .hpp file, and the .hpp files needs the include of the .cpp file. Then they would all communicate – CooperH Nov 24 '17 at 18:18
  • 2
    No, you _do not_ include the `.cpp` file. Not from a `.h` file and not from another `.cpp` file.It really sounds like you need to get with your instructor or a TA and get some clarification and direction. – Captain Obvlious Nov 24 '17 at 18:19
  • @Nick _"Then they would all communicate "_ They are _"stiched"_ together in the linking phase. See [How does the compilation/linking process work?](https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work) please. – user0042 Nov 24 '17 at 18:24
  • `>>bank.cpp ( this has nothing)` where is `addMoney` method? – Killzone Kid Nov 24 '17 at 18:30

1 Answers1

1

The conventional structure looks more like this where you have a header file that defines how your bank class works:

// bank.h
class bank {
  // ...
};

Then you have an implementation file that's used to store code relevant to how bank can be initialized, copied, and other details as to how it works internally:

// bank.cpp
#include "bank.h"

bank::bank() : savings(0.0) {
  // ...
}

Keep in mind this code is largely irrelevant in any context other than this file. In another .cpp file the only thing that code needs to know is the method signatures, the implementation doesn't matter, as the signatures define how objects are created and how functions are called. So long as that's established, everything can be compiled properly.

In a sense the bank.h file from the perspective of bank.cpp is a list of things you've got to implement. Anything declared there must be implemented even if that implementation is just a stub and does nothing.

Then for executable programs, not libraries, you also have a main file:

// main.cpp
#include "bank.h"

int main(int argc, char**argv) {
  bank b;
  // ...
}

Note that main references the definition but not the implementation of the bank class. The implementation is added to your executable by the linker in a final pass by combining the compiled main.o and bank.o files together.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Ok that makes more sense so I just didn't need the include .cpp file. So now I have this error: error: ‘bank’ does not name a type, which is coming from the .cpp file where I have the like bank.addMoney(); – CooperH Nov 24 '17 at 18:23
  • @Nick You need to include your `bank.h` in your `bank.cpp` file to ensure it knows what it's implementing. I've updated this answer with more detail. – tadman Nov 24 '17 at 18:25
  • Ok thanks for the help. So then instead of doing like bank.addMoney. I should do like b.addMoney? – CooperH Nov 24 '17 at 18:27
  • `bank` is your type. `b` is a variable of type `bank`. This can lead to confusion, which is why many C++ code bases often steer towards using either capital names like `Bank` for classes, or using namespace prefixes like `app` or whatever your app is called where the final type is `app::bank`. In those cases you could do `Bank bank` or `app::bank bank`. – tadman Nov 24 '17 at 18:28
  • `bank.addMoney()` is theoretically a valid function call, but since `bank` is a type then `addMoney` needs to be a `static` method. That's not what you want here, but it can be useful in other situations. – tadman Nov 24 '17 at 18:30
  • Guess I should re read up on some of this. So in main Function I did b.addMoney(10); then in the hpp file I need to have a public member that is addMoney. then it will go into the .cpp file and look for the bank.addMoney() and then execute>? – CooperH Nov 24 '17 at 18:31
  • C++ is a notoriously tricky language especially when you start to use features well beyond what C provides, so you'll need a good introduction and reference guide to the language so you don't drown in the complexity. A good place to start is the [book by the language's designer](http://www.stroustrup.com/4th.html) but there are many others [that get into other aspects of the language](https://www.aristeia.com/books.html). You're asking good questions, but it's a lot to try and explain in a small comment here. – tadman Nov 24 '17 at 18:57
  • so I actually think I figured it all out fixed a lot of the errors I was getting. Now I just get errors from the .cpp file. So I have everything like bank.addMoney(). but it's giving me an expected unqualified-id before ‘.’ token bank.defaultsettings(){ – CooperH Nov 24 '17 at 19:34
  • It's probably worth opening up a new question a this point with your updated code that focuses on this new problem. – tadman Nov 24 '17 at 19:37