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

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

本文关键字:外部 main 符号 LNK2019 错误 函数 C++ tmainCRTStartup 引用      更新时间:2023-10-16

我是C++的初学者。我试着调试我的代码,因为我确信有一些指针错误,我仍在努力理解,但我无法编译它。代码最初来自我写的一个java程序,它只是一个带有一些比较方法的song类,我已经将它转录到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,因为没有它我无法更新变量