c - 数据库程序: array type has incomplete element type

标签 c arrays unix compiler-errors

 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: error: array type has incomplete element type

prototypes.h:7:24: error: expected ')' before '*' token

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

最佳答案

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

类似以下内容应该会导致完全相同的错误:

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)
<小时/>

没有注意到它不仅仅是提示不完整类型,而是不完整元素类型

这本质上意味着 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 释放它) .

关于c - 数据库程序: array type has incomplete element type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295152/

相关文章:

c - 链表段错误。 C

c++ - 如何判断 double float 是否可以安全地存储为单精度 float ?

c - 如何提高多线程读取文件的性能?

bash - 使用一个命令的输出作为 sed 中的替换文本

linux - bash shell 中的管道是如何实现的?

c - 在 C 中读取/写入 UNIX 管道

java - 如何在 Maven 项目中从 C 运行 java 类文件

java - 为什么我不能在 for 循环中第一次输入另一个字符串?

c++ - 使用指针对数组进行动态内存分配的好处

php - 插入忽略独特的电子邮件 laravel 5.4