2012-02-16 80 views
0

我正在從一本關於Cocoa和Objective-C的書開始工作。我遵循書本練習,我確信我已經完全像書中的代碼那樣編寫了代碼。但是,編譯代碼時出現編譯器錯誤。即使我從PDF書中複製粘貼它,我仍然遇到編譯錯誤。從書中逐字複製的編譯代碼導致編譯器錯誤

這裏是命令行和輸出:

-MacBook-Pro:ch03 CauldronPoint$ gcc SongTest2.c Song2.c -o SongTest 
Song2.c:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before  
‘createSong’ 
Song2.c:20: error: expected ‘)’ before ‘theSong’ 

下面是代碼:

// 
// Song2.h 
// 

#ifndef _Song2_h 
#define _Song2_h 

typedef struct { 
    char* title; 
    int lengthInSeconds; 
    int yearRecorded; 
} Song; 

Song createSong (char* title, int length, int year); 
void displaySong (Song theSong); 

#endif 

// 
// Song2.c 
// 

#include <stdio.h> 

Song createSong (char* title, int length, int year) { 
    Song mySong; 
    mySong.lengthInSeconds = length; 
    mySong.yearRecorded = year; 
    mySong.title = title; 
    displaySong (mySong); 
    return mySong; 
} 
void displaySong (Song theSong) { 
    printf ("'%s' is %i seconds long ", theSong.title, theSong.lengthInSeconds); 
    printf ("and was recorded in %i\n", theSong.yearRecorded); 
} 

// 
// SongTest2.c 
// 

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

main() { 
    Song allSongs[3]; 
    allSongs[0] = createSong ("Hey Jude", 210, 2004); 
    allSongs[1] = createSong ("Jambi", 256, 1992); 
    allSongs[2] = createSong ("Lightning Crashes", 223, 1997); 
} 

任何人有我如何能得到這個沒有錯誤,請編譯什麼想法?

回答

2

您需要在Song2.c中包含頭文件Song2.h
編譯器抱怨,因爲它不瞭解Song的類型。

// 
// Song2.c 
// 

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