By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

Classes,lists

P: n/a
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[i] << " , " << 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[i] << " , " << 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[i] << " , " << 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[i] << " , " << 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[i] << " , " << 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.

Mar 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
zf*****@umd.umich.edu wrote:
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[i] << " , " << 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[i] << " , " << 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[i] << " , " << 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[i] << " , " << 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[i] << " , " << 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
Mar 19 '07 #2

P: n/a
3 ..List.cpp In file included from List.cpp
9 ..List.h variable or field `Store' declared void
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.

Mar 19 '07 #3

P: n/a
JE
On Mar 19, 11:00 am, zfar...@umd.umich.edu wrote:
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);
<snip>
Please advise. I dont see any syntax errors.
std::ifstream

Mar 19 '07 #4

P: n/a

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.

Mar 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.