2013-02-12 35 views
1

任何意義的代碼如下:在C++中使用矢量給我一個錯誤,我不能做的

#include <stdio.h> 
#include <math.h> 
#include <vector> 


float comp_cur(float A_x, float A_y, float B_x, float B_y, float C_x, float C_y); 
void comp_arr(std::vector<float> vals, std::vector<float> out, int scale, float dim); 

main(){ 
    std::vector <float> curves; 
    std::vector <float> inputs; 

    inputs.push_back(2.0); 
    inputs.push_back(3.0); 
    inputs.push_back(6.0); 
    inputs.push_back(1.0); 
    inputs.push_back(7.0); 

    comp_arr(inputs,curves,1.0,1.0); 
} 

float comp_cur(float A_x, float A_y, float B_x, float B_y, float C_x, float C_y){ 

    B_x = B_x-A_x; 
    B_y = B_y-A_y; 
    C_x = C_x-A_x; 
    C_y = C_y-A_y; 
    A_x = 0; 
    A_y = 0; 

    float numerator_A = C_y*B_x - B_y*C_x; 
    float denominator_A = C_x*C_x*B_x - C_x*B_x*B_x; 
    float A = numerator_A/denominator_A; 

    float B = (B_y - A*B_x*B_x)/B_x; 

    float curviture = 2*A/powf((1 + (2*A*B_x + B)*(2*A*B_x + B)), 1.5); 
    return (curviture); 
} 

void comp_arr(std::vector<float> vals, std::vector<float> out, int scale, float dim){ 
    for(int i = scale; i < sizeof(vals)-scale+1; i++){ 
     float cur = comp_cur((i-scale)*dim, vals[i-1], i*dim, vals[i], (i+scale)*dim, vals[i+1]); 
     out.push_back(cur); 
    } 
} 

約comp_cur一切工作正常,但comp_arr函數拋出一個可怕的錯誤。我對C和C++相當陌生,所以我根本無法理解這個錯誤。

