简体   繁体   English

如何在另一个结构中使用结构的 function?

[英]How to use a function of a struct in another struct?

i'm doing a simple project in C in which, for the moment, i use two structs: Player and Card.我正在 C 中做一个简单的项目,目前我使用两个结构:Player 和 Card。 I have created the 2 struct in different header files, because functions in Player use Card, but also other elements that I haven't already done.我在不同的 header 文件中创建了 2 结构,因为 Player 中的函数使用 Card,但还有其他我尚未完成的元素。 When I try to use getId() in Player, the VSCode's compiler says:当我尝试在 Player 中使用 getId() 时,VSCode 的编译器会说:

reference to external symbol _getId not resolved in _discardCard() _discardCard() 中未解析对外部符号 _getId 的引用

card.h code is: card.h 代码是:

#include <stdbool.h> 
typedef struct card{
    int id;
    bool black;
    int numElems;
    char* text[3];
}card;

card* initCard(int id,bool black,char* text[],int numElems)
int getId(card* c);

card.c code is: card.c 代码为:

#include "carta.h"
#include<stdio.h>
#include <stdbool.h>
#include <stdlib.h>

...
int getId(carta* c){
    return c->id;
}

Instead the code for Player.h is:相反, Player.h 的代码是:

#include "card.h"
#define CARDSMAX 5

typedef struct{
    bool master;
    int id;
    int points;
    char* nickname;
    card* cards[CARDSMAX];
    int NumCards;
}player;

... //other functions

card* discardCard(int id,player* g);

The code for Player.c is: Player.c 的代码是:

#include "player.h"
#include <stdio.h>
#include <stdlib.h>
player* initPlayer(char* nickname,int id){
    player* g=(player*) malloc(sizeof (player));
    g->id=id;
    g->nickname=nickname;
    g->master=false;
    g->points=0;
    g->NumCards=0;
    for(int i=0;i<CARDSMAX;i++){
        g->cards[i]=(card*)malloc(sizeof(card));
    }
    return g;
}

....   

card* DiscardCard(int id,player* g){
    for(int i=0;i<CARDSMAX;i++){
        card* c=g->cards[i];
        if(getId(c)==id){
            card* e= g->cards[i];
            g->cards[i]=NULL;
            g->NumCards--;
            return e;
        }
    }
    return NULL;
}

Can someone help me?有人能帮我吗? (If you see some inconsistency in the code, it'is because i tried a fast translation from my language) (如果您发现代码中有些不一致,那是因为我尝试了从我的语言快速翻译)

So, as suggested, the problem was that in Windows' compiler, when you use struct that use function defined in other structs,it is required to pass the.obj file during the compilation.因此,正如建议的那样,问题在于在 Windows 的编译器中,当您使用使用其他结构中定义的 function 的结构时,需要在编译期间传递 .obj 文件。 For doing this you need to compile every.c file that is used by others singulary and then link the.obj file created in this fase.为此,您需要编译其他人使用的每个.c 文件,然后链接在此过程中创建的.obj 文件。 So in my case the first step is to compile:所以在我的情况下,第一步是编译:

cl card.c

that create a card.obj (the Windows' compiler could show you a message that says something like:"The start point is not indicated" if you don't use a main in this first file, don't worry and carry on with this procedure) and then I compile:创建一个 card.obj (Windows 的编译器可能会向您显示一条消息,例如:“未指示起点”如果您不在第一个文件中使用 main,请不要担心并继续这个过程)然后我编译:

cl player.c /link card.obj

So in this way you pass the compiled object in which it's provided the implementation of all the function in the struct.因此,通过这种方式,您可以传递已编译的 object,其中提供了结构中所有 function 的实现。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM