簡體   English   中英

C ++錯誤LNK2019:函數_tmainCRTStartup中引用了未解析的外部符號_main

[英]C++ error LNK2019: unresolved external symbol _main referenced in function _tmainCRTStartup

我是C ++的完整入門者。 我正在嘗試調試代碼,因為我確定存在一些指針錯誤,因此我仍在試圖理解但無法編譯它。 該代碼最初來自我編寫的Java程序,它只是一個帶有一些比較方法的歌曲類,我已將其轉錄為c ++以嘗試學習兩種語言之間的差異,該代碼本身未顯示任何錯誤,但無法顯示編譯時不會彈出該錯誤。 我嘗試查看該錯誤的解決方案,但沒有任何效果,因此這是我的win32控制台項目代碼。 感謝您的幫助。

     // 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;
    }
};
#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. int Sortcount刪除了static ,因為沒有它我無法更新變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM