C++ 未定义对'的引用;WinMain@16';
我得到一个[Linker Error]未定义的对'WinMain@16“我无法解决这个问题。我正在使用Dev-C++-在我的项目设置中,选择了“Win32 Console”,因为我希望它是一个控制台应用程序 示例标题(Test.h):C++ 未定义对'的引用;WinMain@16';,c++,header,linker,declaration,C++,Header,Linker,Declaration,我得到一个[Linker Error]未定义的对'WinMain@16“我无法解决这个问题。我正在使用Dev-C++-在我的项目设置中,选择了“Win32 Console”,因为我希望它是一个控制台应用程序 示例标题(Test.h): \ifndef测试 #定义测试 #包括 使用名称空间std; 课堂测试{ 私人: 智力测验; 公众: int main(); }; #恩迪夫 示例.cpp文件 #include<iostream> #include "Test.h" using na
\ifndef测试
#定义测试
#包括
使用名称空间std;
课堂测试{
私人:
智力测验;
公众:
int main();
};
#恩迪夫
示例.cpp文件
#include<iostream>
#include "Test.h"
using namespace std;
int Test::main(){
/* EXAMPLE */
cout << "Enter Test" <<endl;
cin >> testing;
cout << "----------------------------"<<endl;
system("pause");
return 0;
}
#包括
#包括“Test.h”
使用名称空间std;
int Test::main(){
/*范例*/
答案在评论中提供,但要点如下:
#ifndef TEST_H
#define TEST_H
#include<string>
using namespace std;
class Test {
private:
int testing;
public:
int main();
};
int Test::main(){
/* EXAMPLE */
cout << "Enter Test" <<endl;
cin >> testing;
cout << "----------------------------"<<endl;
system("pause");
return 0;
}
#endif
\ifndef测试
#定义测试
#包括
使用名称空间std;
课堂测试{
私人:
智力测验;
公众:
int main();
};
int Test::main(){
/*范例*/
cout噢,既然有更好的方法,你为什么要使用system(“暂停”)
?(你可以在这里阅读system()
为什么是邪恶的:)
为什么不这样做呢:
void PressEnterToContinue()
{
std::cout << "Press ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
void按ENTERTOCONTINUE()
{
std::coutmain
必须是一个自由函数。将main放入类中看起来非常像Java:-(您可以尝试同时拥有一个成员函数和一个全局函数,int main(){return Test::main();}
。程序的“main”应该是“不在任何命名空间中”。将Test::
添加到main将使其成为一个完全不同的函数-就像调用它MatsMain
或kerflunk
是行不通的。现在还不完全清楚您在这里试图做什么,但很明显,您需要在代码中的某个地方有一个不带命名空间的main
。这样可以创建一个测试
对象,并在该类中调用main
,如果您愿意的话。非常感谢您的解决方案,但是它给了我一个“从“class”转换为非标量类型“class”requested”的错误。我想它可能必须是main(),但从来都不知道您可以在头文件中创建函数。我有输入(cin)输出(Cout),if语句,while循环(…)不确定是否会导致任何问题。程序本身运行之前,我试图改进它使用头文件!使从java到C++的转变到目前为止是一个噩梦!(,现在进行更改哇,真的那么简单?哎哟,对不起那个人!非常感谢它现在已经编译并运行了。:-)
#include<iostream>
#include "Test.h"
using namespace std;
int main(){
/* EXAMPLE */
Test *testObject = new Test();
testObject->main();
delete(testObject);
system("pause");
return 0;
}
void PressEnterToContinue()
{
std::cout << "Press ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}