c++ - C++错误LNK2019 : unresolved external symbol _main referenced in function _tmainCRTStartup

标签 c++ compiler-errors linker lnk2019

我是C++的完整入门者。我正在尝试调试代码,因为我确定存在一些指针错误,因此我仍在尝试理解但无法编译它。该代码最初来自我编写的Java程序,它只是一个带有一些比较方法的歌曲类,我已将其转录为c++以尝试学习两种语言之间的差异,该代码本身未显示任何错误,但无法显示编译时不会 pop 该错误。我尝试查看该错误的解决方案,但没有任何效果,因此这是我的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;
}

完成的更改:
  • main()移到
  • 类之外
  • 使compareTo()函数public直接被称为
  • static中删除了int Sortcount,因为如果没有它我将无法更新变量。
  • 关于c++ - C++错误LNK2019 : unresolved external symbol _main referenced in function _tmainCRTStartup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29385910/

    相关文章:

    c++ - 双冒号前的 clang 格式中断

    linux - 静态链接库的优缺点是什么?

    C++:根据模板参数填充数组

    c++ - 计算内存使用情况?

    C++ Virtual Studio Hello World 缺少命令?

    android - 在Android Studio中启动新项目时出错

    java - 发现多个文件具有独立于操作系统的路径 'convertGermanToBoolean.properties'

    c++ - 如何摆脱完全专用函数模板的多个定义?

    c++ - 链接器如何找到标准函数和 WinAPI 函数?

    c++ - 当我尝试写入二维数组时出现未处理的异常