簡體   English   中英

錯誤:嘗試使用處理程序時取消引用指向不完整類型的指針

[英]error: dereferencing pointer to incomplete type when trying to use handlers

當我嘗試使用 TournamentKey 做任何事情時,我的代碼不會構建並返回取消引用的錯誤,例如:

TournamentKey new_tournament_key=(TournamentKey)malloc(sizeof(new_tournament_key));
    new_tournament_key->tournamentId=tournament_id;

為什么會這樣? 我想我已經添加了所有內容,包括在 cMake 列表中。

在錦標賽.h

#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 */

在錦標賽中。c

    #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;
    }
}

在國際象棋中。c

#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;
}

為什么會這樣?

因為struct tournament_key_t {...}定義在tournaments.c里面,所以它在chess.c里面是不可見的。 如果你想讓它可見,你可以復制它,或者你可以將它從 tours.c 移動到tournaments.h ,就像chess.c一樣#include "tournaments.h" tournaments.c

 typedef struct tournament_key_t *TournamentKey;

不要使用 typedef 指針——它們很容易混淆。 更喜歡做typedef struct tournament_key_t TournamentKey; 只需編寫* - 您代碼將更加清晰和容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM