多个编译器上的 c++ 代码中出现未知错误

Unknown error in c++ code on multiple compilers

提问人:Shreshth Sharma 提问时间:9/12/2023 更新时间:9/12/2023 访问量:127

问:

我尝试了许多不同版本的 c++,但由于某种原因编译器不接受我的代码,我也没有看到明显的语法错误,请帮忙。还请告诉我哪个版本的 c++ 支持 <bits.stdc ++.h> 头文件。

#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
 
 
bool comp(pair<int ,int> a, pair<int ,int> b)
{
    return (a .first<b .first);
}


int main()
{
    int n;
    (input n)  //for some reason stack overflow wont let me input
    vector <int ,int> laptops;
    for(int z=0; z<n; z++)
    {
        int a ,b;
        cin>> a>> b;
        pair <int ,int> p;
        p= make _pair(a ,b);
        laptops . push _back (p);
    } 
    sort( laptops .begin(), laptops .end(),comp());
    bool flag=false;
    for (auto& it : arr)
    {
        if(a .second>b.second)
        flag=true;
    }
    if(flag==true)
    cout<<"Happy Alex";
    else
    cout<< "Poor Alex" ;
    
    return 0;
}

这是我在 programiz c++ 编译器上遇到的错误。

ERROR!
g++ /tmp/v6DZdO2ElD.cpp
In file included from /usr/local/include/c++/12.2.0/vector:64,
                 from /tmp/v6DZdO2ElD.cpp:3:
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'struct std::_Vector_base<int, int>':
/usr/local/include/c++/12.2.0/bits/stl_vector.h:423:11:   required from 'class std::vector<int, int>'
/tmp/v6DZdO2ElD.cpp:18:21:   required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:88:28: error: 'int' is not a class, struct, or union type
   88 |         rebind<_Tp>::other _Tp_alloc_type;
      |                            ^~~~~~~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:90:9: error: 'int' is not a class, struct, or union type
   90 |         pointer;
      |         ^~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'class std::vector<int, int>':
/tmp/v6DZdO2ElD.cpp:18:21:   required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:513:20: error: '_M_allocate' has not been declared in 'std::vector<int, int>::_Base'
  513 |       using _Base::_M_allocate;
      |                    ^~~~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:514:20: error: '_M_deallocate' has not been declared in 'std::vector<int, int>::_Base'
  514 |       using _Base::_M_deallocate;
      |                    ^~~~~~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:516:20: error: '_M_get_Tp_allocator' has not been declared in 'std::vector<int, int>::_Base'
  516 |       using _Base::_M_get_Tp_allocator;
      |                    ^~~~~~~~~~~~~~~~~~~
In file included from /usr/local/include/c++/12.2.0/bits/stl_algobase.h:59,
                 from /usr/local/include/c++/12.2.0/algorithm:60,
                 from /tmp/v6DZdO2ElD.cpp:1:
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = int; _Alloc = int]':
/tmp/v6DZdO2ElD.cpp:18:21:   required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:137:24: error: 'int' is not a class, struct, or union type
  137 |         _Vector_impl() _GLIBCXX_NOEXCEPT_IF(
      |                        ^~~~~~~~~~~~~~~~~~~~
/tmp/v6DZdO2ElD.cpp: In function 'int main()':
/tmp/v6DZdO2ElD.cpp:25:26: error: no matching function for call to 'std::vector<int, int>::push_back(std::pair<int, int>&)'
   25 |         laptops.push_back(p);
      |         ~~~~~~~~~~~~~~~~~^~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1276:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = int; value_type = int]'
 1276 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1276:35: note:   no known conversion for argument 1 from 'std::pair<int, int>' to 'const std::vector<int, int>::value_type&' {aka 'const int&'}
 1276 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1293:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = int; _Alloc = int; value_type = int]'
 1293 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/usr/local/include/c++/12.2.0/bits/stl_vector.h:1293:30: note:   no known conversion for argument 1 from 'std::pair<int, int>' to 'std::vector<int, int>::value_type&&' {aka 'int&&'}
 1293 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
/tmp/v6DZdO2ElD.cpp:27:18: error: 'class std::vector<int, int>' has no member named 'begin'
   27 |     sort(laptops.begin(),laptops.end(),comp());
      |                  ^~~~~
/tmp/v6DZdO2ElD.cpp:27:34: error: 'class std::vector<int, int>' has no member named 'end'
   27 |     sort(laptops.begin(),laptops.end(),comp());
      |                                  ^~~
