Sign in to follow this  

template class and vector issue

This topic is 3730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

*EDIT: NO LONGER AN ISSUE, I FOUND THE PROBLEM! THANKS THOUGH!* Ok, here's the low down; I am writing an order entry system (console, not windows) and to save the data to files I have a template class that takes in a vector and saves the data. I have 5 vectors that I want to save. 1 of them works, the others give me this error: Error error C2664: 'fileMan<T>::saveToFile' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'std::vector<_Ty>' Seems kind of odd that it says it can't convert from the type that it is to the type that it is? here's some code: Fileman.cpp template class
/*
	fileman.cpp 
	file manager template class
*/

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

template <class T>
class fileMan
{
public:
	vector<T> loadFromFile(string fileName, vector<T> data);
	void saveToFile(string fileName, vector<T> data, int numElements);

private:
	vector<T> myData;
};

template <class T>
vector<T> fileMan<T>::loadFromFile(string fileName, vector<T> data)
{
	return myData;
}

template <class T>
void fileMan<T>::saveToFile(string fileName, vector<T> data, int numElements)
{
	ofstream outputFile;

	outputFile.open(fileName.c_str());

	if (outputFile.is_open())
	{
		for (int i = 0; i < numElements; i++)
		{
			outputFile << data[i];
		}
	}
	else
	{
		cout << "Could not save data!" << endl << endl;
	}
}



my customer header file (customer vector works with the fileman class)
// customer.h header file

#ifndef CUSTOMER_H
#define CUSTOMER_H

#include <iostream>

using namespace std;

class customer:public person
{
// operator overload
friend ostream& operator<<(ostream&, const customer&);

public:
	int getCustomerID();

	customer(); // consturctor with no data

	// constructors with data
	customer(string firstName, string middleName, string lastName);
	customer(string firstName, string middleName, string lastName, string emailAddress);
	customer(string firstName, string middleName, string lastName, string emailAddress, string street1, string street2, string city, string state, string zip);

	

private:
	int customerID;

};

#endif


customer.cpp file
/* 
	customer.cpp implementation file
	derived from person
*/

#include <iostream>
#include "person.h"
#include "customer.h"



using namespace std;

int customer::getCustomerID()
{
	return customerID;
}

customer::customer()
{
	firstName = "Undefined";
	middleName = "Undefined";
	lastName = "Undefined";
	emailAddress = "Undefined";
	customerID = 1;
	physicalAddressLine1 = "Undefined";
	physicalAddressLine2 = "Undefined";
	physicalAddressCity = "Undefined";
	physicalAddressState = "Undefined";
	physicalAddressZip = "000000";
}

customer::customer(string firstName, string middleName, string lastName)
{
	firstName = firstName;
	middleName = middleName;
	lastName = lastName;
	emailAddress = "Undefined";
	physicalAddressLine1 = "Undefined";
	physicalAddressLine2 = "Undefined";
	physicalAddressCity = "Undefined";
	physicalAddressState = "Undefined";
	physicalAddressZip = "000000";
}

customer::customer(string firstName, string middleName, string lastName, string emailAddress)
{
	firstName = firstName;
	middleName = middleName;
	lastName = lastName;
	emailAddress = emailAddress;
	physicalAddressLine1 = "Undefined";
	physicalAddressLine2 = "Undefined";
	physicalAddressCity = "Undefined";
	physicalAddressState = "Undefined";
	physicalAddressZip = "000000";
}

customer::customer(string firstName, string middleName, string lastName, string emailAddress, string street1, string street2, string city, string state, string zip)
{
	firstName = firstName;
	middleName = middleName;
	lastName = lastName;
	emailAddress = emailAddress;
	physicalAddressLine1 = street1;
	physicalAddressLine2 = street2;
	physicalAddressCity = city;
	physicalAddressState = state;
	physicalAddressZip = zip;
}

// operator overload - lets use this to format the data for the save file
ostream& operator<< (ostream& osObject, const customer& customerObject)
{
	osObject << "#BEGINREC#" << endl;
	osObject << customerObject.firstName << endl;
	osObject << customerObject.middleName << endl;
	osObject << customerObject.lastName << endl;
	osObject << customerObject.physicalAddressLine1 << endl;
	osObject << customerObject.physicalAddressLine2 << endl;
	osObject << customerObject.physicalAddressCity << endl;
	osObject << customerObject.physicalAddressState << endl;
	osObject << customerObject.physicalAddressZip << endl;
	osObject << customerObject.emailAddress << endl;
	osObject << "#ENDREC#" << endl;
	return osObject;
}



employee.h header (this does not work with the fileman class)
// employee.h header file

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>

using namespace std;

class employee:public person
{

// operator overload
	friend ostream& operator<<(ostream&, const employee&);

public:
	int getEmployeeID();
	double getPayRate();
	void setPayRate(double newPayRate);
	string getPayPeriod();
	void setPayPeriod(string newPayPeriod);
	string getSupervisor();
	void setSupervisor(string newSupervisor);
	string getDepartment();
	void setDepartment(string newDepartment);
	string getDateOfHire();
	void setDateOfHire(string newDOH);

