C++ Clang编译时报错:error: expected unqualified-id

本文介绍了在使用C++和Clang编译时遇到'expected unqualified-id'错误的问题。原因是缺少main函数,解决方法是确保每个C++程序包含一个main函数。这是一个常见的初学者陷阱,提醒开发者注意避免忘记写main函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题概述

今天在学习 Vector 容器时发现CLion抛出了这样一个错误,如下图
错误信息
使用Clang编译后这个问题依旧存在

~/Documents/Clion_Project/Learning/L11.cpp:12:1: error: expected unqualified-id
for (int i = 0; i < v.size(); i++)
^
1 error generated.

原因

没有写 main 函数

解决方法

#include <vector>
using namespace std;

int main()
{
    struct Vertex
    {
        int a;
        float b;
    };

    vector<Vertex> v;
    for (int i = 0; i < v.size(); i++)
    {

    }
}

反思

注意 main 函数不要忘写了!!!

H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp: In function 'int main()': H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:16: error: expected unqualified-id before '[' token for (auto& [a, b] : edges) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:16: error: expected ';' before '[' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:17: error: 'a' was not declared in this scope for (auto& [a, b] : edges) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:20: error: 'b' was not declared in this scope for (auto& [a, b] : edges) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp: In lambda function: H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:23: error: expected '{' before ':' token for (auto& [a, b] : edges) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp: In function 'int main()': H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:23: error: expected ';' before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:23: error: expected primary-expression before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:23: error: expected ')' before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:54:23: error: expected primary-expression before ':' token 编译失败!错误信息: Command '['g++', '-std=c++14', 'H:\\小视野 信竞题 -对拍\\课程二 43-71课\\第54课 图论专题 欧拉图\\Problem C 相框_std.cpp', '-o', 'H:\\小视野 信竞题 -对拍\\课程二 43-71课\\第54课 图论专题 欧拉图\\Problem C 相框_std.exe']' returned non-zero exit status 1. Traceback (most recent call last): File "H:\小视野 信竞题 -对拍\对拍.py", line 105, in <module> diffTest() File "H:\小视野 信竞题 -对拍\对拍.py", line
03-28
&[a, b] : wires) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:16: error: expected ';' before '[' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:17: error: 'a' was not declared in this scope for (auto &[a, b] : wires) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:20: error: 'b' was not declared in this scope for (auto &[a, b] : wires) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp: In lambda function: H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:23: error: expected '{' before ':' token for (auto &[a, b] : wires) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp: In function 'int main()': H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:23: error: expected ';' before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:23: error: expected primary-expression before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:23: error: expected ')' before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:23:23: error: expected primary-expression before ':' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:33:15: error: expected unqualified-id before '[' token for (auto [a, b] : wires) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:33:15: error: expected ';' before '[' token H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:33:16: error: 'a' was not declared in this scope for (auto [a, b] : wires) { ^ H:\С��Ұ �ž��� -����\�γ̶� 43-71��\��54�� ͼ��ר�� ŷ��ͼ\Problem C ���_std.cpp:33:19: error: 'b' was not declared in this scope f
最新发布
03-28
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值