c++ - 我正在尝试使用简单的堆栈推送和弹出来反转字符串。但是我收到一些我无法理解的错误
问题描述
我的代码:
#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++ 运行时库时遇到问题。可能编译器出于某种原因试图在 C 模式下编译。我假设您使用 Linux 作为您的操作系统。
试试命令g++ program.cpp -o program
。如果它没有编译,请告诉我错误是什么。
推荐阅读
- elm - 将多个复杂的父操作传递给深度嵌套的子视图
- ubuntu - 使用问题:wget [OPTION] ... [URL]
- jquery - 数据表警告:
- java - 是否可以在 Junit5 中禁用参数化测试基础参数
- css - Prestashop 编辑标题 css
- android - 在 Android Q 上使用 Glide 以“content://”方案显示来自 URI 的图像
- logstash - 使用 if-else 和 Logstash 拆分
- python - 比较 Salesforce 和 Pardot 中的数据
- powershell - Powershell - 使用 dir / GetChildItem 列出修改日期的目录
- highcharts - Highcharts 甘特图(或 XRange)组 y 轴元素