Start New Discussion within our Software Development Community

the program wont compile because it found and error with ReadFromFile class method in the main.cpp file. Any help would be appreciated.

class inventory deceleration in "Inventory.h" file

#ifndef INVENTORYDEF
#define INVENTORYDEF

#include <iostream>
#include <fstream>

int const MAXNAME=30;
int const SIZE=2;

class Inventory 
{
private:
	int quantity[SIZE]; 
	float unitcost[SIZE];
	char description[SIZE][MAXNAME];
public:
	void ReadFromFile(ifstream);
	void SortByDescription();
	void Output();

};

#endif

this is the main program with class methods "main.cpp" file

#include <iostream>
#include <fstream>
#include "Inventory.h"

using namespace std;

void Inventory::ReadFromFile(ifstream &fin) <-----------Read from file causing error
{
	for(int n=0; n<SIZE; n++)
	{
		fin >> description[n] >> quantity[n] >> unitcost[n];
	}
}

void Inventory::SortByDescription()
{
	BubbleSort(description, SIZE);
}

void Inventory::Output()
{
	
	for(int n=0; n<SIZE; n++)
	{
		cout << description[n] << quantity[n] << unitcost[n];
	}
}

void BubbleSort (char array1[][MAXNAME], int size)
	{
		bool done = false;
		int limit = 0;
		
		while (!done)
		{
			done = true;
			for (int n=0; n<size-limit-1 ; n++)
				if (strcmp(array1[n], array1[n+1]) > 0)
				{
					char temp[MAXNAME]; 
					strcpy_s(temp,array1[n]);
					strcpy_s(array1[n], array1[n+1]);
					strcpy_s(array1[n+1], temp);
					done = false;
				}
		}
	}

int main(void)
{
	Inventory inventory;
	ifstream fin;
	char filename[30];

	cout << "Enter filename: ";
	cin >> filename;
	fin.open(filename);

	inventory.ReadFromFile(fin);
	inventory.SortByDescription();
	inventory.Output();

	return 0;
}

Streams must be passed by reference like you did in main.cpp. So change inventory class like this: void ReadFromFile(ifstream&);

This article has been dead for over six months. Start a new discussion instead.