-3

I am having a problem with the while loop inserting data into a stl list from text file. Could you please help me understand my errors? Thanks a lot. Errors are

server3.cpp: In function ‘int main(int, char**)’:
server3.cpp:43:11: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
server3.cpp:74:15: error: ‘class std::list<Record>’ has no member named ‘id’
server3.cpp:74:25: error: ‘class std::list<Record>’ has no member named ‘firstName’
server3.cpp:74:42: error: ‘class std::list<Record>’ has no member named ‘lastName’
server3.cpp:75:12: error: ‘class std::list<Record>’ has no member named ‘id’
server3.cpp:76:17: error: no match for ‘operator[]’ in ‘hashtable[hash]’
server3.cpp:76:50: error: expected primary-expression before ‘)’ token

Code follows:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <vector>
#include <list>
#include <iostream>
#include <fstream>
using namespace std;

const int SIZE =100;/*size of hashTable*/
/*Struct representing the record*/
struct Record
{
     int id;
     char firstName[100];
     char lastName[100];
} rec;


/*Structure representing a single cell*/
class Cell
{
    std:: list<Record> recs;
    pthread_mutex_t lock;
};

/* The actual hash table */
std::list<Cell> hashtable;



int main (int argc, char * argv[])
{

    ifstream indata; /* indata is like a cin*/
        indata.open("fileName"); /* opens the file*/

    list <Record> rec;/*create an object*/
    int hash;
    while ( !indata.eof() ) /* keep reading until end-of-file*/
    { 
    indata>> rec.id >> rec.firstName >> rec.lastName;
    hash =rec.id % sizeof(hashtable);
    hashtable [hash].listofrecords.push_back (Record);

    }
     indata.close();

   return 0;    



}
billz
  • 44,644
  • 9
  • 83
  • 100

3 Answers3

1

Most of them are telling you that list doesn't have no members, because you try to do

indata>> rec.id >> rec.firstName >> rec.lastName;

but rec is a list, not a Record.

hashtable[hash]

is also illegal (see the interface for std::list, and Record is a type, and you can't insert a type in a container, you can only insert objects:

...push_back (Record);

is illegal.

The code doesn't have just the occasional error we all make from time to time, but is fundamentally flawed. I suggest you start learning C++ (if that's what you're doing) from a good book.

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

Please note that you are creating a collection of records, but this means that you need to access an element of the collection before accessing the fields of the records contained there.

Here's a reference on using the list type: http://www.cplusplus.com/reference/list/list/

RonaldBarzell
  • 3,822
  • 1
  • 16
  • 23
0

There are at least three major problems with this line alone.

 hashtable [hash].listofrecords.push_back (Record);
  1. std::list doesn't have an operator[] so you can't use the [hash] subscripting.
  2. No place in your program have you defined what listofrecords means.
  3. You are trying to push_back a type named Record where an object is required.

Please find a good C++ book to get started with: The Definitive C++ Book Guide and List

Community
  • 1
  • 1
Blastfurnace
  • 18,411
  • 56
  • 55
  • 70