/tmp/ccTCZ2bX.o: In function `main': 
comp.cpp:(.text+0x174): undefined reference to `__cxa_end_cleanup' 
/tmp/ccTCZ2bX.o:(.ARM.extab+0x0): undefined reference to `__gxx_personality_v0' 
/tmp/ccTCZ2bX.o: In function `std::vector<float, std::allocator<float> >::~vector()': 
comp.cpp:(.text._ZNSt6vectorIfSaIfEED2Ev[_ZNSt6vectorIfSaIfEED5Ev]+0x6c): undefined reference to `__cxa_end_cleanup' 
/tmp/ccTCZ2bX.o:(.ARM.extab.text._ZNSt6vectorIfSaIfEED2Ev[_ZNSt6vectorIfSaIfEED5Ev]+0x0): undefined reference to `__gxx_personality_v0' 
/tmp/ccTCZ2bX.o: In function `std::vector<float, std::allocator<float> >::vector(std::vector<float, std::allocator<float> > const&)': 
comp.cpp:(.text._ZNSt6vectorIfSaIfEEC2ERKS1_[_ZNSt6vectorIfSaIfEEC5ERKS1_]+0xb4): undefined reference to `__cxa_end_cleanup' 
/tmp/ccTCZ2bX.o:(.ARM.extab.text._ZNSt6vectorIfSaIfEEC2ERKS1_[_ZNSt6vectorIfSaIfEEC5ERKS1_]+0x0): undefined reference to `__gxx_personality_v0' 
/tmp/ccTCZ2bX.o: In function `std::_Vector_base<float, std::allocator<float> >::~_Vector_base()': 
comp.cpp:(.text._ZNSt12_Vector_baseIfSaIfEED2Ev[_ZNSt12_Vector_baseIfSaIfEED5Ev]+0x70): undefined reference to `__cxa_end_cleanup' 
/tmp/ccTCZ2bX.o:(.ARM.extab.text._ZNSt12_Vector_baseIfSaIfEED2Ev[_ZNSt12_Vector_baseIfSaIfEED5Ev]+0x0): undefined reference to `__gxx_personality_v0' 
/tmp/ccTCZ2bX.o: In function `std::vector<float, std::allocator<float> >::_M_insert_aux(__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > >, float const&)': 
comp.cpp:(.text._ZNSt6vectorIfSaIfEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPfS1_EERKf[std::vector<float, std::allocator<float> >::_M_insert_aux(__gnu_cxx::__normal_iterator< float*, std::vector<float, std::allocator<float> > >, float const&)]+0x290): undefined reference to `__cxa_end_catch' 
comp.cpp:(.text._ZNSt6vectorIfSaIfEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPfS1_EERKf[std::vector<float, std::allocator<float> >::_M_insert_aux(__gnu_cxx::__normal_iterator< float*, std::vector<float, std::allocator<float> > >, float const&)]+0x294): undefined reference to `__cxa_end_cleanup' 
comp.cpp:(.text._ZNSt6vectorIfSaIfEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPfS1_EERKf[std::vector<float, std::allocator<float> >::_M_insert_aux(__gnu_cxx::__normal_iterator< float*, std::vector<float, std::allocator<float> > >, float const&)]+0x2a0): undefined reference to `__cxa_begin_catch' 
comp.cpp:(.text._ZNSt6vectorIfSaIfEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPfS1_EERKf[std::vector<float, std::allocator<float> >::_M_insert_aux(__gnu_cxx::__normal_iterator< float*, std::vector<float, std::allocator<float> > >, float const&)]+0x308): undefined reference to `__cxa_rethrow' 
/tmp/ccTCZ2bX.o:(.ARM.extab.text._ZNSt6vectorIfSaIfEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPfS1_EERKf[std::vector<float, std::allocator<float>  >::_M_insert_aux(__gnu_cxx::__normal_iterator<float*, std::vector<float, std::allocator<float> > >, float const&)]+0x0): undefined reference to `__gxx_personality_v0' 
/tmp/ccTCZ2bX.o: In function `std::_Vector_base<float, std::allocator<float> >::_Vector_base(unsigned int, std::allocator<float> const&)': 
comp.cpp:(.text._ZNSt12_Vector_baseIfSaIfEEC2EjRKS0_[_ZNSt12_Vector_baseIfSaIfEEC5EjRKS0_]+0x90): undefined reference to `__cxa_end_cleanup' 
/tmp/ccTCZ2bX.o:(.ARM.extab.text._ZNSt12_Vector_baseIfSaIfEEC2EjRKS0_[_ZNSt12_Vector_baseIfSaIfEEC5EjRKS0_]+0x0): undefined reference to `__gxx_personality_v0' 
/tmp/ccTCZ2bX.o: In function `std::vector<float, std::allocator<float> >::_M_check_len(unsigned int, char const*) const': 
comp.cpp:(.text._ZNKSt6vectorIfSaIfEE12_M_check_lenEjPKc[std::vector<float, std::allocator<float> >::_M_check_len(unsigned int, char const*) const]+0x54): undefined reference to  `std::__throw_length_error(char const*)' 
/tmp/ccTCZ2bX.o: In function `__gnu_cxx::new_allocator<float>::deallocate(float*, unsigned int)': 
comp.cpp:(.text._ZN9__gnu_cxx13new_allocatorIfE10deallocateEPfj[__gnu_cxx::new_allocator<float>::deallocate(float*, unsigned int)]+0x1c): undefined reference to `operator delete(void*)' 
/tmp/ccTCZ2bX.o: In function `__gnu_cxx::new_allocator<float>::allocate(unsigned int, void const*)': 
comp.cpp:(.text._ZN9__gnu_cxx13new_allocatorIfE8allocateEjPKv[__gnu_cxx::new_allocator<float>::allocate(unsigned int, void const*)]+0x40): undefined reference to  `std::__throw_bad_alloc()' 
comp.cpp:(.text._ZN9__gnu_cxx13new_allocatorIfE8allocateEjPKv[__gnu_cxx::new_allocator<float>::allocate(unsigned int, void const*)]+0x50): undefined reference to `operator new(unsigned  int)' 
collect2: ld returned 1 exit status 
+4

看起來您的鏈接步驟未與C++標準庫鏈接。 – 2013-02-12 19:56:11

+0

看來你正在使用傳值。傳遞每個引用或使用指針。 [Wiki](http://en.wikipedia.org/wiki/Reference_(C%2B%2B)) – 2013-02-12 19:57:40

+1

請給出完整的命令行。並且,是的,通過引用(參數列表中的'std :: vector const&vals'等將'vector's傳遞給'comp_arr') – Walter 2013-02-12 19:58:02

回答

3

我會在不知道如何編譯的情況下進行有根據的猜測。

請確保您使用g++而不是gcc編譯 - g++會自動設置一些用於編譯C++的選項,包括鏈接到標準庫。或者,給出-lstdc++-x c++選項。