C ++错误LNK2019:函数_tmainCRTStartup中引用了未解析的外部符号_main

时间:2015-04-01 08:15:15

标签: c++ compiler-errors linker lnk2019

我是C ++的初学者。我正在尝试调试我的代码因为我确定有一些指针错误,所以我仍然试图理解,但我无法编译它。代码最初来自我编写的java程序,它只是一个带有一些比较方法的歌曲类,我将它转录为c ++以尝试学习语言之间的差异,代码本身没有显示任何错误,但它只是可以&#39 ; t编译时没有弹出错误。我已经尝试查看此错误的解决方案,但发现没有任何作用,所以这里是我的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;
    }
};

1 个答案:

答案 0 :(得分: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. static删除了int Sortcount,因为我无法更新变量。