/tmp/v6DZdO2ElD.cpp:27:44: error: too few arguments to function 'bool comp(std::pair<int, int>, std::pair<int, int>)'
   27 |     sort(laptops.begin(),laptops.end(),comp());
      |                                        ~~~~^~
/tmp/v6DZdO2ElD.cpp:8:6: note: declared here
    8 | bool comp(pair<int,int> a,pair<int,int> b)
      |      ^~~~
/tmp/v6DZdO2ElD.cpp:29:21: error: 'arr' was not declared in this scope
   29 |     for (auto& it : arr)
      |                     ^~~
/tmp/v6DZdO2ElD.cpp:31:12: error: 'a' was not declared in this scope
   31 |         if(a.second>b.second)
      |            ^
/tmp/v6DZdO2ElD.cpp:31:21: error: 'b' was not declared in this scope
   31 |         if(a.second>b.second)
      |                     ^
/usr/local/include/c++/12.2.0/bits/stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = int; _Alloc = int]':
/usr/local/include/c++/12.2.0/bits/stl_vector.h:312:7:   required from here
/usr/local/include/c++/12.2.0/bits/stl_vector.h:139:26: error: 'int' is not a class, struct, or union type
  139 |         : _Tp_alloc_type()
      |                          ^
C++ 错误处理 语法错误

评论

4赞 kiner_shah 9/12/2023
不要使用头文件。使用单个标头。<bits/stdc++.h>
0赞 kiner_shah 9/12/2023
你的代码中有很多问题,到处都是不必要的空格,未声明的变量(或错误的变量),不正确的声明等。
0赞 Pepijn Kramer 9/12/2023
旁注:如果您不编写泛型/模板库代码,也不要使用。只需使用两个具有可读名称的成员变量创建自己的结构即可。这样做的优点是可以创建一个不会与任何通用 std::p air 混淆的强类型。这也将允许你创建一个实际上只适用于你自己的数据类型(结构)的比较函数std::pair
2赞 PaulMcKenzie 9/12/2023
还请告诉我哪个版本的 c++ 支持 <bits.stdc ++.h> 头文件 -- 不要使用此头文件

答:

2赞 Alan Birtles 9/12/2023 #1

代码中存在许多错误:

  1. 我不确定应该是什么?(input n)
  2. 第二个模板参数是它的分配器类型,我假设你的意思是std::vectorstd::vector<std::pair<int, int>>
  3. 不能在方法名称中放置空格,并且make _pairpush _back
  4. arr大概应该是laptops
  5. 将比较器传递给 时,不要调用它,只需传递函数名称 (std::sortstd::sort(laptops.begin(), laptops.end(), comp))
  6. 我不确定应该是什么,既不存在,也不存在。if(a .second>b.second)ab

<bits/stdc++.h> 不是 C++ 的一部分,并且只是 libstdc++ 的标头,因此应避免使用。

2赞 Pepijn Kramer 9/12/2023 #2

这就是我所说的在不使用通用 std::p air 的情况下编写可读代码的意思。

#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>

// using namespace std; <== no don't do this.

// Say what you mean, and mean what you say
// you want to compare some laptop data, NOT a pair
struct laptop_data_t
{
    int laptop_id;
    int user_id;
};

// give functions as readable names as you can
bool sort_by_laptop_id(const laptop_data_t& lhs, const laptop_data_t& rhs)
{
    return (lhs.laptop_id) < (rhs.laptop_id);
}

// put user input in a function
// this also allows for a function that will fill in data automatically for testing later
auto get_laptops_from_user_input()
{
    std::size_t n; // datastructure sizes are not `int`
    std::cout << "How many laptops? : ";
    std::cin >> n;
    std::vector<laptop_data_t> laptops(n); // <== allocate data for n laptos

    // use a range based for loop, i.o. index based loop
    // whenever you can
    for(auto& laptop : laptops)
    {
        std::cout << "laptop id : ";
        std::cin >> laptop.laptop_id;
        std::cout << "user id : ";
        std::cin >> laptop.user_id;
    }
    
    return laptops;
}

// function for generating test data
auto get_laptops_test_data()
{
    std::vector<laptop_data_t> laptops{{4,12},{3,11},{5,13},{1,1}};
    return laptops;
}

int main()
{
    auto laptops = get_laptops_test_data();

    //(input n)  //for some reason stack overflow wont let me input
    //vector <int ,int> laptops; <== NO you cannot do this.
    
    std::sort( laptops.begin(), laptops.end(), sort_by_laptop_id);
    
    for(const auto& laptop : laptops)
    {
        std::cout << "latop (id,user_id) = " << laptop.laptop_id <<", " << laptop.user_id << "\n";
    }

    return 0;
}