gpt4 book ai didi

C++从txt文件读取到对象二叉树

转载 作者:行者123 更新时间:2023-11-28 06:18:06 25 4
gpt4 key购买 nike

*** 使用我的更改编辑了代码——不再出现访问冲突,它完美地分配了我的所有值,除了客户 ID——只是将其留空。这是因为 ID 是数字而不是技术上的字符串吗??

这是作业——我们应该从 .txt 文件中读取数据,然后将记录分配给对象,然后将对象写入二叉树。这是输入文本文件格式:

00001
Wilee Caytote
123 E. Fifth St.
Phynox
AZ
12345-1234
00002
Dave Wells
444 W. Third St.
Dayton
OH
45402
00012
Robert U. McKey
4986 Boundary St.
Jacksonville
AZ
12345
00123
Ruby B. Edwards
4861 Spring Ave.
Philadelphia
PA
19108

每一行都是 Customer (Id, Name, Address) 和 Address (Street, City, State, Zip) 类的成员。我正在覆盖我的 cin >> 运算符以获取值,但这些值没有分配给对象成员。我确定我的程序可以读取文本文件,因为我执行了以下操作,并将我的文件打印到控制台。

myfile.open ("Customers.txt");
while(getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();

这是我的 Customer.h 类,其中 cin 重载是:

#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include "Address.h"

using namespace std;

class Customer
{

private:
//Attributes for customers
string custID;
string custName;
Address* address;

public:
//Constructors for Customer
Customer();
Customer(string, string, Address*);
~Customer();
//Setters for Customer
void setCustID(int);
void setCustName(string);
void setCustAddress(Address*);
//Getters for Customer
string getCustID() { return custID; }
string getCustName() { return custName; }

//Operator overloads
bool operator>(Customer obj) { return custID > obj.custID; }
bool operator<(Customer obj) { return custID < obj.custID; }
bool operator==(Customer obj) { return custID == obj.custID; }

//Operator overloads for input
friend istream &operator>>(istream &input, Customer &customer) {
getline(input, customer.custID);
getline(input, customer.custName);
(*customer.address);
return input;
}

//Operator overloads for output
friend ostream &operator<<(ostream &output, Customer &customer) {
output << "CustID: " << customer.custID << endl << "Customer Name: " << customer.custName << endl << (*customer.address);
return output;
}
};

这是我的 Address.h 类:

#pragma once
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

class Address
{
private:
string street;
string city;
string state;
string zip;

public:
//Constructors for address
Address();
Address(string, string, string, string);
~Address();
//Setters for address
void setAddressStreet(string);
void setAddressCity(string);
void setAddressState(string);
void setAddressZip(string);
//Getters for address
string getAddressStreet() {return street;}
string getAddressCity() {return city;}
string getAddressState() {return state;}
string getAddressZip() {return zip;}

//Operator overload for input
friend istream &operator>>(istream &input, Address &address) {
getline(input, address.street);
getline(input, address.city);
getline(input, address.state);
getline(input, address.zip);
return input;
}
//Operator overload for output
friend ostream &operator<<(ostream &output, Address &address) {
output << "Street Address: " << address.street << endl << "City: " << address.city << endl << "State: " << address.state << endl << "Zip: " << address.zip << endl;
return output;
}
};

这是我的驱动程序,我正在读取文件,将行分配给对象,并将它们插入二叉树中:

#include "Customer.h"
#include "BinaryTree.h"

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
BinaryTree tree;
Customer customer;
string id;

//Define file and line variables
ifstream myfile;
string line;

//Open file and read lines into objects
myfile.open ("Customers.txt");
while(myfile >> line)
{
myfile >> customer;
tree.addCustomer(customer);
cout << customer;
}
myfile.close();

system ("pause");
return 0;

客户.cpp:

#include "Customer.h"

//Customer no arg constructor
Customer::Customer()
{
custID = "";
custName = "";
address = new Address;
}

//Customer constructor
Customer::Customer(string custID, string custName, Address* address)
{
this->custID = custID;
this->custName = custName;
}

//Customer destructor
Customer::~Customer()
{
}

地址.cpp:

#include "Address.h"

//Address no arg constructor
Address::Address()
{
street = "";
city = "";
state = "";
zip = "";
}

//Address constructor
Address::Address(string street, string city, string state, string zip)
{
this->street = street;
this->city = city;
this->state = state;
this->zip = zip;
}

//Address destructor
Address::~Address()
{
}

最佳答案

您需要在Customer 类中初始化您的地址指针。您正在取消引用导致访问冲突异常的未初始化指针。

在您的 customer 构造函数中,添加以下行:

address = new Address;

您需要将以下内容添加到 customer 析构函数中:

delete address;

不过,我很好奇为什么您的 Address 成员是一个指针。直接声明它而不是创建指向它的指针会更安全且更不容易出错。如果您真的希望它成为一个指针,我建议您查看 std::unique_ptr而不是使用原始指针。

关于C++从txt文件读取到对象二叉树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29852643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com