【问题标题】:Compiler complain about "invalid type argument of ‘->’", why?编译器抱怨“'->' 的类型参数无效”,为什么?
【发布时间】:2015-10-10 20:28:24
【问题描述】:

我有这个功能:

void simul(char *new)
{

    segment_table seg = malloc(sizeof(st));
    segcode newcode = malloc(sizeof(sc));
    int clen = 0;
    seg -> segname = new;
    clen = code_length(seg -> segname);
    seg -> code = read_hexcode(seg -> segname, newcode, clen);
    load_image(seg, clen-3);
    if (strstr(new, "paper_boot-strap loader.hexcode") == NULL)
            run(segment_offset(seg));
}

当我尝试编译我的程序时,出现以下错误:

error: ‘st’ undeclared (first use in this function)
error: ‘sc’ undeclared (first use in this function)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
error: invalid type argument of ‘->’ (have ‘segment_table’)
  • 我做错了什么?

【问题讨论】:

  • 如果您真的阅读错误消息,您认为它们是什么意思?例如,您认为消息“错误:'st' undeclared”是什么意思?为什么你认为你会得到这个错误?
  • 让我猜猜,可能是‘st’ undeclared。或者‘sc’ undeclared...
  • 如果这真的是 C++,使用 new 作为变量名是不允许的。如果是 C,请使用正确的标签。
  • 这是 C。'st' 和 'sc' 未声明
  • 好的,您知道错误的含义,您认为您可以如何解决它们? stsc 定义在哪里?

标签: c


【解决方案1】:

头文件,例如header.h:

// First an include guard (https://en.wikipedia.org/wiki/Include_guard)
#ifndef HEADER_
#define HEADER_

// Define the structures and their type-aliases
typedef struct st { ... } st;
typedef struct sc { ... } sc;

// Declare a couple of other type-aliases
// (Note that I personally recommend against declaring pointer type-aliases)
// (I personally recommend against using type-aliases for simple types
// like the above structures too)
typedef st *segment_table;
typedef sc *segcode;

// Declare a function prototype
void simul(char *new);

#endif  // HEADER_

一个源文件

#include "header.h"

void simul(char *new)
{
    ...
}

第二个源文件

#include "header.h"

int main(void)
{
    sumul("some string");
}

【讨论】:

  • 好的,谢谢,但是对于错误:'->' 的类型参数无效(有'segment_table')我如何在我的头文件中声明它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 2012-03-28
  • 2016-11-01
  • 1970-01-01
相关资源
最近更新 更多