c++ - 指针数组的 vector 方法 push_back 编译但不运行

标签 c++ arrays pointers c++11 vector

所以我有一个指向 float 的指针数组 vector 。我试图推回 float* 类型的空指针数组。当我这样做时,我得到一个错误,我将在最后发布。这是代码:

#include <iostream>
#include <math.h>
#include <vector>
#include <tr1/array>

using namespace std;

typedef vector< tr1::array<float* , 2> > polygon;

void triangle_solver(float*,float*,float*,float*,float*,float*);//Prototype function for solving triangles, 6 floats for 3 sides and 3 angles
void polygon_solver(polygon poly);
void displayPoly(polygon);
int main()
{
    polygon test;
    cout << test.size() << endl;
    //polygon_solver(test);
    return 0;
}

void displayPoly(polygon poly)
{
        for (polygon::iterator it = poly.begin(); it != poly.end(); it++)
        {
            for (int c=0; c<2; c++)
            {
                if ((*it)[c]) cout << *(*it)[c] << endl;
                else cout << '?' << endl;
            }
        }
}

void polygon_solver(polygon poly)
{
    if (poly.size() > 3)
    {

        float *a1 = NULL;
        float *a3 = NULL;
        float *s2 = NULL;
        polygon sub(poly.begin()+2, poly.end()-1);
        float *A1 = NULL;
        float *A3 = NULL;
        float *S2 = NULL;
        sub.push_back({A1, S2});                //ERROR HERE
        float *aA3 = poly[2][1];
        float *aA1 = poly[0][1];
        cout << "Sub Polygon: " << endl;
        displayPoly(sub); cout << endl;
        for (int c=0; c<1; c++)
        {
            triangle_solver(a1, poly[1][1], a3, poly[1][0], s2, poly[0][1]);
            polygon_solver(sub);
        }
        cout << "Polygon: " << endl;
        displayPoly(poly); cout << endl;
    }
}

我使用 NULL 来表示未知数(这是标准吗?)请注意,它在代码块中编译但不运行,也不会通过在命令行运行 gcc 来编译。这是错误:

main.cpp: In function ‘void polygon_solver(polygon)’:
main.cpp:45:23: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
         sub.push_back({A1, S2});
                       ^
main.cpp:45:31: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
         sub.push_back({A1, S2});
                               ^
