我正在尝试使用简单的堆栈推送和弹出来反转字符串。但是,我收到一些错误,我无法理解

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

我的代码:

#include<iostream>
#include<stack>
using namespace std;


void Reverse_string(char *C, int n)
{
    stack<char> S;
    for(int i =0; i < n ; i++)
    {
        S.push(C[i]);
    }

    for(int i = 0; i< n; i++)
    {
        C[i] = S.top();  //overwite the char at index i
        S.pop();
    }

}
int main()
{
    char C[51]= "Hello WOrld";

    Reverse_string(C,11);
    printf("Output = %s", C);
} 


错误:

/tmp/ccC6pV6B.o: In function `__static_initialization_and_destruction_0(int, int)':
reverse_stack.cpp:(.text+0x1bb): undefined reference to `std::ios_base::Init::Init()'
reverse_stack.cpp:(.text+0x1d0): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccC6pV6B.o: In function `std::_Deque_base<char, std::allocator<char> >::_Deque_impl::~_Deque_impl()':
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE11_Deque_implD2Ev[_ZNSt11_Deque_baseIcSaIcEE11_Deque_implD5Ev]+0x14): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccC6pV6B.o: In function `std::_Deque_base<char, std::allocator<char> >::_Deque_impl::_Deque_impl()':
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE11_Deque_implC2Ev[_ZNSt11_Deque_baseIcSaIcEE11_Deque_implC5Ev]+0x14): undefined reference to `std::allocator<char>::allocator()'
/tmp/ccC6pV6B.o: In function `std::_Deque_base<char, std::allocator<char> >::_M_initialize_map(unsigned long)':
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE17_M_initialize_mapEm[_ZNSt11_Deque_baseIcSaIcEE17_M_initialize_mapEm]+0x16b): undefined reference to `__cxa_begin_catch'
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE17_M_initialize_mapEm[_ZNSt11_Deque_baseIcSaIcEE17_M_initialize_mapEm]+0x1a5): undefined reference to `__cxa_rethrow'
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE17_M_initialize_mapEm[_ZNSt11_Deque_baseIcSaIcEE17_M_initialize_mapEm]+0x1ad): undefined reference to `__cxa_end_catch'
/tmp/ccC6pV6B.o: In function `__gnu_cxx::new_allocator<char>::deallocate(char*, unsigned long)':
reverse_stack.cpp:(.text._ZN9__gnu_cxx13new_allocatorIcE10deallocateEPcm[_ZN9__gnu_cxx13new_allocatorIcE10deallocateEPcm]+0x1c): undefined reference to `operator delete(void*)'
/tmp/ccC6pV6B.o: In function `std::_Deque_base<char, std::allocator<char> >::_M_create_nodes(char**, char**)':
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE15_M_create_nodesEPPcS3_[_ZNSt11_Deque_baseIcSaIcEE15_M_create_nodesEPPcS3_]+0x48): undefined reference to `__cxa_begin_catch'
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE15_M_create_nodesEPPcS3_[_ZNSt11_Deque_baseIcSaIcEE15_M_create_nodesEPPcS3_]+0x64): undefined reference to `__cxa_rethrow'
reverse_stack.cpp:(.text._ZNSt11_Deque_baseIcSaIcEE15_M_create_nodesEPPcS3_[_ZNSt11_Deque_baseIcSaIcEE15_M_create_nodesEPPcS3_]+0x6c): undefined reference to `__cxa_end_catch'
/tmp/ccC6pV6B.o: In function `__gnu_cxx::new_allocator<char*>::deallocate(char**, unsigned long)':
reverse_stack.cpp:(.text._ZN9__gnu_cxx13new_allocatorIPcE10deallocateEPS1_m[_ZN9__gnu_cxx13new_allocatorIPcE10deallocateEPS1_m]+0x1c): undefined reference to `operator delete(void*)'
/tmp/ccC6pV6B.o: In function `__gnu_cxx::new_allocator<char>::allocate(unsigned long, void const*)':
reverse_stack.cpp:(.text._ZN9__gnu_cxx13new_allocatorIcE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIcE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
reverse_stack.cpp:(.text._ZN9__gnu_cxx13new_allocatorIcE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIcE8allocateEmPKv]+0x38): undefined reference to `operator new(unsigned long)'
/tmp/ccC6pV6B.o: In function `__gnu_cxx::new_allocator<char*>::allocate(unsigned long, void const*)':
reverse_stack.cpp:(.text._ZN9__gnu_cxx13new_allocatorIPcE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIPcE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
reverse_stack.cpp:(.text._ZN9__gnu_cxx13new_allocatorIPcE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIPcE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
/tmp/ccC6pV6B.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
c++ stack
1个回答
0
投票

您似乎在链接到C ++运行时库时遇到问题。出于某种原因,编译器可能正在尝试以C模式进行编译。我假设您使用Linux作为操作系统。

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