Classes,lists

Z

zfareed

I have a program with a list being read from a file and stored in an
array. On compilation I get the following errors in my implementation
file.I cannot figure out the problem.
//specification List.h
#include <fstream>

const int MAX_ITEMS = 20;
typedef int ItemType;

class List
{
public:
void Store(ifstream& infile,ItemType item);
// Pre: The list is not full
// Post: item is in the list
void PrintList();
// Post: If the list is not empty, the elements are
// printed on the screen; otherwise "The list
// is empty" is printed on the screen.
int Length();
// Post: return value is the number of items in the
list.
bool IsEmpty();
// Post: returns true if list empty;false otherwise
bool IsFull();
// Post: returns true if there is no more room in the
// list; false otherwise.
List();
// Constructor
// Post: Empty list is created.


private:
int length;
ItemType values[MAX_ITEMS];
};
//implementation List .cpp
#include <fstream>
#include <iostream>
#include "List.h"

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}

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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}

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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}


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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}


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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}


3 ..List.cpp In file included from List.cpp
9 ..List.h variable or field `Store' declared void
9 ..List.h expected `;' before '(' token
37 List.cpp no `void List::Store(std::ifstream&, ItemType)' member
function declared in class `List'

Please advise. I dont see any syntax errors.
 
F

Fei Liu

I have a program with a list being read from a file and stored in an
array. On compilation I get the following errors in my implementation
file.I cannot figure out the problem.
//specification List.h
#include <fstream>

const int MAX_ITEMS = 20;
typedef int ItemType;

class List
{
public:
void Store(ifstream& infile,ItemType item);
// Pre: The list is not full
// Post: item is in the list
void PrintList();
// Post: If the list is not empty, the elements are
// printed on the screen; otherwise "The list
// is empty" is printed on the screen.
int Length();
// Post: return value is the number of items in the
list.
bool IsEmpty();
// Post: returns true if list empty;false otherwise
bool IsFull();
// Post: returns true if there is no more room in the
// list; false otherwise.
List();
// Constructor
// Post: Empty list is created.


private:
int length;
ItemType values[MAX_ITEMS];
};
//implementation List .cpp
#include <fstream>
#include <iostream>
#include "List.h"

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}

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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}

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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}


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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}


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

using namespace std;

List::List()
{
length = 0;
}

bool List::IsFull()
{
return (length == MAX_ITEMS);
}

bool List::IsEmpty()
{
return (length == 0);
}

int List::Length()
{
return length;
}

void List::printList()
{
if(length == 0)
cout << "The list is empty" << endl;
else
for(int i=0; i < MAX_ITEMS-1;i++)
cout << values << " , " << endl;
}

void List::Store(ifstream& infile,ItemType item)
{
infile >> item;

while(infile)
{
values[length] = item;
length++;
infile >> item;
}
}


3 ..List.cpp In file included from List.cpp
9 ..List.h variable or field `Store' declared void
9 ..List.h expected `;' before '(' token
37 List.cpp no `void List::Store(std::ifstream&, ItemType)' member
function declared in class `List'

Please advise. I dont see any syntax errors.


Except that you triple posted List.cpp and a few style issues, your code
is valid. Are you sure this is the same code that showed you the compile
error at List.h::9?

Fei
 
Z

zfareed

Except that you triple posted List.cpp and a few style issues, your code
is valid. Are you sure this is the same code that showed you the compile
error at List.h::9?

Fei- Hide quoted text -

- Show quoted text -

I apologise for the triple paste. Yes this is exactly the same code
and their is one more file, the driver.

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

using namespace std;

ifstream infile;
ItemType item;

int main()
{
infile.open("int.dat");
Store(infile,item);
PrintList();

system("PAUSE");
return 0;
}

I am lost.
 
J

JE

I have a program with a list being read from a file and stored in an
array. On compilation I get the following errors in my implementation
file.I cannot figure out the problem.
//specification List.h
#include <fstream>

const int MAX_ITEMS = 20;
typedef int ItemType;

class List
{
public:
void Store(ifstream& infile,ItemType item);
Please advise. I dont see any syntax errors.

std::ifstream
 
T

tragomaskhalos

A couple of style tips:
List::List()
{
length = 0;
}
Better is:
List::List() : length(0) {}
void List::Store(ifstream& infile,ItemType item)
Better is:
void List::Store(istream& infile,ItemType item)
Then you can store to other types of streams besides files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Class templates 1
class templates 0
TF-IDF 1
class templates 4
Character operations in C++ 2
How do I make this craftinfsystem Work 1
Crossword 2
Seek for help..linked list..urgent!!! 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top