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

VS Studio运行LeetCode两数之和C++代码出现编译错误求助

问题

我在VS Studio IDE里运行LeetCode上两数之和的C++暴力解法代码时,碰到了一连串编译错误:

  1. 一开始报vector does not name a type,加了#include <vector>后,又出现两个错误:
    • using-declaration for non-member at class scope
    • expected unqualified-id before '<' token
  2. 但同样的代码在LeetCode平台上不用加#include <vector>就能正常跑,想知道这是为啥,以及怎么解决。

我在VS里用的代码:

#include <iostream>
#include <vector>

using std::vector;

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size(); i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target){
                    return {i, j};
                }
            }            
        }
        return {};
    }
};

LeetCode上的原始代码:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size(); i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target){
                    return {i, j};
                }
            }            
        }
        return {};
    }
};
原因分析

1. LeetCode不用手动加头文件的原因

LeetCode的编译环境会自动给用户的代码套一层通用前置代码,里面已经包含了<vector>、<iostream>这类常用头文件,还加了using namespace std;之类的命名空间声明,所以你提交的代码不用重复写这些就能识别vector。

2. VS里报错的具体原因

  • 最初的vector does not name a type:VS的编译环境不会自动帮你引入头文件,必须手动写#include <vector>才能用std::vector类型。
  • 加头文件后的两个错误:
    • 如果不小心把using std::vector;写到了class Solution内部,就会触发using-declaration for non-member at class scope——类里面的using声明只能用来继承基类的成员,不能直接声明全局命名空间里的非成员类型。
    • expected unqualified-id before '<' token:大概率是VS默认用了C98标准,而代码里用了C11才支持的列表初始化返回值(return {i,j};、return {};),老版本编译器识别不了这种语法。
解决方法

方法1:修正代码结构与VS标准设置

  1. 确保using std::vector;写在全局范围(也就是class Solution定义之前),别放在类里面。
  2. 把VS的C标准改成C11或更高版本:
    右键项目 → 属性 → 配置属性 → C/C++ → 语言 → C标准 → 选ISO C++11 标准或者更高的(比如C17)。
  3. 如果要运行代码,得加个主函数测试,示例代码如下:
#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::endl;

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size(); i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target){
                    return {i, j};
                }
            }            
        }
        return {};
    }
};

int main() {
    Solution sol;
    vector<int> nums = {2, 7, 11, 15};
    int target = 9;
    vector<int> result = sol.twoSum(nums, target);
    cout << "[" << result[0] << ", " << result[1] << "]" << endl;
    return 0;
}

方法2:不用using声明,直接写全称

要是不想用using std::vector;,可以在代码里直接写std::vector,这样更规范,还能避免命名空间冲突:

#include <iostream>
#include <vector>

class Solution {
public:
    std::vector<int> twoSum(std::vector<int>& nums, int target) {
        for(int i = 0; i < nums.size(); i++){
            for(int j = i + 1; j < nums.size(); j++){
                if(nums[i] + nums[j] == target){
                    return {i, j};
                }
            }            
        }
        return {};
    }
};

// 主函数和上面一样...

内容的提问来源于stack exchange,提问作者The Sideshow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 14:20:59