/tmp/ccZsz9el.o: In function `main':
main.cpp:(.text+0x25): undefined reference to `std::cout'
main.cpp:(.text+0x2a): undefined reference to `std::ostream::operator<<(unsigned long)'
main.cpp:(.text+0x2f): 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> >&)'
main.cpp:(.text+0x37): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccZsz9el.o: In function `displayPoly(std::vector<std::tr1::array<float*, 2ul>, std::allocator<std::tr1::array<float*, 2ul> > >)':
main.cpp:(.text+0xf4): undefined reference to `std::cout'
main.cpp:(.text+0xf9): undefined reference to `std::ostream::operator<<(float)'
main.cpp:(.text+0xfe): 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> >&)'
main.cpp:(.text+0x106): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.cpp:(.text+0x112): undefined reference to `std::cout'
main.cpp:(.text+0x117): 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)'
main.cpp:(.text+0x11c): 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> >&)'
main.cpp:(.text+0x124): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccZsz9el.o: In function `polygon_solver(std::vector<std::tr1::array<float*, 2ul>, std::allocator<std::tr1::array<float*, 2ul> > >)':
main.cpp:(.text+0x2f5): undefined reference to `std::cout'
main.cpp:(.text+0x2fa): 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*)'
main.cpp:(.text+0x2ff): 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> >&)'
main.cpp:(.text+0x307): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.cpp:(.text+0x343): 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> >&)'
main.cpp:(.text+0x348): undefined reference to `std::cout'
main.cpp:(.text+0x34d): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.cpp:(.text+0x431): undefined reference to `std::cout'
main.cpp:(.text+0x436): 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*)'
main.cpp:(.text+0x43b): 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> >&)'
main.cpp:(.text+0x443): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
main.cpp:(.text+0x476): 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> >&)'
main.cpp:(.text+0x47b): undefined reference to `std::cout'
main.cpp:(.text+0x480): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/ccZsz9el.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x555): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x564): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccZsz9el.o: In function `std::vector<std::tr1::array<float*, 2ul>, std::allocator<std::tr1::array<float*, 2ul> > >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::tr1::array<float*, 2ul>*, std::vector<std::tr1::array<float*, 2ul>, std::allocator<std::tr1::array<float*, 2ul> > > >, std::tr1::array<float*, 2ul> const&)':
main.cpp:(.text._ZNSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS3_S5_EERKS3_[_ZNSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS3_S5_EERKS3_]+0x271): undefined reference to `__cxa_end_catch'
main.cpp:(.text._ZNSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS3_S5_EERKS3_[_ZNSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS3_S5_EERKS3_]+0x284): undefined reference to `__cxa_begin_catch'
main.cpp:(.text._ZNSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS3_S5_EERKS3_[_ZNSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS3_S5_EERKS3_]+0x2ec): undefined reference to `__cxa_rethrow'
/tmp/ccZsz9el.o: In function `std::vector<std::tr1::array<float*, 2ul>, std::allocator<std::tr1::array<float*, 2ul> > >::_M_check_len(unsigned long, char const*) const':
main.cpp:(.text._ZNKSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE12_M_check_lenEmPKc[_ZNKSt6vectorINSt3tr15arrayIPfLm2EEESaIS3_EE12_M_check_lenEmPKc]+0x4c): undefined reference to `std::__throw_length_error(char const*)'
/tmp/ccZsz9el.o: In function `__gnu_cxx::new_allocator<std::tr1::array<float*, 2ul> >::deallocate(std::tr1::array<float*, 2ul>*, unsigned long)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorINSt3tr15arrayIPfLm2EEEE10deallocateEPS4_m[_ZN9__gnu_cxx13new_allocatorINSt3tr15arrayIPfLm2EEEE10deallocateEPS4_m]+0x1c): undefined reference to `operator delete(void*)'
/tmp/ccZsz9el.o: In function `__gnu_cxx::new_allocator<std::tr1::array<float*, 2ul> >::allocate(unsigned long, void const*)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorINSt3tr15arrayIPfLm2EEEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorINSt3tr15arrayIPfLm2EEEE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()'
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorINSt3tr15arrayIPfLm2EEEE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorINSt3tr15arrayIPfLm2EEEE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)'
/tmp/ccZsz9el.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status

最佳答案

您的错误日志指出了一个警告和一个链接错误

1) 您的 2 个第一个警告说您正在使用 c++11 功能,但未指定使用 c++11 标准进行编译。警告不会停止编译,除非您使用 -Werror 标志。但是您仍然应该通过在编译行中添加 -std=c++11 来修复它们。

2) 您的链接错误只是告诉您链接器找不到 std::cout。那是因为 gcc 没有正确链接 C++ 的标准库。添加此标志:-lstdc++,或改用 g++(来源:Compiling a C++ program with gcc)

关于c++ - 指针数组的 vector 方法 push_back 编译但不运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28798936/

相关文章:

C - 处理共享内存时出现段错误

c - 传递给函数的结构数组,取消引用 -> 和 * 之间的区别

c++ - 为什么 int 指针的 const vector 中的取消引用元素是可变的?

c++ - 在 C++ 中正确写入非阻塞套接字

php - PHP 使用什么样的数组?

php - sql连接等于数组

c - 将 +/- 字母等级定义为常量。 C

c++ - 未定义对 `SOIL_load_OGL_texture' 的引用?

c++ - 找不到可执行文件,请指定一个

c++ - "explicit template argument list not allowed"使用 g++,但使用 clang++ 编译?