2015-04-01 69 views
0

我是一個完整的初學C++。我試圖調試我的代碼,因爲我確定有一些指針錯誤,並且我仍然試圖理解,但我無法編譯它。該代碼最初來自我編寫的java程序,它只是一個帶有一些比較方法的歌類,我已將它轉錄爲C++以嘗試瞭解這些語言之間的差異,代碼本身沒有顯示錯誤,但它不能編譯沒有彈出錯誤。我試着尋找解決這個錯誤的方法,但沒有發現任何作用,所以這裏是我的代碼爲win32控制檯項目。感謝您的幫助。C++錯誤LNK2019:無法解析的外部符號_main在函數中引用_tmainCRTStartup

 // ConsoleApplication4.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <string> 
#include <iostream> 

using namespace std; 

class Song { 
private: 
    string Artist; 
    string Title; 
    string Lyrics; 
public: 
    static int Sortcount; 
    static int Searchcount; 

    Song(string A_Artist, string T_Title, string L_Lyrics) { 

     Artist = A_Artist; 
     Title = T_Title; 
     Lyrics = L_Lyrics; 
    } 

    //Compares the artist of one song to another. 
    class ArtistComparator { 

    public: 
     int compare(Song *o1, Song *o2) { 
      string Artist1 = (*o1).Artist; 
      string Artist2 = (*o2).Artist; 
      Searchcount++; 

      if (Artist1.compare(Artist2) == 0){ 
       return 0; 
      } 
      Searchcount++; 
      return (*o1).Artist.compare((*o2).Artist); 
     } 

    }; 

    //Compares the title of one song to another. 
    class TitleComparator { 

    public: 

     int compare(Song arg0, Song arg1) { 

      string Title1 = arg0.Title; 
      string Title2 = arg1.Title; 
      return Title1.compare(Title2); 
     } 
    }; 


public: 

    //Testing method to make sure the Song class works and 
    //the compareTo method works. 
    int main(int argc, char** argv){ 
     Song test1 = Song("cat", "bat", "this is not a real song"); 
     Song test2 = Song("cat", "apple", "also not a real song"); 
     int compareResult = test1.compareTo(test2); 
     if (compareResult == -1){ 
      std::cout << "test1 comes first"; 
     } 
     else{ 
      cout << "test2 comes first"; 
      cout << test2.toString(); 
     } 

    }; 

    string getArtist(){ 
     return Artist; 
    }; 

    string getTitle(){ 
     return Title; 
    }; 

    string getLyrics(){ 
     return Lyrics; 
    }; 

    string toString(){ 
     return Artist + ", " + Title + ", " + Lyrics; 
    }; 

    //compareTo method used for sorting songs. 
    //increments Sortcount each time it is called to keep track 
    //of the efficiency of the sort algorithm. 
private: 
    int compareTo(Song other){ 
     Sortcount++; 
     int art = Artist.compare(other.Artist); 
     if (art == 0){ 

      return Title.compare(other.Title); 
     } 
     else 
      return art; 
    } 
}; 
+6

與Java不同,C++(和C)中的'main'函數不能是成員函數,它必須是獨立函數。 – 2015-04-01 08:18:46

+0

只需在類定義之外放置main()。你已經做到了。 – 2015-04-01 08:23:37

回答

0
#include "stdafx.h" 
#include <string> 
#include <iostream> 

using namespace std; 

class Song { 
private: 
    string Artist; 
    string Title; 
    string Lyrics; 
public: 
    int Sortcount; 
    static int Searchcount; 

    Song(string A_Artist, string T_Title, string L_Lyrics) { 

     Artist = A_Artist; 
     Title = T_Title; 
     Lyrics = L_Lyrics; 
    } 

    //Compares the artist of one song to another. 
    class ArtistComparator { 

    public: 
     int compare(Song *o1, Song *o2) { 
      string Artist1 = (*o1).Artist; 
      string Artist2 = (*o2).Artist; 
      Searchcount++; 

      if (Artist1.compare(Artist2) == 0){ 
       return 0; 
      } 
      Searchcount++; 
      return (*o1).Artist.compare((*o2).Artist); 
     } 

    }; 

    //Compares the title of one song to another. 
    class TitleComparator { 

    public: 

     int compare(Song arg0, Song arg1) { 

      string Title1 = arg0.Title; 
      string Title2 = arg1.Title; 
      return Title1.compare(Title2); 
     } 
    }; 


public: 

    //Testing method to make sure the Song class works and 
    //the compareTo method works. 


    string getArtist(){ 
     return Artist; 
    }; 

    string getTitle(){ 
     return Title; 
    }; 

    string getLyrics(){ 
     return Lyrics; 
    }; 

    string toString(){ 
     return Artist + ", " + Title + ", " + Lyrics; 
    }; 

    //compareTo method used for sorting songs. 
    //increments Sortcount each time it is called to keep track 
    //of the efficiency of the sort algorithm. 
    int compareTo(Song other){ 
     Sortcount++; 
     int art = Artist.compare(other.Artist); 
     if (art == 0){ 

      return Title.compare(other.Title); 
     } 
     else 
      return art; 
    } 
}; 

int main(int argc, char** argv){ 
    Song test1 = Song("cat", "bat", "this is not a real song"); 
    Song test2 = Song("cat", "apple", "also not a real song"); 
    int compareResult = test1.compareTo(test2); 
    if (compareResult == -1){ 
     std::cout << "test1 comes first"; 
    } 
    else{ 
     cout << "test2 comes first"; 
     cout << test2.toString(); 
    } 
    getchar(); 
    return 0; 
} 

變化做:

  1. 感動main()
  2. 製造compareTo()功能public,因爲它是直接叫
  3. 刪除staticint Sortcount,因爲我無法更新外沒有這個變量。
+0

* ...只有靜態函數可以對靜態變量進行更改*這是不正確的。當SortCount是靜態的時候它失敗的原因是因爲兩個靜態變量只聲明瞭但沒有定義。在類定義中,靜態成員變量的**聲明**是'static int SortCount;'。靜態成員變量的**定義**是int Song :: SortCount = 0;出現在類定義之外的單個源文件中。僅添加聲明並忘記定義會導致鏈接錯誤。這不會發生在其他靜態成員上,因爲它沒有被使用。 – Ionut 2015-04-01 09:12:19

+0

謝謝。我已經更新了評論:) – 2015-04-01 09:17:29

+0

我看到了,但正確的解決方法是爲這些靜態變量添加定義,而不是刪除'static'限定符。刪除'static'會改變代碼的行爲。 – Ionut 2015-04-01 09:22:24

相關問題