【问题标题】:Reading from a file,using headers and cpp's从文件中读取,使用标题和 cpp
【发布时间】:2014-04-20 15:25:52
【问题描述】:

好的,所以我有一个 3 级的车辆继承树。叶类(树下的最后一个)是六个。我想创建一个从 .txt 文件中读取的函数,然后将信息分配给不同的变量(例如,该文件将包含 2006 Toyota Supra Diesel,这些将分配给 yearbrandmodelfuel_type)。实际上我设法做到了,但后来当我将代码拆分为 .h 和 .cpp 文件时,该函数停止工作。

void Cars::txt_input()
{
    getline(file,input);          //This is in the .cpp file for one of the 
    year = atoi(input.c_str());   //child classes.
    getline(file,input);
    brand = input;
    getline(file,input);
    model = input;
    getline(file,input);
    passenger_capacity = atoi(input.c_str());
    getline(file,input);
    engine_power = atoi(input.c_str());
    getline(file,input);
    max_speed = atoi(input.c_str());    
    getline(file,input);
    fuel_type = input;
    getline(file,input);
    wheel_drive = input;
    getline(file,input);
    average_cons = atoi(input.c_str());
    getline(file,input);
    number_doors = atoi(input.c_str());
    getline(file,input);
    cType = input;
    getline(file,input);
    price = atoi(input.c_str());
}

在我的 main() 中,我声明了 ifstream file("vehicles.txt")string input,它们在拆分代码之前运行良好。另外,我已经创建了ifstream filestring input 变量作为基类的受保护成员,否则它将无法编译,因为它找不到任何文件和输入变量可以使用。但是,现在看来该函数根本没有从main() 中获取变量,而只是用 0 或 N/A 填充所有对象的字段(根据构造函数。)

for (int i = 0; i < 11; i++)
{
    if(i<4) 
        vehicles.push_back(new Cars());
        vehicles[vehicles.size()-1]->txt_input();
    }
    if(i==4 || i==5)
    {
        vehicles.push_back(new Bus());
        vehicles[vehicles.size()-1]->txt_input();
    }
    //...
}

这就是我在main() 中创建对象的方式。正如我所说,当我在 Main.cpp 中拥有所有代码时,它运行良好。另外,我尝试将(ifstream file,string input) 作为参数传递给txt_input() 函数,但它会产生错误C2248。这是我第一次发帖,如有歧义,请见谅。如果需要,我会提供更多代码/信息。

class Vehicles
{
public:

    Vehicles();

    virtual void txt_input();
    virtual void user_input();
    virtual void output();

    void red_output();

    int getPC();
    int getPrice();
    string getBrand();

    void open_file();

protected:
    string brand, model, type;
    int year, passenger_capacity, price;
    double engine_power, max_speed;
    ifstream file;
    string input;
};

这是父类。

【问题讨论】:

  • fileinput 定义在哪里?他们是 Cars 或 globals 的成员吗?
  • 您的 ifstream 文件在哪里以及如何声明和定义?更简洁的设置是将输入流传递给您的 txt_input 函数。
  • @Jarod42 文件和输入是在基类中定义的。我刚刚编辑了帖子,对不起。
  • @heinrichj ifstream 文件仅在基类中声明。如何将流传递给 txt_input 函数?正如我所说,当我尝试时它给了我一个 C2248。另外,ifstream 文件只是被声明了,并没有真正打开文件。
  • txt_input 是否在您的基类中声明为虚拟? “文件”成员是如何初始化的?如果您显示基类的完整代码和具体类之一,也许会更容易......

标签: c++ ifstream


【解决方案1】:

因此,根据 cmets 的说法,问题在于您的变量 file 现在是 Vehicles 的成员,与您主目录中的前一个 ifstream 无关...

所以以下可能会有所帮助:

/*virtual*/ void Vehicles::txt_input(std::istream& file)
{
    std::string input;    

    getline(file, input);
    year = atoi(input.c_str());
    getline(file, input);
    brand = input;
    // And so on... 

}

所以你的循环变成了:

std::ifstream file("vehicles.txt");
for (int i = 0; i != 4; i++) {
    Cars* cars = new Cars();
    cars->txt_input(file);
    vehicles.push_back(cars);
}
for (int i = 0; i != 2; i++) {
    Bus* bus = new Bus();
    bus->txt_input(file);
    vehicles.push_back(bus);
}

【讨论】:

  • 是的,伙计,做到了。非常感谢。只是一些简单的问题: 1.为什么我们将它作为 istream 而不是 ifstream(如变量类型)传递? 2.为什么一定要引用传递?
  • istream 更通用,因此,您可以传递 std::cin 示例
【解决方案2】:

您在main 中设置了变量file,但使用的成员变量也名为file,它没有设置为vehicles.txt。一种方法是让构造函数将文件名作为参数。

例如代码 - 未编译 构造函数

Cars::Cars(const ifstream& if) : file(if) { }

将其添加到矢量时 vehicles.push_back(new Cars(file)); // this is the file defined in main set to "vehicles.txt"

注意:在命名类成员变量(例如 mFile、m_file、_file)时使用约定以避免混淆。

【讨论】:

  • 是的,方法完成了。当一切都在 Main.cpp 中时,我有了这个函数,并且我已经全局定义了 ifstream 文件(“vehicles.txt”)和字符串输入。然后,如您所见,在 for 循环期间,将不同的对象推入向量中,并执行它们的 txt_input() 函数。但是,当我将代码拆分为 cpp 和标头时,该函数似乎只是在继承树本身中寻找此类变量。这就是我在基类 ifstream 文件中定义的原因;和字符串输入;仅此而已。
  • 类“Vehicles”是“Land”“Sea”“Air”的父类。 “Land”是“Cars”“Bus”的父级。 “Sea”是“Cruise_Ship”“Yacht”的父项。“Air”是“Airplane”“Helicopter”的父项。
  • 向我们展示基类定义和初始化file 的代码很重要。您还可以检查getline 设置的任何错误,以了解发生了什么。
  • 感谢您的帮助。我将在未来研究命名约定。
猜你喜欢
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2013-11-18
  • 2022-01-08
  • 2012-03-04
  • 2021-06-20
  • 2012-06-20
相关资源
最近更新 更多