首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:尝试使用处理程序时取消引用指向不完整类型的指针

错误:尝试使用处理程序时取消引用指向不完整类型的指针
EN

Stack Overflow用户
提问于 2021-05-23 19:12:18
回答 1查看 31关注 0票数 0

当我尝试使用TournamentKey做任何事情时,我的代码不会构建并返回取消引用的错误,例如:

代码语言:javascript
复制
TournamentKey new_tournament_key=(TournamentKey)malloc(sizeof(new_tournament_key));
    new_tournament_key->tournamentId=tournament_id;

为什么会发生这种情况?我想我已经添加了所有内容,包括在cMake列表中。

在testnaments.h中

代码语言:javascript
复制
#ifndef CHESS_TOURNAMENTS_H
#define CHESS_TOURNAMENTS_H

#include <stdbool.h>

typedef struct tournament_t *Tournament;
typedef struct tournament_key_t *TournamentKey;

MapDataElement copyTournamentData(MapDataElement dataElement);

MapKeyElement copyTournamentKey(MapKeyElement key);

void freeTournamentData(MapDataElement value);

void freeTournamentKey(MapKeyElement key);

int compareTournamentKeys(MapKeyElement key1, MapKeyElement key2);

#endif /* CHESS_TOURNAMENTS_H */

在testnaments.c中

代码语言:javascript
复制
    #include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#include "tournaments.h"

#define KEYS_EQUAL 0
#define FIRST_KEY_BIGGER 1
#define SECOND_KEY_BIGGER -1
#define INVALID_KEY 2

struct tournament_t{
    int tournamentId;
    char* tournamentsPlace;
    int numberOfGames;
    int tournamentWinner;
    int maxGamesPerPlayer;
    Map games;
};

struct tournament_key_t{
    int tournamentId;
};

MapDataElement copyTournamentData(MapDataElement dataElement)
{
    Tournament new_tournament = (Tournament) malloc(sizeof *new_tournament);
    if(!new_tournament){
        return NULL;
    }

    new_tournament->tournamentWinner = ((Tournament) dataElement)->tournamentWinner;
    new_tournament->numberOfGames = ((Tournament) dataElement)->numberOfGames;
    new_tournament->tournamentId = ((Tournament) dataElement)->tournamentId;
    new_tournament->maxGamesPerPlayer = ((Tournament) dataElement)->maxGamesPerPlayer;
    new_tournament->tournamentsPlace = malloc(strlen(((Tournament) dataElement)->tournamentsPlace)+1);
    if (new_tournament->tournamentsPlace) {
        memset(new_tournament->tournamentsPlace, '\0', sizeof *new_tournament->tournamentsPlace);
        strcpy(new_tournament->tournamentsPlace, ((Tournament) dataElement)->tournamentsPlace);
    }
    new_tournament->games = mapCopy(((Tournament) dataElement)->games);

    return new_tournament;
}

MapKeyElement copyTournamentKey(MapKeyElement key)
{
//    TournamentKey temp_key = (TournamentKey)key;
    TournamentKey new_key = (TournamentKey)malloc(sizeof *new_key);
    if (!new_key){
        return NULL;
    }
    new_key->tournamentId = ((TournamentKey) key)->tournamentId;
    return (MapKeyElement)new_key;
}

void freeTournamentData(MapDataElement value)
{
    // value is a data element in Tournaments Map i.e. its a tournament.
    // First mapDestroy the map of games in the current tournamnet,
    // Then free the tournament intself.
    Tournament curr_tour = (Tournament)value;
//    free(curr_tour->tournamentsPlace);
    mapDestroy(curr_tour->games);
    free(curr_tour);
}

void freeTournamentKey(MapKeyElement key)
{
    free(key);
}

int compareTournamentKeys(MapKeyElement key1, MapKeyElement key2)
{
    if(!key1 || !key2)
    {
        printf("compareTournamentKeys: You have got a NULL key\n");
        return INVALID_KEY;
    }

    TournamentKey new_key1 = (TournamentKey)key1;
    TournamentKey new_key2 = (TournamentKey)key2;

    if(new_key1->tournamentId > new_key2->tournamentId)
    {
        return FIRST_KEY_BIGGER;
    }
    else if(new_key1->tournamentId == new_key2->tournamentId)
    {
        return KEYS_EQUAL;
    }
    else
    {
        return SECOND_KEY_BIGGER;
    }
}

在chess.c中

代码语言:javascript
复制
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "map.h"
#include "chess.h"
#include "games.h"
#include "tournaments.h"

#define KEYS_EQUAL 0
#define FIRST_KEY_BIGGER 1
#define SECOND_KEY_BIGGER -1
#define INVALID_KEY 2

struct chess_t{
    Map mapOfTournaments;
};

ChessSystem chessCreate()
{
    ChessSystem chess_game_system=(ChessSystem)malloc(sizeof(*chess_game_system));
    if(!chess_game_system)
        return NULL;

    chess_game_system->mapOfTournaments=mapCreate(&copyTournamentData,
                                                  &copyTournamentKey,
                                                  &freeTournamentData,
                                                  &freeTournamentKey,
                                                  &compareTournamentKeys);

    return chess_game_system;
}

int playerGamesPlayed(ChessSystem chess, int tournament_id, int player_id)
{
    int num_of_games_played=0;
    TournamentKey new_tournament_key=(TournamentKey)malloc(sizeof(new_tournament_key));
    new_tournament_key->tournamentId=tournament_id;
    Tournament curr_tournament=mapGet(chess->mapOfTournaments, new_tournament_key);
    MAP_FOREACH(GameKey, curr_game_key, curr_tournament->games){
        if (curr_game_key){
            int curr_player_first_id=curr_game_key->firstPlayerId;
            int curr_player_second_id=curr_game_key->secondPlayerId;

            if(curr_player_first_id == player_id || curr_player_second_id == player_id){
                num_of_games_played++;
            }
            freeGameKey(curr_game_key);
        }
    }
    freeTournamentKey(new_tournament_key);
    return num_of_games_played;
}
EN

回答 1

Stack Overflow用户

发布于 2021-05-23 19:16:38

为什么会发生这种情况?

因为struct tournament_key_t { ...}定义在tournaments.c中,所以它在chess.c中不可见。如果你想让它可见,你可以复制它,或者你可以把它从tournaments.c移动到tournaments.h,就像chess.c#include "tournaments.h"做的那样。

类型定义结构tournament_key_t *TournamentKey;

不要使用类型定义函数指针--它们很容易混淆。我更喜欢做typedef struct tournament_key_t TournamentKey;,只写* --你的代码会更清晰,更容易。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67659089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档