djdgf4 发表于 2023-2-21 14:40

collect2: error: ld returned 1 exit status 是什么错误?

课后作业,用栈实现括号匹配检查,代码如下:
// 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 == '{' || Brackets_list == '[' || Brackets_list == '(')
    {
      S.push(Brackets_list);
    }
    else if (Brackets_list == '}' || Brackets_list == ']' || Brackets_list == ')')
    {
      if (S.empty())
      {
            cout << "False!" << endl;
            return 0;
      }
      if (S.top() == parentheses_left && Brackets_list == parentheses_right)
            S.pop();
      else if (S.top() == brackets_left && Brackets_list == brackets_right)
            S.pop();
      else if (S.top() == braces_left && Brackets_list == 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 to `std::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 to `std::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 to `std::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 to `std::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 function `__static_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 to `std::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 to `std::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 to `std::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 to `__cxa_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 to `std::__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 to `__cxa_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 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 function `__gnu_cxx::new_allocator<char*>::allocate(unsigned long, void const*)':
/usr/include/c++/11/ext/new_allocator.h:116: undefined reference to `std::__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 to `operator 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 to `std::__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+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

编译c++程序请使用g++或clang++,你用的是gcc。
页: [1]
查看完整版本: collect2: error: ld returned 1 exit status 是什么错误?