C++ 列表stl C++;结构

C++ 列表stl C++;结构,c++,linux,list,stl,C++,Linux,List,Stl,我在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&

我在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
server3.cpp:在函数“int main(int,char**)”中:
Serv3.CPP:43:11:错误:ISO C++禁止指针和整数之间的比较[fime]
server3.cpp:74:15:错误:“class std::list”没有名为“id”的成员
server3.cpp:74:25:错误:“class std::list”没有名为“firstName”的成员
server3.cpp:74:42:错误:“class std::list”没有名为“lastName”的成员
server3.cpp:75:12:错误:“class std::list”没有名为“id”的成员
server3.cpp:76:17:错误:“哈希表[hash]”中的“运算符[]”不匹配
server3.cpp:76:50:错误:在“')标记之前应该有主表达式
代码如下:

#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;    



}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
常数int SIZE=100/*哈希表的大小*/
/*表示记录的结构*/
结构记录
{
int-id;
charfirstname[100];
char lastName[100];
}rec;
/*表示单个单元格的结构*/
类单元
{
std::列表记录;
pthread_mutex_t lock;
};
/*实际哈希表*/
std::列表哈希表;
int main(int argc,char*argv[])
{
ifstream indata;/*indata就像一个cin*/
open(“fileName”);/*打开文件*/
list rec;/*创建一个对象*/
整数散列;
同时(!indata.eof())/*继续读取,直到文件结束*/
{ 
indata>>rec.id>>rec.firstName>>rec.lastName;
hash=rec.id%sizeof(哈希表);
哈希表[hash].listofrecords.push_back(记录);
}
indata.close();
返回0;
}

他们中的大多数人都告诉您,
列表
没有成员,因为您试图这样做

indata>> rec.id >> rec.firstName >> rec.lastName;
但是
rec
是一个列表,而不是
记录

hashtable[hash]
也是非法的(请参见
std::list
的界面,
Record
是一个类型,您不能在容器中插入类型,只能插入对象:

...push_back (Record);
这是违法的


<>代码不只是偶尔出现的错误,而是强的,根本上有缺陷的<强>。我建议你开始学习C++(如果你正在做的话)从。

请注意,您正在创建一个记录集合,但这意味着您需要在访问其中包含的记录字段之前访问集合的元素

以下是有关使用列表类型的参考:

仅这条线路就至少有三个主要问题

 hashtable [hash].listofrecords.push_back (Record);
  • std::list
    没有
    运算符[]
    ,因此不能使用
    [hash]
    订阅
  • 在您的程序中没有任何地方定义了
    listofrecords
    的含义
  • 您正试图在需要对象的位置
    向后推
    一个名为
    Record
    的类型

  • 请找到一本好的C++书籍,开始:

    这是什么意思?谢谢。@纳塔莎列维奇,这意味着你需要从可靠的书开始学习C++。