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

问题描述 投票:0回答:1

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

#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++ error-handling syntax-error
1个回答
0
投票

您的代码中有很多错误:

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

<bits/stdc++.h>
不是 C++ 的一部分,只是 libstdc++ 的标头,所以应该避免。

© www.soinside.com 2019 - 2024. All rights reserved.