	employee(); // empty constructor

	// constructors with values
	employee(string fName, string mName, string lName, string DOH);
	employee(string fName, string mName, string lName, string DOH, double newPayRate, string newPayPeriod);
	employee(string fName, string mName, string lName, string DOH, double newPayRate, string newPayPeriod, string newSupervisor);
	employee(string fName, string mName, string lName, string DOH, double newPayRate, string newPayPeriod, string newSupervisor, string newDepartment);

private:
	int employeeID;
	string dateOfHire;
	double payRate;
	string payPeriod; // hourly, weekly, bimonthly, monthly, yearly
	string supervisor;
	string department;

};

#endif


employee.cpp file
/*
	employee.cpp implementation file
	derived from person
*/

#include <iostream>
#include "person.h"
#include "employee.h"

using namespace std;

int employee::getEmployeeID()
{
	return employeeID;
}

double employee::getPayRate()
{
	return payRate;
}

void employee::setPayRate(double newPayRate)
{
	payRate = newPayRate;
}

string employee::getPayPeriod()
{
	return payPeriod;
}

void employee::setPayPeriod(string newPayPeriod)
{
	payPeriod = newPayPeriod;
}

string employee::getSupervisor()
{
	return supervisor;
}

void employee::setSupervisor(string newSupervisor)
{
	supervisor = newSupervisor;
}

string employee::getDepartment()
{
	return department;
}

void employee::setDepartment(string newDepartment)
{
	department = newDepartment;
}

string employee::getDateOfHire()
{
	return dateOfHire;
}

void employee::setDateOfHire(string newDOH)
{
	dateOfHire = newDOH;
}

employee::employee() // empty constructor
{
	firstName = " ";
	middleName = " ";
	lastName = " ";
	dateOfHire = " ";
	employeeID = 0;
	department = " ";
	supervisor = " ";
	payRate = 0.0;
	payPeriod = " ";
}

// constructors with values
employee::employee(string fName, string mName, string lName, string DOH)
{
	firstName = fName;
	middleName = mName;
	lastName = lName;
	dateOfHire = DOH;
	employeeID = 0;
	department = " ";
	supervisor = " ";
	payRate = 0.0;
	payPeriod = " ";	
}

employee::employee(string fName, string mName, string lName, string DOH, double newPayRate, string newPayPeriod)
{
	firstName = fName;
	middleName = mName;
	lastName = lName;
	dateOfHire = DOH;
	employeeID = 0;
	department = " ";
	supervisor = " ";
	payRate = newPayRate;
	payPeriod = newPayPeriod;
}

employee::employee(string fName, string mName, string lName, string DOH, double newPayRate, string newPayPeriod, string newSupervisor)
{
	firstName = fName;
	middleName = mName;
	lastName = lName;
	dateOfHire = DOH;
	employeeID = 0;
	department = " ";
	supervisor = newSupervisor;
	payRate = newPayRate;
	payPeriod = newPayPeriod;
}

employee::employee(string fName, string mName, string lName, string DOH, double newPayRate, string newPayPeriod, string newSupervisor, string newDepartment)
{
	firstName = fName;
	middleName = mName;
	lastName = lName;
	dateOfHire = DOH;
	employeeID = 0;
	department = newDepartment;
	supervisor = newSupervisor;
	payRate = newPayRate;
	payPeriod = newPayPeriod;
}

// operator overload - lets use this to format the data for the save file
ostream& operator<< (ostream& osObject, const employee& employeeObject)
{
	osObject << "#BEGINREC#" << endl;
	osObject << employeeObject.firstName << endl;
	osObject << employeeObject.middleName << endl;
	osObject << employeeObject.lastName << endl;
	osObject << employeeObject.physicalAddressLine1 << endl;
	osObject << employeeObject.physicalAddressLine2 << endl;
	osObject << employeeObject.physicalAddressCity << endl;
	osObject << employeeObject.physicalAddressState << endl;
	osObject << employeeObject.physicalAddressZip << endl;
	osObject << employeeObject.dateOfHire << endl;
	osObject << employeeObject.department << endl;
	osObject << employeeObject.emailAddress << endl;
	osObject << employeeObject.payRate << endl;
	osObject << "#ENDREC#" << endl;
	return osObject;
}



Customer and employee both derive from a person class. I can include that code if needed. Here's the function call in main():
// create vectors to store each data type
vector<customer> vCustomers;
vector<employee> vEmployees;
vector<order> vOrders;
vector<account> vAccounts;
vector<product> vProducts;

fileMan<customer> myfileman;
******
cout << endl;
cout << "Writing data to files, please wait..." << endl;
myfileman.saveToFile("customers.txt", vCustomers, vCustomers.size());
myfileman.saveToFile("employees.txt", vEmployees, vEmployees.size());
myfileman.saveToFile("orders.txt", vOrders, vOrders.size());
myfileman.saveToFile("products.txt", vProducts, vProducts.size());
myfileman.saveToFile("accounts.txt", vAccounts, vAccounts.size());
cout << "Done writing data!" << endl;
isRunning = false;


Any ideas?

Share this post


Link to post
Share on other sites

This topic is 3730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this