期望头文件中有一个参数
expected a parameter in the header
我试图读取和显示一个名为myDocument.txt的文件的内容,但我总是得到下面的错误与我的头文件。
./ReadDocument.h:22:24: error: expected parameter declarator std::ifstream myflux("myDocument.txt");
怎么回事?
这是我的ReadDocument.h:
class ReadDocument{
public:
ReadDocument(); //empty constructor
ReadDocument(const std::string& fileName); //constructor
void documentContent();
friend std::ostream& operator <<(std::ostream& os, const ReadDocument& d);
std::list<std::string> myList;
std::ifstream myflux("myDocument.txt");
if(myflux){
//Getting the words from the file string by string
std::string data;
while(myflux>>data){
myList.push_back(data);
}
myflux.close();
}
else{
std::string message = "ttERROR ==> This file doesn't exist.";
throw FileDoesntExistException(message);
}
};
这是我的ReadDocument.cpp:
#include "ReadDocument.h"
ReadDocument::ReadDocument(){}
void ReadDocument::documentContent(){}
std::ostream& operator <<(std::ostream& os, const ReadDocument& d){
for(std::list <std::string> :: const_iterator it = ReadDocument::myList.begin(); it!=ReadDocument::myList.end(); it++)
std::cout<<*it<<std::endl;
return os;
}
这是usereaddocument。cpp
#include "ReadDocument.h"
int main(){
ReadDocument rd("test.txt");
std::cout<<rd<<std::endl;
return 0;
}
我试着重命名文件,仍然是同样的问题。我把它放在课堂上,没有改变任何东西。我总是得到同样的错误。是的,这是我的整个代码
类myflux
之后的部分不在方法中。
你想把代码放在函数documentContent
中,像这样吗?
ReadDocument.h:
class ReadDocument{
public:
ReadDocument(); //empty constructor
ReadDocument(const std::string& fileName); //constructor
void documentContent();
friend std::ostream& operator <<(std::ostream& os, const ReadDocument& d);
std::list<std::string> myList;
std::ifstream myflux("myDocument.txt");
};
ReadDocument.cpp:
#include "ReadDocument.h"
ReadDocument::ReadDocument(){}
void ReadDocument::documentContent(){
if(myflux){
//Getting the words from the file string by string
std::string data;
while(myflux>>data){
myList.push_back(data);
}
myflux.close();
}
else{
std::string message = "ttERROR ==> This file doesn't exist.";
throw FileDoesntExistException(message);
}
}
std::ostream& operator <<(std::ostream& os, const ReadDocument& d){
for(std::list <std::string> :: const_iterator it = ReadDocument::myList.begin(); it!=ReadDocument::myList.end(); it++)
std::cout<<*it<<std::endl;
return os;
}
顺便说一下,你应该避免创建一个空的构造函数而不填充任何东西。自c++ 11以来,您应该在头文件中写入
ReadDocument() = default;
,并从cpp中删除ReadDocument::ReadDocument(){}
。
与你的class
你是declaring
两个public
variables
,并试图给每个instance
你的类这些default
characteristics
。在default
constructor
中设置默认条件的解决方案如下:
class ReadDocument{
private:
std::list<std::string> myList; //moved member variables to private
std::ifstream myflux; //part of class
public:
ReadDocument(); //empty constructor
ReadDocument(const std::string& fileName); //constructor
void documentContent();
friend std::ostream& operator <<(std::ostream& os, const ReadDocument& d);
};
实现:#include "ReadDocument.h"
ReadDocument::ReadDocument()
{
myflux.open("myDocument.txt");
if(myflux){
//Getting the words from the file string by string
std::string data;
while(myflux>>data){
myList.push_back(data);
}
myflux.close();
}
else{
std::string message = "ttERROR ==> This file doesn't exist.";
throw FileDoesntExistException(message);
}
}
void ReadDocument::documentContent(){}
std::ostream& operator <<(std::ostream& os, const ReadDocument& d){
for(std::list <std::string> :: const_iterator it =
d.myList.begin(); it!=d.myList.end(); it++)
std::cout<<*it<<std::endl;
return os;
}
代码现在默认将myflux
与"myDocument.txt"
和extract
关联,file data
与myList
关联(除非使用string参数实例化以与const string&ReadDocument
的class
的每个instance
的构造函数)。以及ostream函数中的其他一些修复。
p。S*假设这是与ReadDocument
class
相关联的整个.cpp
文件,您忘记实现包含const
string&
parameter
, ReadDocument(const std::string& fileName)
的constructor
.
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 我怎么能有一个以2D数组为参数的函数,而数组有一个我想更改的参数/维度
- 是否可以有一个模板函数,可以将向量和非向量类型都作为参数
- 此模板函数上的最后一个参数有什么用?
- 对于属性上的 NOTIFY 信号,如果我给它一个参数有什么区别?
- 为什么有一个构造函数同时接受initializer_list和一个参数包
- 如果我有一个将指针变量作为输入的函数,有没有办法将函数作为参数传递
- 为什么一个可变类模板最多只能有一个参数包
- 为什么boost::multi_array的ConstMultiArrayConcept有一个NumDims模板参数?
- 有一个可以是指针类型或非指针类型的模板参数
- 是否有一个WinAPI可以从带有可选空格和其他参数的命令行获取文件名
- C#:Cdecl-DllExport,参数中有一个指向类实例的指针
- 为什么make_unique有一个额外的移动,可以将 std::bind 作为参数的构造函数
- 有形状和圆形类,以及点类.创建将点类作为一个参数的圆时出现分割错误
- 我有一个成员,该成员是指向参数化函数的指针,我可以扩展参数类型吗?
- 当你有一个像 ::lower 这样的参数时,这在C++中是什么意思
- 期望头文件中有一个参数
- 模板类构造函数,在第二个模板中有一个参数引发错误"constant"
- MSB3305:正在处理的COM引用-至少有一个参数不能被运行时封送处理程序封送