You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode运行1999C代码遇`WinMain@16`未定义引用错误求助

问题与解决:C++代码链接错误 undefined reference to WinMain@16

问题描述

运行编号为1999C的C++代码时出现如下链接错误:

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

原代码如下:

#include<bits/stdc++.h>
using namespace std;

int mian(){

int t;
cin>>t;

while(t-->0){
    int n,s,m;
    cin>>n>>s>>m;
    vector<pair<int,int>>t;
    for(int i=0;i<n;i++){
            int e1;
            int e2;
            cin>>e1>>e2;
            t.push_back({e1,e2});
    }
        string str="YES";
        if(abs(t[0].first)>=s){
            
            cout<<str<<endl;return 0;}

        if(abs(m-t[n-1].second)>=s){

            cout<<str<<endl;return 0;}

    for(int i=0;i<n-2;i++){

        if(abs(t[i].second-t[i+1].first)>=s)

        {cout<<str<<endl;return 0;}}



    cout<<"NO"<<endl;
    
}



    return 0;
}

问题原因及修复方案

1. 入口函数拼写错误(直接导致链接错误)

程序入口函数main被误写成mian,编译器找不到标准C++程序的入口点,会默认尝试寻找Windows程序的WinMain入口,从而抛出该链接错误。
修复:将int mian()修改为int main()。

2. 变量名冲突

代码中存在两个同名变量t:

  • 外层的测试用例计数变量int t;
  • 循环内部的区间存储容器vector<pair<int,int>>t;
    这会导致编译阶段的变量作用域冲突。
    修复:将vector的变量名改为其他名称,比如intervals,并替换所有对应的引用。

3. 循环边界错误

原代码中for(int i=0;i<n-2;i++)的边界不正确,无法遍历所有相邻区间对,正确的边界应为i < n-1。

4. 测试用例处理逻辑错误

原代码中找到符合条件的情况就直接return 0,会导致程序处理完第一个测试用例就退出,无法处理后续用例,需改为continue或跳转标签。

修复后的完整代码

#include<bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin>>t;

    while(t-->0){
        int n,s,m;
        cin>>n>>s>>m;
        vector<pair<int,int>>intervals;
        for(int i=0;i<n;i++){
            int e1,e2;
            cin>>e1>>e2;
            intervals.push_back({e1,e2});
        }
        
        const string str = "YES";
        // 检查起始点到第一个区间的间隔
        if(abs(intervals[0].first) >= s){
            cout<<str<<endl;
            continue;
        }

        // 检查最后一个区间到终点的间隔
        if(abs(m - intervals[n-1].second) >= s){
            cout<<str<<endl;
            continue;
        }

        // 检查相邻区间之间的间隔
        bool found = false;
        for(int i=0;i<n-1;i++){
            if(abs(intervals[i].second - intervals[i+1].first) >= s){
                cout<<str<<endl;
                found = true;
                break;
            }
        }

        if(!found){
            cout<<"NO"<<endl;
        }
    }

    return 0;
}

内容的提问来源于stack exchange,提问作者Aditya Rana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 21:35:09