c - 错误:尝试使用处理程序时取消引用指向不完整类型的指针
问题描述
当我尝试使用 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;
}
}
在 chess.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(©TournamentData,
©TournamentKey,
&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
。如果你想让它可见,你可以复制它,或者你可以把它从tournaments.c
移到tournaments.h
,就像chess.c
一样#include "tournaments.h"
。
typedef struct tournament_key_t *TournamentKey;
不要使用 typedef 指针——它们很容易混淆。宁愿typedef struct tournament_key_t TournamentKey;
只写- 你的*
代码会更加清晰和容易。
推荐阅读
- python - 如何在提取色矩特征时使偏度值不为空?
- reactjs - 如何在 React 高阶组件中输入 defaultProps
- firebase - 颤动 Firestore 数据检查并导航到新屏幕
- c# - 我无法让 AspNetCoreRateLimit 限制 API 调用
- restriction - 无法将楼层限制添加到 My Miconic PDDL 域代码
- python - Anaconda /Miniconda 导航器不工作我试图安装但无法安装,缺少脚本文件
- keras-tuner - 如何在 keras 调谐器中选择模型以外的最佳模型?
- python - Office365 IMAP 有时无法获取新电子邮件
- html - 使用 sass 在项目中更改一页背景之前的 html
- windows - 如何从 Windows slave 修剪部分 git rev-parse 输出