gpt4 book ai didi

c - 结构不编译?

转载 作者:太空宇宙 更新时间:2023-11-04 05:45:50 24 4
gpt4 key购买 nike

我正在尝试学习结构并在 .h 和 .c 文件中分别包含以下代码。

    typedef struct{
int lengthOfSong;
int yearRecorded;
} Song;

Song makeSong (int length, int year);
void displaySong(Song theSong);

.c:

Song makeSong(int length, int year){
Song newSong;
newSong.lengthOfSong = length;
newSong.yearRecorded = year;

displaySong(newSong);

return newSong;
}

void displaySong(Song theSong){
printf("This is the length of the song: %i \n This is the year recorded: %i", theSong.lengthOfSong, theSong.yearRecorded);
}

出于某种原因,我收到错误:song.c:1: error: expected '=', ',', ';', 'asm' or 'attribute' before '做歌'song.c:11: 错误:在‘theSong’之前需要‘)’

我做错了什么吗?

编辑主函数(其他函数已经运行):

   #include <stdio.h>
#include "math_functions.h"
#include "song.h"

main(){
int differ = difference(10, 5);
int thesum = sum(3, 7);
printf("differnece: %i, sum: %i \n", differ, thesum);
Song theSong = makeSong(5, 8);

}

最佳答案

displaySong 接受一个参数 theSong 而你正在尝试使用 newSong

您还需要 #include "song.h" 来自 song.c - 错误消息看起来您跳过了它。

关于c - 结构不编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5426049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com