Click here to Skip to main content
15,063,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I currently have a class that uses a pointer vector to another class. Everything has worked fine until I began implementing boost serialization. On the first run with the program, everything works fine and appears to save the data into the .xml exactly as it should. However, I get a pop-up dialog error when attempting to run the second time(load the data). Please help. Thanks in advance...


Edit: The error that I get is a popup from Microsoft Visual C++ Runtime Library. It states:

Debug Error!

C:\Users\...\taxTracker.exe

abort() has been called

(Press Retry to debug the application)

What I have tried:

Account class:
C++
#ifndef Account_h
#define Account_h

#include "Check.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;

class Account
{
private:
	friend class boost::serialization::access;
	template<class Archive>
	void serialize(Archive& archive, const unsigned int version)
	{
		archive& BOOST_SERIALIZATION_NVP(check);
		archive& BOOST_SERIALIZATION_NVP(taxRate);
		archive& BOOST_SERIALIZATION_NVP(numOfChecks);

	}
	vector<Check*> check;
	double taxRate;
	int numOfChecks;

public:

	Account();
	Account(double t);
	int getNumOfChecks();
	void listAll();
	void addCheck(Check*);
	void editCheckAmount(int, double);
	void removeCheck(int);
	void setTaxRate(double);
	double getTaxRate();
	double getTaxesOwed(); //method that returns taxes owed on all checks
	double getTotalIncome();

};
#endif Account_h


Check class:
C++
#ifndef Check_h
#define Check_h

#include <string>
#include <vector>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;

class Check
{
private:
	friend class boost::serialization::access;
	double amount;
	string date;
	int id;

	template<class Archive>
	void serialize(Archive& archive, const unsigned int version) {
		archive& BOOST_SERIALIZATION_NVP(amount);
		archive& BOOST_SERIALIZATION_NVP(date);
		archive& BOOST_SERIALIZATION_NVP(id);
	}


public:

	Check();
	Check(double, char*, int);

	double getAmount();
	void setAmount(double a);

	string getDate();
	void setDate(char*);

	void setId(int id);
	int getId();

};
#endif Check_h


And the driver taxTracker:

C++
#include "Account.h"
#include "Check.h"
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/vector.hpp>

#include <chrono>
#include <ctime>

#pragma warning(disable : 4996)

using namespace std;

int mainMenu(Account&);
void addCheck(Account&);
void editCheck(Account&);

int main() {

	Account account;
	ifstream in;

	try {
		in.open("data.xml");
		boost::archive::xml_iarchive xml_input_archive(in);
		xml_input_archive& BOOST_SERIALIZATION_NVP(account);
		in.close();
	}
	catch (ifstream::failure e) {
		cerr << "Exception opening/reading/closing file";
	}
	catch (boost::archive::archive_exception e) {
		cerr << "Exception opening/reading/closing file";
	}
	

	//intro message
	cout << "Welcome to Tax Tracker. \n\nTax Tracker is a tool to help you keep track of your total income and the taxes owed on your income."
		<< "\n\nAll information is stored locally.\n" << endl;
	cout << "Please make a choice from the following menu: \n" << endl;

	int choice = 0;

	//repeating switch case
	do {
		choice = mainMenu(account);
		switch (choice) {

		case 1:
			addCheck(account);
			break;

		case 2:
			editCheck(account);
			break;

		case 3:
			account.listAll();
			break;

		case 4:
			double tax;
			cout << "Please enter your desired tax rate: " << endl;
			cin >> tax;
			account.setTaxRate(tax);
			break;

		case 5:
			cout << "Saving information..." << endl;
			cout << "Thanks for using Tax Tracker." << endl;

			
			ofstream out("data.xml");
			boost::archive::xml_oarchive xml_output_archive(out);

			xml_output_archive& BOOST_SERIALIZATION_NVP(account);
			break;
		}
	} while (choice != 5);

	return 0;
}

// menu for making a selection.
int mainMenu(Account& account) {

	int choice;

	cout << left << "\nTotal Income: " << setw(10) << account.getTotalIncome()
		<< "Taxes Owed: " << account.getTaxesOwed() << endl;

	cout << "\n1. Add entry\n"
		<< "2. Edit entry\n"
		<< "3. View entries\n"
		<< "4. Change tax rate\n"
		<< "5. Exit" << endl;
	cin >> choice;

	return choice;
}

//adds a Check to Account along with a timestamp
void addCheck(Account& account) {

	int amount;
	cout << "Please enter the amount of the check: ";
	cin >> amount;

	std::time_t now = time(0);
	char* dt = ctime(&now);

	account.addCheck(new Check(amount, dt, account.getNumOfChecks() + 1));

}

//edit Check ... still needs date editing and exception handling in case of editing a non existing entry
void editCheck(Account& account) {

	int entryChoice;
	int editChoice;
	double amount;

	account.listAll();

	cout << "\nPlease enter the ID number of the entry that you would like to edit: ";
	cin >> entryChoice;

	cout << "\nWhat would you like to do?" << endl;
	cout << "\n1. Edit the amount" << endl;
	cout << "\n2. Remove the entry" << endl;
	cout << "\n3. Back to main menu" << endl;
	cin >> editChoice;

	if (editChoice == 1) {
		cout << "New amount: ";
		cin >> amount;
		account.editCheckAmount(entryChoice, amount);
	}
	else if (editChoice == 2) {
		account.removeCheck(entryChoice);
	}


}
Posted
Updated 17-Aug-21 6:11am
v3
Comments
Greg Utas 16-Aug-21 19:28pm
   
It is doubtful that you will get help unless you post the details of this secret "pop-up dialog error".
Adam Aug2021 16-Aug-21 19:36pm
   
Sorry about that, and thanks for the tip!

That's a very generic error that doesn't say why abort was called. But press Retry and use the debugger to find the problem.

If you don't know how to use a debugger, Visual Studio has lots of documentation about it. Using a debugger is indispensable once you've advanced beyond simple programs.

I'm not familiar with this Boost seralization/deserialization library, so I can't help. Even if I could, it would mean debugging your code by hand, which is very tedious unless one sees something obvious.

Here's a starting point[^]. Note that this link is for VS2019. If you're using VS2017, the documentation should be very similar, but there's a drop-down menu on the left that allows you to select the version you're running.
   
v2
Thanks for the help all. Turns out all I had to do was switch from xml to text (ie. xml_oarchive to text_oarchive). Not sure what happened there but I don't have the time to keep fiddling around with it as the beginning of the semester is closing in.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900