So I split my files into a few pieces and now i’m getting a bunch of type errors. Such as:
phoneNUmber.h 'istream does not name a type'
What’s this mean exactly? Here’s my files they’re pretty short.
Main
#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;
int main()
{
PhoneEntry entry;
ifstream filezilla;
filezilla.open("phone.txt");
if(filezilla)
{
while(!filezilla.eof() && entry.readEntry(cin))
{
entry.writeEntry(cout);
}
}
else
{
cout << "Four Oh Four - File Not Found" << endl;
}
return 0;
}
Phone Entry
#include "phoneNumber.h"
class PhoneEntry
{
private:
PhoneNumber phone;
string firstName,
lastName;
void _writeDots(ostream& fout, int n) const;
public:
istream& readEntry(istream&);
ostream& writeEntry(ostream&) const;
};
istream& PhoneEntry::readEntry(istream& Sin)
{
Sin >> firstName >> lastName;
phone.readNumber(Sin);
return Sin;
};
ostream& PhoneEntry::writeEntry(ostream& Sout) const
{
const int num = 28;
int fill = num - ((firstName + lastName).length());
Sout << firstName << ", " << lastName;
_writeDots(Sout, fill);
phone.writeNumber(Sout);
return Sout;
};
void PhoneEntry::_writeDots(ostream& fout, int n) const
{
if(n % 2)
{
fout << ".";
}
for(int i = 0; i < n; i++)
{
fout << " .";
}
};
PhoneNumber
class PhoneNumber
{
private:
int areaCode,
prefix,
suffix;
public:
istream& readNumber(istream&);
ostream& writeNumber(ostream&) const;
};
istream& PhoneNumber::readNumber(istream& Sin)
{
Sin >> areaCode >> prefix >> suffix;
return Sin;
};
ostream& PhoneNumber::writeNumber(ostream& Sout) const
{
Sout << " " << areaCode << "-" << prefix << "-" << suffix << endl;
return Sout;
};
It means that the compiler thinks
ifstreamis a variable name, with no associated type. It thinks this, because it hasn’t been given the class definition forifstreamdue to you missing the proper include files:Additionally, in PhoneEntry.cpp and PhoneNumber.cpp
everyplace you have a
istreamit should bestd::istreamSame with
ostreamandifstreametc.In your
main.cppyou have the following statementusing namespace std;which imports thestdnamespace into the global namespace, thus inside that file, you don’t have to prepend the namespacestd::to all the iostream calls.Since you split things out, each new cpp file no longer has the
std::in it’s global namespace, thus you have to do it yourself.You could add
using namespace std;inside your cpp files, but I usually don’t.