Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在同一对象的成员之间传递ifstream变量,C++;_C++_File_Class_Object_Member - Fatal编程技术网

C++ 在同一对象的成员之间传递ifstream变量,C++;

C++ 在同一对象的成员之间传递ifstream变量,C++;,c++,file,class,object,member,C++,File,Class,Object,Member,目标:使用类变量,以便同一对象的以下成员可以使用对象成员中声明的ifstream,而无需使用函数头参数传递 问题:所创建对象测试的本地ifstream未在该对象的第二个成员中重复使用。我一定是设置错误了,我该如何修复它 类和文件现在对我来说就像爬山一样,但我甚至找不到第一个立足点——让该死的变量工作起来!我环顾网络太久了,但所有的例子都很复杂,我只想有一些基本的工作开始修补。我敢肯定,我错过了一件非常简单的事情,真是令人沮丧>:[ main.cpp #include "file.h #inclu

目标:使用类变量,以便同一对象的以下成员可以使用对象成员中声明的ifstream,而无需使用函数头参数传递

问题:所创建对象测试的本地ifstream未在该对象的第二个成员中重复使用。我一定是设置错误了,我该如何修复它

类和文件现在对我来说就像爬山一样,但我甚至找不到第一个立足点——让该死的变量工作起来!我环顾网络太久了,但所有的例子都很复杂,我只想有一些基本的工作开始修补。我敢肯定,我错过了一件非常简单的事情,真是令人沮丧>:[

main.cpp

#include "file.h
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    file test;
    test.file_pass();
    return 0;
}
#include "file.h"

//**********************************
//This will read the file.
file::file()
{
    ifstream stream("Word Test.txt");
}

//**********************************
//This will output the file.
void file::file_pass()
{
   //ifstream stream("Word Test.txt"); //if line activated, program works fine of course.
    string line;
    while(getline(stream, line))
            cout << line << endl;
}
#包括“file.h
#包括
#包括
使用名称空间std;
int main()
{
文件测试;
test.file_pass();
返回0;
}
file.h

#ifndef FILE_H
#define FILE_H
#include <fstream>
#include <iostream>

using namespace std;

class file
{
    public:

        file();
        void file_pass();

    //private:
        ifstream stream;
};

#endif
\ifndef文件
#定义文件
#包括
#包括
使用名称空间std;
类文件
{
公众:
文件();
无效文件_pass();
//私人:
ifstream;
};
#恩迪夫
file.cpp

#include "file.h
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    file test;
    test.file_pass();
    return 0;
}
#include "file.h"

//**********************************
//This will read the file.
file::file()
{
    ifstream stream("Word Test.txt");
}

//**********************************
//This will output the file.
void file::file_pass()
{
   //ifstream stream("Word Test.txt"); //if line activated, program works fine of course.
    string line;
    while(getline(stream, line))
            cout << line << endl;
}
#包括“file.h”
//**********************************
//这将读取该文件。
file::file()文件
{
ifstream流(“Word Test.txt”);
}
//**********************************
//这将输出文件。
void file::file_pass()
{
//ifstream(“Word Test.txt”);//如果行被激活,程序当然可以正常工作。
弦线;
while(getline(流,行))

cout这里您正在创建一个与类成员同名的新局部变量:

file::file()
{
    ifstream stream("Word Test.txt");
}
相反,您可以使用它来初始化构造函数中的类成员:

file::file() : stream("Word Test.txt")
{
}

在这里,您将创建一个与类成员同名的新局部变量:

file::file()
{
    ifstream stream("Word Test.txt");
}
相反,您可以使用它来初始化构造函数中的类成员:

file::file() : stream("Word Test.txt")
{
}

当我的所有的投票和回答问题都在9分钟内被选中,当堆栈溢出让我知道:“我知道这是简单的,我在9分钟内丢失了HaTakes所有的投票和一个回答问题的检查标记,当我的堆栈溢出时:”我知道这是一个简单的我错过了HaHopaApple你可以阅读/学习关于C++之前的ASKI你可以先读一下C++,然后再问一下。