函数___tmainCRTStartup中引用未解析的外部符号_main

unresolved external symbol _main referenced in function ___tmainCRTStartup

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

当我尝试编译 c++ 控制台应用程序时,我收到此错误:">函数 __tmainCRTStartup 中引用的未解析的外部符号 main"。我已经做了一些搜索,我能找到的就是将我的"链接器"从窗口更改为控制台,反之亦然。这不起作用,我什至尝试创建一个新的控制台应用程序。

我不确定是什么原因造成的,template <typename T>是否有可能在两个文件中出现时引起混淆?这里的任何帮助将不胜感激。

代码如下:

主.cpp:

#include <iostream>
#include "tools.h"
using namespace tools;
template <typename T>
int main()
{
T input1;
T input2;
std::cout << "Enter in 1st number: " << endl;
std::cin >> input1;
std::cout << "Enter in 2nd number: " << endl;
std::cin >> input2;
std::cout << "num1 - num2 = [" << numberDifference(input1, input2) << "]" << endl;
getchar();
getchar();
return 0;
}

工具.h:

#include <iostream>
namespace tools
{
template <typename T>
T numberDifference(T num1, T num2)
{
    if(num1 > num2)
        return num1 - num2;
    else
        return num2 - num1;
}
};

删除main函数的template定义。

或者至少从适当的main函数调用它。

例如

template< typename T >
int templated_main( int c, char** argv )
{
    // What was in your original main function....
}
int main( int c, char** argv )
{
    return templated_main<int>( c, argv );
}

删除 main 之前的template <typename T>。这将使main成为模板函数。

并将T input1; T input2;更改为某些特定类型,例如 intfloat .