简体   繁体   English

错误:“->”的类型参数无效(具有'Queue {aka struct Queue}

[英]error:invalid type argument of '->'(have 'Queue {aka struct Queue}

I'm trying read a file with this format instance|instruction... like this 1 01 01 12 12 33, where the first number is the instance and after that is the numbers of the instruction. 我正在尝试使用这种格式的instance |指令读取文件...像这样1 01 01 12 12 33,其中第一个数字是实例,其后是指令的编号。 Each line has it own id. 每行都有自己的ID。 so in that example that line if was the first one the id was 1, and the next line 2, and so on. 因此在该示例中,如果if行是第一行,则id为1,下一行为2,依此类推。

What I'm trying to do is group that information by a struct like the PCB struct I have. 我想做的是按照一种结构(如我拥有的PCB结构)将信息分组。 Each line has an id, and a queue where I store each number of the instruction part. 每行都有一个id和一个队列,我在其中存储指令部分的每个数字。

I have a struct where i have some variables and a queue inside it. 我有一个结构,我里面有一些变量和一个队列。 The problem is the queue is not working. 问题是队列无法正常工作。

typedef struct Queue
{
  int sizeQueue;
  int limit;
  Node *head;


}Queue;

typedef struct PCB
{
 int id;
 int program_counter;
 int size;
 Queue pointer_instrucoes;
 int instante;

}PCB;

typedef struct Node
{
 PCB *element;
 Node *next;

}Node;

And in the main I'm calling the struct with the queue to store some values. 在主体中,我使用队列调用该结构以存储一些值。

PCB *p=new_pcb(contador);
....
p->pointer_instrucoes->head=atoi(s2); //s2 is the some number of the //instruction

but I'm getting this error:invalid type argument of '->'(have 'Queue {aka struct Queue} 但我收到此错误:'->'的类型参数无效(具有'Queue {aka struct Queue}

MVCE: MVCE:

main file 主文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //para o output
#include "queue2.h"

int le_ficheiro(char* filename) {
FILE *ficheiro=fopen(filename,"r");
size_t len=0;
char *line=NULL;
ssize_t read;
char *s1;//string antes do primeiro espaço
char *s2;//string depois do primeiro espaço
char *sp; //linha toda
if(ficheiro==NULL) {
    exit(EXIT_FAILURE);
}
int contador=1; //onde comeca o id

while((read = getline (&line, &len,ficheiro))!=-1)   //le de linha a linha
{
    PCB *p=new_pcb(contador);
    sp=strchr(line,' ');
    if(!sp)
    {
        exit(EXIT_FAILURE);
    }
    s1=strndup(line,sp-line);
    s2=sp+1;
    p->instante=atoi(s1); //converte char to int

    printf("Instante: %d\n",p->instante);
    printf("Id: %d\n",p->id);

    p.pointer_instrucoes.head=atoi(s2);

    printf("%d\n",p->pointer_instrucoes->head);



    printf("Retrieved line of length %zu:\n",read);
    printf("%s\n",s2);
    printf("Aqui : %c\n",line[0]);
    contador++;
}
 fclose(ficheiro);
 if(line)
    free(line);
    free(s1);
 exit(EXIT_SUCCESS);

 }


int main()
{
  char name[50];
  printf("Name of the file: ");
  scanf("%s",name);
  le_ficheiro(name);
  return 0;

} }

queue2.h file queue2.h文件

#include <stdbool.h>

typedef struct PCB PCB;
typedef struct Node Node;
typedef struct Queue Queue;
typedef struct Queue
{
  int sizeQueue;
  int limit;
  Node *head;


 }Queue;
typedef struct PCB
{
  int id;
  int program_counter;
  int size;
  Queue pointer_instrucoes;
  int instante;

 }PCB;

 typedef struct Node
 {
  PCB *element;
  Node *next;

  }Node;

 PCB * new_pcb(int id); 
 Node * new_node(PCB * e);
 Queue * new_queue(int limit);

Queue2.c Queue2.c

#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include "queue2.h"


Node * new_node(PCB * e)
 {
  Node *n=malloc(sizeof(Node));
  n->element=e;
  n->next=NULL;
  return n;
  }




Queue * new_queue(int limit)
{
   Queue *q=malloc(sizeof(Queue));
   q->limit=limit;
   q->head=NULL;
   q->sizeQueue=0;

   return q;

     }

   PCB * new_pcb(int id)
{
   PCB *p = malloc(sizeof(PCB));
   p->id=id;
   p->pointer_instrucoes=0;
   p->program_counter=0;
   p->size=0;
   return p;
   }

Example of input: 1 01 02 03 04 05 输入示例:1 01 02 03 04 05

2 01 02 03 02 11 2 01 02 03 02 11

Your problems are rudimentary, and you need to spend time competing le_ficheiro . 您的问题是基本的,您需​​要花时间竞争le_ficheiro For starters, you cannot assign the result of atoi(s2) to p.pointer_instrucoes.head . 对于初学者,您不能将atoi(s2)的结果分配给p.pointer_instrucoes.head head is not an int , it is a pointer to Node* . head不是int ,而是指向 Node*指针 You violate the Strict Aliasing Rule C11 Standard - §6.5 Expressions (p6,7) and probably a dozen others in attempting to access and modify Node* by assigning an int as its value. 通过将int赋值为int来尝试访问和修改Node* ,您违反了严格别名规则 C11标准-§6.5表达式(p6,7)

You invoke Undefined Behavior attempting to access p->id when it is indeterminate (uninitialized). 当不确定(未初始化)的p->id时,您调用Undefined Behavior尝试访问p->id

You make life much more difficult than it needs to be by attempting to manually separate values from line using strchr(line, ' ') . 通过尝试使用strchr(line, ' ')手动将值与line分开,会使生活变得比原来困难得多。 You already have all values contained in line , you can use sscanf to parse all six values to int in a single call and validate the line, eg 您已经在line包含了所有值,您可以使用sscanf解析所有六个值以在单个调用中将int进行验证并验证该行,例如

    while ((read = getline (&line, &len,ficheiro)) != -1) //le de linha a linha
    {
        int a, b, c, d, e, f;
        if (sscanf (line, "%d %d %d %d %d %d", 
                    &a, &b, &c, &d, &e, &f) != 6) {
            fprintf (stderr, "error: failed to parse line '%d'.\n",
                    contador);
            continue;
        }
        PCB *p = new_pcb (contador);
        if (!p)  /* Validate EVERY Allocation */
            exit (EXIT_FAILURE);

However, before you can validate every allocation and avoid potentially invoking Undefined Behavior , you must provide meaningful return values from new_node, new_queue & new_pcb , to indicate success/failure, eg 但是,在验证每个分配并避免潜在地调用Undefined Behavior之前 ,必须从new_node, new_queue & new_pcb提供有意义的返回值以指示成功/失败,例如

Node *new_node (PCB *e)
{
    Node *n = malloc (sizeof *n);
    if (!n) {   /* Validate EVERY Allocation */
        perror ("malloc-new_node");
        return NULL;  /* return NULL on failure */
    }
    n->element = e;
    n->next = NULL;

    return n;
}

Queue *new_queue (int limit)
{
    Queue *q = malloc (sizeof *q);   /* typo in declaration */
    if (!q) {
        perror ("malloc-new_queue");
        return NULL;
    }
    q->limit = limit;
    q->head = NULL;
    q->sizeQueue = 0;

    return q;
}

PCB *new_pcb (int id)
{
    PCB *p = malloc (sizeof *p);
    if (!p) {
        perror ("malloc-new_pcb");
        return NULL;
    }
    p->id = id;
    p->pointer_instrucoes = 0;
    p->program_counter = 0;
    p->size = 0;

    return p;
}

It is clear you got part way through le_ficheiro and just gave up. 很明显,您在le_ficheiro了一段路,然后放弃了。 It isn't anywhere near complete, only 1 of the data values from each line of input was even used in your code (the rest you attempted to print as text just to see where your s2 pointer was pointing -- but not make any further or meaningful use of the values) 它并不是很接近完成,代码中甚至只使用了每行输入中的一个数据值(其余的您试图打印为文本只是为了查看s2指针指向的位置,但是没有做进一步的事情)或有意义地使用值)

You don't even call new_node in order to create a Node* value that conceivably could be assigned to p.pointer_instrucoes.head . 您甚至不调用new_node来创建Node*值,可以想象将其分配给p.pointer_instrucoes.head Take it step-by-step. 逐步进行。 You need to call new_node somewhere in le_ficheiro so that you have a pointer you can assign to p.pointer_instrucoes.head . 你需要调用new_node在某处le_ficheiro ,让你有一个指针,你可以分配到p.pointer_instrucoes.head There isn't any magic to it, but you must be methodical about insuring each value and each member of each structure is created and properly initialized with a value before you attempt to use them. 它没有任何魔力,但是在使用每个值之前,您必须有条不紊地确保每个值,并创建每个结构的每个成员并使用值正确初始化。

Basically you had a shell for le_ficheiro which you need to continue to complete, working from a somewhat better starting position of: 基本上,您有一个le_ficheiro外壳,需要从以下更好的起始位置继续完成:

int le_ficheiro (char* filename) 
{
    FILE *ficheiro = fopen (filename,"r");
    size_t len = 0;
    ssize_t read = 0;
    int contador = 1;   //onde comeca o id

    if (ficheiro == NULL) {
        perror ("fopen-filename");
        exit (EXIT_FAILURE);
    }

    while ((read = getline (&line, &len,ficheiro)) != -1) //le de linha a linha
    {
        int a, b, c, d, e, f;
        if (sscanf (line, "%d %d %d %d %d %d", 
                    &a, &b, &c, &d, &e, &f) != 6) {
            fprintf (stderr, "error: failed to parse line '%d'.\n",
                    contador);
            continue;
        }
        PCB *p = new_pcb (contador);
        if (!p)
            exit (EXIT_FAILURE);

        p->instante = a;
        printf ("Instante: %d\n", p->instante);

        /* you must assign and allocate as necessary to use remaining
         * values read from file HERE. Nowhere do you assign p->id
         * before attempting to print, etc...
         */
        printf ("Id: %d\n", p->id);

        /* you cannot assign atoi(s2) to p.pointer_instrucoes.head.
         * it expects type 'Node *', not int.
         */
        // p.pointer_instrucoes.head = FIXME;
        // printf ("%d\n", p->pointer_instrucoes->head);
        contador++;
    }
    fclose(ficheiro);

    free (line);
    free(s1);

    exit(EXIT_SUCCESS);
}

When you are stuck in writing a function, don't give up. 当您坚持编写函数时,请不要放弃。 Instead see How to debug small programs and talk to the duck... Really, it helps :) 相反,请参见如何调试小型程序并与鸭子交谈...确实,这很有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:“-&gt;”的类型参数无效(具有“结构节点”) - error: invalid type argument of '->' (have 'struct node') 错误:“-&gt;”的类型参数无效(具有“ struct xmplrpc_binding”) - error: invalid type argument of ‘->’ (have ‘struct xmplrpc_binding’) “-&gt;”的无效类型参数(有“struct arr”) - invalid type argument of '->' (have 'struct arr') -> 的无效类型参数具有结构空间 - invalid type argument of -> have struct room 二进制*的操作数无效(有&#39;ab {aka struct a}&#39;和&#39;ab * {aka struct a *}&#39;) - Invalid operands to binary * (have ‘ab {aka struct a}’ and ‘ab * {aka struct a *}’) &#39;-&gt;&#39;的无效类型参数(具有&#39;struct coada&#39;) - invalid type argument of ‘->’ (have ‘struct coada’) c struct队列错误:“数组类型的元素类型不完整” - c struct queue error: “array type has incomplete element type” 错误:参数无效; 发送msgsnd()消息时; 不匹配队列ID - error: Invalid argument; while sending msgsnd() message; not matching queue ID 指针ABC。 错误:一元&#39;*&#39;的类型参数无效(具有&#39;struct config&#39;) - Pointers ABC. error: invalid type argument of unary ‘*’ (have ‘struct config’) 错误:结构初始化程序中的多余元素; &#39;-&gt;&#39; 的无效类型参数(有 &#39;int&#39;) - Errors: Excess elements in struct initializer ; invalid type argument of '->' (have 'int')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM