Search code examples
c++classheaderfstream

How do I remove errors after creating .h and .cpp files for a class? C++


So I'm learning to use a class .h and .cpp files in my program that reads a file containing information about a bank account. Initially the code worked fine, however after creating the .h and .cpp class files, things don't work so smoothly anymore, as I'm getting strange errors that don't make sense to me.

This is my MAIN cpp file:

#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{   string fileName;
cout << "Enter the name of the data file: ";
cin>>fileName;
cout<<endl;
bankAccount object(fileName);
return 0;
}

This is my Bankaccount.h file

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <iostream>
#include <fstream>
#include <string>

class bankAccount
{
public:
    bankAccount(string n);
    bankAccount();

private:
    ifstream sourceFile;
}

And lastly this is the Bankaccount.cpp file

#include "Bankaccount.h"
#include <iostream>
#include <fstream>
#include <string>

bankAccount::bankAccount(string n)
{
    sourceFile.open(n.c_str());
}

Which is now generating these errors:

include\Bankaccount.h|13|error: expected ')' before 'n'|
include\Bankaccount.h|18|error: 'ifstream' does not name a type|
include\Bankaccount.h|14|note: bankAccount::bankAccount()|
include\Bankaccount.h|14|note: candidate expects 0 arguments, 1 provided|
include\Bankaccount.h|4|note: bankAccount::bankAccount(const bankAccount&)|
include\Bankaccount.h|4|note: no known conversion for argument 1 from       'std::string {aka std::basic_string}' to 'const bankAccount&'|

I think it might be an issue with the headers? I went a little bit crazy and put all of my relevant headers on each file trying to get it to work.


Solution

  • using namespace std;
    

    This is considered a bad programming practice, and you will do yourself a favor if you forget that this is actually a part of C++ language. Although there are proper situations where one would employ using namespace, this should be avoided until one has a much better technical understanding of C++, its structure, and its grammar; in order to recognize and understand when this can be used correctly (if at all).

    In your main() you have:

    string fileName;
    

    There is no such class in the C++ library whose name is string. The class's correct name is std::string; however by shoving using namespace std; a few lines above, you end up blissfully unaware of this basic, fundamental fact.

    Now, after you understand this, let's go back and look at your header file:

    ifstream sourceFile;
    

    Well, there's no such class in the C++ library called ifstream, either. The class's proper name is std::ifstream. All classes and templates from the C++ library exist in the std namespace.

    However, because when you #included the header file your using namespace std; alias is not yet defined, your compiler doesn't recognize the class name, and you get this compilation error as a reward.

    The solution is not to cram a using namespace std; in your header file. That will simply lead to more chaos and confusion. The proper fix is:

    1. Remove using namespace std; from your code, completely.

    2. Use full names of all classes from the C++ library, everywhere. Replace all references to string, ifstream, and everything else, with their actual class names: std::string, std::ifstream, and so on. Get into the habit of explicitly using the std namespace prefix every time. It might seem like a bother at first, but you'll quickly pick up the habit before long, and you won't think of it twice.

    And you'll never be confused by these kinds of compilation errors ever agin.