简体   繁体   English

C++ 中未解决的外部问题

[英]unresolved externals in C++

#include <iostream>
using namespace std;

void PrintN(int i, int N)
{
    for (i = 1; i <= N; i++)
        printf("%d\n", i);
    return;
}

when I compiled it, it worked well, but it gave two errors in line 1 when I built it.当我编译它时,它运行良好,但是当我构建它时它在第 1 行给出了两个错误。 One is "Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)" The other is "unresolved externals".一个是“Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)”另一个是“unresolved externals”。 But after I add "int main()", it can work well,but not export the integers from 1 to N on the screen.但是在我添加“int main()”之后,它可以正常工作,但不能在屏幕上导出从 1 到 N 的整数。 I think the void function can work independently even without main function.我认为即使没有主 function,void function 也可以独立工作。 It really made me confused.这真的让我很困惑。

You need to define main , without it the OS has no way to execute your application.您需要定义main ,没有它操作系统将无法执行您的应用程序。 Furthermore, who would execute PrintN ?此外,谁会执行PrintN

#include <iostream>

void PrintN( int i, int n )
{
    for (; i < n; i++ )
        std::cout << i << '\n';
}

int main( )
{
    PrintN( 10, 20 ); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM