[英]CS50 Plurality fails to compile
我總是在我的本地機器上處理 Psets 並用char *
替換string
,所以我不必在我的頭文件中使用 CS50 庫。 這是我對為什么我的代碼在運行check50
時無法編譯的唯一解釋
代碼在我的機器和 CS50 IDE 上都按預期工作,但check50
仍然給我這個錯誤:
code failed to compile
Log
running clang plurality.c -o plurality -std=c11 -ggdb -lm -lcs50...
running clang plurality_test.c -o plurality_test -std=c11 -ggdb -lm -lcs50...
plurality_test.c:68:1: warning: control may reach end of non-void function
[-Wreturn-type]
}
^
plurality_test.c:109:20: error: unknown type name 'string'
int main(int argc, string argv[])
^
1 warning and 1 error generated.
復數.c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
// Max number of candidates
#define MAX 9
// Candidates have name and vote count
typedef struct
{
char *name;
int votes;
} candidate;
// Array of candidates
candidate candidates[MAX];
// Number of candidates
int candidate_count;
// Function prototypes
bool vote(char name[]);
void print_winner(void);
int search(char name[]);
int main(int argc, char *argv[])
{
// Check for invalid usage
if (argc < 2)
{
printf("Usage: plurality [candidate ...]\n");
return 1;
}
// Populate array of candidates
candidate_count = argc - 1;
if (candidate_count > MAX)
{
printf("Maximum number of candidates is %i\n", MAX);
return 2;
}
for (int i = 0; i < candidate_count; i++)
{
candidates[i].name = argv[i + 1];
candidates[i].votes = 0;
}
int voter_count;
printf("Number of voters: ");
scanf("%i", &voter_count);
// Loop over all voters
for (int i = 0; i < voter_count; i++)
{
char name[10];
printf("Vote: ");
scanf("%s", name);
// Check for invalid vote
if (!vote(name))
{
printf("Invalid vote.\n");
}
}
// Display winner of election
print_winner();
}
// Update vote totals given a new vote
bool vote(char name[])
{
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0)
{
candidates[i].votes++;
return true;
}
}
return false;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
int prev = -1;
int curr;
int id;
for (int i = 0; i < candidate_count + 1; i++)
{
curr = candidates[i].votes;
if (curr > prev)
{
id = i;
prev = candidates[id].votes;
}
}
printf("%s\n", candidates[id].name);
return;
}
@Blauelf 回答了這個:
檢查器代碼重命名您的main
函數並附加它自己的。
警告存在是因為main
是唯一一個返回非void
函數,如果您不顯式返回值(默認情況下它將返回 0),其返回值仍被定義。 對於其他函數,返回值是什么取決於編譯器,通常取決於 CPU 架構。 通過重命名函數,此特殊屬性不再適用。 沒問題,因為它只是一個警告,並且永遠不會調用該函數。
然后他們附加自己的main
函數,這就是錯誤發生的地方:那個人希望你#include <cs50.h>
。 確保為提交添加此行,即使您自己不使用其功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.