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

时间:2015-04-24 16:22:35

标签: c++ operator-overloading iostream

***使用我的更改编辑了代码 - 不再获取访问权限,并且除了客户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

每一行都是客户(身份证,姓名,地址)和地址(街道,城市,州,邮政)类的成员。我压倒了我的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;

}

customer.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()
{
}

Address.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()
{
}

1 个答案:

答案 0 :(得分:1)

您需要在<tr>课程中初始化地址指针。您正在取消引用未初始化的指针,该指针导致访问冲突异常。

Customer构造函数中,添加以下行:

customer

您需要将以下内容添加到address = new Address; 析构函数中:

customer

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