1

http://cocoadevcentral.com/articles/000081.phpから文字通りコピーして貼り付けた C ファイルをコンパイルしようとしているので、構文エラーの種類はないと思いますが、エラーが発生し続けます。何が間違っているのかわかりません。TextWrangler というテキスト エディターを使用して、書き込みとファイルを保存しています。端末ツールと一緒にXcodeをダウンロードしたので、それも問題にはなりません。

エラーは次のとおりです。

testc4.c:4: error: expected identifier or ‘(’ before ‘)’ token
song.c:5: error: function definition declared ‘typedef’
song.c:5: error: return type is an incomplete type
song.c: In function ‘make_song’:
song.c:6: error: ‘Song’ undeclared (first use in this function)
song.c:6: error: (Each undeclared identifier is reported only once
song.c:6: error: for each function it appears in.)
song.c:6: error: expected ‘;’ before ‘newSong’
song.c:8: error: ‘newSong’ undeclared (first use in this function)
song.c:12: warning: ‘return’ with a value, in function returning void
song.c: At top level:
song.c:15: error: expected ‘)’ before ‘theSong’

プログラムをコンパイルするために使用しているコマンドは次のとおりです。

gcc testc4.c song.c -o testc4

必要に応じてファイルを投稿できますが、チュートリアルから直接コピーされます。テスト ファイルには、必要な名前を付けるのではなく、testc4.c という名前を付けました。

編集:(コーディングする必要があると思いました)

testc4.c ファイル:

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

main ()
{
  Song firstSong  = make_song (210, 2004);
  Song secondSong = make_song (256, 1992);

  Song thirdSong  = { 223, 1997 };
  display_song ( thirdSong );

  Song fourthSong = { 199, 2003 };
}

song.c ファイル:

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

Song make_song (int seconds, int year)
{
  Song newSong;

  newSong.lengthInSeconds = seconds;
  newSong.yearRecorded    = year;
  display_song (newSong);

  return newSong;
}

void display_song (Song theSong)
{
  printf ("the song is %i seconds long ", theSong.lengthInSeconds);
  printf ("and was made in %i\n", theSong.yearRecorded);
}

song.h ファイル

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

Song  make_song    (int seconds, int year);
void  display_song (Song theSong);

これらはサイトから直接コピーされます。最初にそれらを入力しましたが、うまくいかなかったときにコピーして貼り付けました。

4

1 に答える 1