我在 while 循环将数据从文本文件插入 stl 列表时遇到问题。你能帮我理解我的错误吗?非常感谢。错误是
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
代码如下:
#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;
}