Class C++;我的所有文件都没有类型
所以我把我的文件分成几部分,现在我得到了一堆类型错误。例如:Class C++;我的所有文件都没有类型,class,object,types,Class,Object,Types,所以我把我的文件分成几部分,现在我得到了一堆类型错误。例如: phoneNUmber.h'istream没有命名类型' 这到底是什么意思?这是我的档案,很短 Main #include <iostream> #include <fstream> #include <string> #include "phoneEntry.h" using namespace std; int main() { PhoneEntry entry; ifs
phoneNUmber.h'istream没有命名类型'
这到底是什么意思?这是我的档案,很短
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;
}
#包括
#包括
#包括
#包括“phoneEntry.h”
使用名称空间std;
int main()
{
电话输入;
ifstream filezilla;
filezilla.open(“phone.txt”);
如果(filezilla)
{
而(!filezilla.eof()&&entry.readEntry(cin))
{
录入、书写(cout);
}
}
其他的
{
cout firstName>>lastName;
电话号码(Sin);
归罪;
};
ostream&PhoneEntry::writeEntry(ostream&Sout)常量
{
常数int num=28;
int fill=num-((firstName+lastName).length());
Sout>后缀;
归罪;
};
ostream和PhoneNumber::writeNumber(ostream和Sout)常量
{
苏特
这到底是什么意思
这意味着编译器认为ifstream
是一个变量名,没有关联的类型。它认为这是因为由于缺少正确的include文件,因此没有为它提供ifstream
的类定义:
#include <iostream>
#include <fstream>
#include <string>
#包括
#包括
#包括
此外,在PhoneEntry.cpp和PhoneNumber.cpp中
每个地方都有一个istream
它应该是std::istream
与ostream
和ifstream
等相同
在main.cpp
中,您有以下语句using namespace std;
,它将std
名称空间导入全局名称空间,因此在该文件中,您不必为所有iostream调用预加名称空间std::
由于将内容拆分,每个新cpp文件的全局名称空间中不再有std::
,因此您必须自己完成
您可以在cpp文件中使用名称空间std;
添加,但我通常不这样做。您是否尝试过将#include
放入PhoneNumber.h文件中?但我将它从文件中包含在main()中所以它应该向下延伸到phoneNumer,对吗?它是到phoneEntry到phoneNumer,所以我不应该需要它,对吗?我的C++fu很弱,但问你为什么需要它是一个很好的后续问题。当编译器编译phoneNumber.cpp
时会发生什么?当编译器看到这一点时,它完全忽略了你的主文件。明白了吗所以它不会继承它。
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;
};
#include <iostream>
#include <fstream>
#include <string>