【问题标题】:Database program: array type has incomplete element type数据库程序:数组类型有不完整的元素类型
【发布时间】:2015-05-31 10:54:43
【问题描述】:
 ATTRIBUTES*  addRelation(char*,char*,ATTRIBUTES*);
 void nattr(ATTRIBUTES*);
 void tuplelen(ATTRIBUTES*);
 void infattr(char*,ATTRIBUTES*);
 void addValues(ATTRIBUTES*,char*);
 int count(VALUES*);
 void project(ATTRIBUTES*,char*);
 void select(char*,char*,char*,ATTRIBUTES*);
 int inStringArray(char*[][],int,char*);

我不断收到这个错误,如果我在数组中包含一个值会令我的程序出错,我会感到困惑吗?

prototypes.h:9:1: 错误:数组类型的元素类型不完整

prototypes.h:7:24: 错误:在 '*' 标记之前需要 ')'

我也有这个错误,但我的语法是正确的。我没有正确编译这个头文件吗? 我一直在用gcc

【问题讨论】:

  • compiling this header file correctly .. 怎么样?
  • 我正在做一个makefile,所以我已经单独编译了所有其他文件,这是唯一给我带来麻烦的地方。我已经编译了它 gcc prototypes.h,即使我制作了程序,它也会显示这个错误,所以它就是这个头文件
  • 你必须在int inStringArray(char*[][],int,char*);中提供第二个维度。
  • 我的问题是什么维度?我已经搜索了错误,但看不到哪里出错了。我只是想创建数组.. 如果有意义的话。我不确定我是否在澄清对不起我是 c 新手,这就是我问的原因

标签: c arrays unix compiler-errors


【解决方案1】:

这样的错误通常是由缺少(完整)声明引起的。换句话说:由于前向声明,您的一种类型是已知的,但编译器不知道该类型的实际结构(这使得无法知道该数组或其元素之一的长度)。

类似下面的内容应该会导致同样的错误:

struct Data;

Data myData[50]; // The compiler doesn't know how much data is needed

要解决此问题,您必须包含正确的头文件或添加完整的声明(确保不重复定义):

struct Data; // This line is now obsolete in this simple example

struct Data {
    int someInteger;
};

Data myData[50]; // The compiler now knows this is essentially 50 integers (+padding)

没有注意到,它不仅仅是在抱怨incomplete type,而是在抱怨incomplete element type

这实质上意味着 C++ 无法确定多维数组的大小。

如果你想定义或传递一个 n 维数组,你必须记住,你只允许一个可变长度的维度(因为编译器将无法确定正确的大小,否则)。简而言之,[] 最多只能出现一次。

这里有一些例子:

void doSomething(int args[]) {
    // 1 dimension, every element is the length of one integer
    args[0]; // this is the first integer
    args[1]; // this is the second integer (offset is args + sizeof(int))
}

void doSomething(int args[][2]) {
    // 2 dimensions, every element is the length of two integers
    args[0]; // this is the first set of integers
    args[1]; // this is the second set (offset is args + sizeof(int[2]))
}

void doSomething(int args[][]) {
    // 2 dimensions, no idea how long an element is
    args[0]; // this is the first set of integers
    args[1]; // this is the second set (offset is args + sizeof(int[])... oops? how long is that?)
}

作为一种解决方法,您可以只传递指针并隐藏您拥有数组的事实(因为指针的长度是已知的)。唯一的缺点是编译器将不再知道您确实传递的是数组而不是单个值(通过引用)。

void doSomething(int args*[]) {
    // 2 dimensions, set of integers
    args[0]; // this is the first set of integers
    args[1]; // this is the second set (offset is args + sizeof(int*))
}

回到你的实际问题:

只需替换行

int inStringArray(char*[][],int,char*);

int inStringArray(char**[],int,char*);

请记住,您可能还需要更新代码的其他部分,并且必须小心以防将该数组传递到某处(例如,使用 delete 释放它)。

【讨论】:

  • 但我没有 struct 。错误与这一行有关: int inStringArray(char*[][],int,char*);
  • @AsheMendez 这是哪个错误?第 7 行消息还是第 9 行?我猜是9号线吗?更新我的答案...
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多