【问题标题】:called object is not a function - how to properly nest headers to call methods被调用对象不是函数 - 如何正确嵌套标题以调用方法
【发布时间】:2013-08-05 20:52:39
【问题描述】:

我有一个分层的 ADT,我试图从客户端调用一个较低的成员。具体来说,我有一个 Graph ADT,它依赖于 List ADT 来存储邻接关系。我在调用客户端中的 List ADT 等方法时遇到问题。

我将 List.h 包含在 Graph.h 中,然后将 Graph.h 包含在客户端中。 (但向客户端添加包含 List.h 不会改变任何内容)。编译器调用Graph.h中的List构造函数没有问题,但是当我调用list方法比如length时却告诉我“调用的对象长度不是函数”。

GraphClient.c 摘录

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

int main(int argc, char** argv) {
List L = newList();
printf("%d",length(L));
}

List.c 摘录

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "List.h"

List newList(void) {
    List L = malloc(sizeof (ListObj));
    L->front = L->back = L->current = NULL;
    L->cursor = -1;
    L->length = 0;
    return (L);
}
int length(List L) {
    if (L == NULL) {
        printf("List error: calling length on NULL List reference");
        exit(1);
    }
    return (L->length);
}

Graph.c 摘录

#include <stdlib.h>
#include "Graph.h"
#include "List.h"

我是否正确分层包含?如果我不尝试在客户端内部调用 List 方法,程序可以正常工作,但如果不这样做,我将无法满足规范。

【问题讨论】:

  • 你的#include "List.h"不是int main(...){...}吗?
  • 我们应该猜测List.h的内容吗?以及您不发布minimal complete example 的原因?

标签: c


【解决方案1】:

在提供的代码中,您表明您需要在 Graph.c 文件中添加 #include "List.h"。这大概是为了让Graph.c代码调用List.c中实现的函数。 GraphClient.c 在这方面没有什么不同。在此处添加#include "List.h"

#include <stdio.h>
#include <stdlib.h>
#include "Graph.h"
#include "List.h"

int main(int argc, char** argv) {
    List L = newList();
    printf("%d",length(L));
}

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 2017-02-21
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    相关资源
    最近更新 更多