C++错误LNK2019:函数_tmainCRTStartup中引用的未解析外部符号_main
C++ error LNK2019: unresolved external symbol _main referenced in function _tmainCRTStartup
我是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;
}
完成的更改:
- 将
main()
移动到类外 - 使
compareTo()
函数public
成为直接调用的函数 - 从
int Sortcount
中删除了static
,因为没有它我无法更新变量
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 在函数内部的声明中初始化数组,并在外部使用它
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 使外部项目可用于find_package CMake
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 不确定要在我的main中放入什么才能使我的代码正常工作
- C++:来自外部文件的Trivia
- 从函数角度看ID到文件路径的内部与外部映射
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 未解析的外部符号__imp_glewInit函数 main 中引用
- main.obj : 错误 LNK2019: 未解析的外部符号 公共: __cdecl.
- C 外部阵列在标头中声明,在main.cpp中不可用
- QT错误:LNK1120:1未解决的外部Main.OBJ:-1:错误:LNK2019运行QMAKE
- C++ 如何从 main() 外部访问在 main() 中初始化的对象
- visual main.obj:错误LNK2019:C++中未解析的外部符号
- 在C++中清除数组会在外部函数中而不是在main()中产生编译器错误
- C++对象在main外部初始化