c++ - 列出 STL C++ 结构

标签 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>’ 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;    



}

最佳答案

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

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

但是 rec 是一个列表,而不是 Record

hashtable[hash]

也是非法的(参见std::list的接口(interface),而Record是一种类型,并且不能在容器中插入类型,只能插入对象:

...push_back (Record);

非法。

该代码不仅存在我们偶尔会犯的错误,而且还存在根本性缺陷。我建议你从 a good book 开始学习 C++(如果你正在做的话) .

关于c++ - 列出 STL C++ 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13811761/

相关文章:

javascript - 在 For 循环中探索 javascript 中的树结构

c++ - 减少所需的 libc 版本

c++ - 德州扑克直接检查值

linux - 如何创建具有正确 tty 设置的 execve() 子进程以运行 'vi' 但仍将 IO 重定向回父进程?

Python:如何修复这个损坏的管道错误?

bash - 如何从 bash 中的循环创建字符串列表?

c++ - 使用 boost::shared_ptr 重载构造函数

c++ - 经典的生产者消费者线程

linux - probe() 函数的参数从何而来?

python - 使用循环为列表中的变量赋值