吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 898|回复: 2
收起左侧

[求助] collect2: error: ld returned 1 exit status 是什么错误?

[复制链接]
djdgf4 发表于 2023-2-21 14:40
课后作业,用栈实现括号匹配检查,代码如下:
[C++] 纯文本查看 复制代码
// C++ Program to check for balanced parentheses in an expression using stack.
// 2023-02-20_190142.png
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;

#define parentheses_left '('
#define parentheses_right ')'
#define brackets_left '['
#define brackets_right ']'
#define braces_left '{'
#define braces_right '}'

int Check_Parentheses(char *Brackets_list)
{
    stack<char> S;
    int i = 0;
    if (Brackets_list[i] == '{' || Brackets_list[i] == '[' || Brackets_list[i] == '(')
    {
        S.push(Brackets_list[i]);
    }
    else if (Brackets_list[i] == '}' || Brackets_list[i] == ']' || Brackets_list[i] == ')')
    {
        if (S.empty())
        {
            cout << "False!" << endl;
            return 0;
        }
        if (S.top() == parentheses_left && Brackets_list[i] == parentheses_right)
            S.pop();
        else if (S.top() == brackets_left && Brackets_list[i] == brackets_right)
            S.pop();
        else if (S.top() == braces_left && Brackets_list[i] == braces_right)
            S.pop();
        else
        {
            cout << "The Brackets isn't match" << endl;
            return 0;
        }
    }
    return S.empty() ? 1 : 0;
}

int main()
{
    char test1[] = {"{{[()]}}"};
    char test2[] = {"{[[}"};
    char test3[] = {']','['};
    char test4[] = {"{}}"};

    Check_Parentheses(test1);
    Check_Parentheses(test2);
    Check_Parentheses(test3);
    Check_Parentheses(test4);

    return 0;
}


然后就报了错误,具体如下:

正在执行任务: C/C++: gcc 生成活动文件

正在启动生成...
/usr/bin/gcc -fdiagnostics-color=always -g /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp -o /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses
/usr/bin/ld: /tmp/ccQjdfFd.o: warning: relocation against _ZSt4cout' in read-only section.text'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function Check_Parentheses(char*)': /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference tostd::cout'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference tostd::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference tostd::cout'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference tostd::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /tmp/ccQjdfFd.o: in functionstatic_initialization_and_destruction_0(int, int)':
/usr/include/c++/11/iostream:74: undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: /usr/include/c++/11/iostream:74: undefined reference tostd::ios_base::Init::~Init()'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_Deque_impl::~_Deque_impl()': /usr/include/c++/11/bits/stl_deque.h:520: undefined reference tostd::allocator<char>::~allocator()'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_Deque_impl::_Deque_impl()': /usr/include/c++/11/bits/stl_deque.h:525: undefined reference tostd::allocator<char>::allocator()'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_M_initialize_map(unsigned long)': /usr/include/c++/11/bits/stl_deque.h:637: undefined reference to
cxa_begin_catch'
/usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:642: undefined reference to __cxa_rethrow' /usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:637: undefined reference tocxa_end_catch'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function void std::deque<char, std::allocator<char> >::_M_push_back_aux<char const&>(char const&)': /usr/include/c++/11/bits/deque.tcc:493: undefined reference tostd::
throw_length_error(char const)'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_M_create_nodes(char**, char**)': /usr/include/c++/11/bits/stl_deque.h:664: undefined reference to__cxa_begin_catch'
/usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:667: undefined reference to __cxa_rethrow' /usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:664: undefined reference tocxa_end_catch'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function `
gnu_cxx::new_allocator<char>::deallocate(char
, unsigned long)':
/usr/include/c++/11/ext/new_allocator.h:145: undefined reference to operator delete(void*, unsigned long)' /usr/bin/ld: /tmp/ccQjdfFd.o: in functiongnu_cxx::new_allocator<char*>::deallocate(char*, unsigned long)':
/usr/include/c++/11/ext/new_allocator.h:145: undefined reference to `operator delete(void
, unsigned long)'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function __gnu_cxx::new_allocator<char*>::allocate(unsigned long, void const*)': /usr/include/c++/11/ext/new_allocator.h:116: undefined reference tostd::
throw_bad_array_new_length()'
/usr/bin/ld: /usr/include/c++/11/ext/new_allocator.h:117: undefined reference to std::__throw_bad_alloc()' /usr/bin/ld: /usr/include/c++/11/ext/new_allocator.h:127: undefined reference tooperator new(unsigned long)'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function __gnu_cxx::new_allocator<char>::allocate(unsigned long, void const*)': /usr/include/c++/11/ext/new_allocator.h:117: undefined reference tostd::__throw_bad_alloc()'
/usr/bin/ld: /usr/include/c++/11/ext/new_allocator.h:127: undefined reference to operator new(unsigned long)' /usr/bin/ld: /tmp/ccQjdfFd.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to__gxx_personality_v0'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

生成已完成,但出现错误。

  • 终端进程已终止,退出代码: -1。
  • 终端将被任务重用,按任意键关闭。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Jacen 发表于 2023-2-21 15:01
这段代码本身没有语法错误,但是存在逻辑错误。具体如下:

在 Check_Parentheses 函数中,只判断了第一个字符,而没有对后面的字符进行遍历。因此,只有对于第一个字符是左括号的情况下才会将其入栈,但是如果第一个字符是右括号,则会直接判定为不匹配。

在判断括号是否匹配时,只是简单地判断了左右括号是否匹配,但是没有考虑括号嵌套的情况。例如,当遇到连续的 {{}} 时,只会将最外层的括号进行匹配,但是实际上内层的括号也需要进行匹配。

因此,需要对代码进行修改,完善判断括号是否匹配的逻辑。
hoo1941 发表于 2023-2-21 16:48
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 01:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表