当前位置: 技术问答>linux和unix
大家来看看,为什么我的这个vector向量定义在VC下可以运行,在Linux下编译通不过?
来源: 互联网 发布时间:2015-07-21
本文导语: #include #include typedef struct mystruct { int x; int y; }MyStruct; void main() { vector str; mystruct *t; str.at(0) = t; } | 编译命令恐怕是原因 #include using namespace std; typedef struct mystru...
#include
#include
typedef struct mystruct
{
int x;
int y;
}MyStruct;
void main()
{
vector str;
mystruct *t;
str.at(0) = t;
}
#include
typedef struct mystruct
{
int x;
int y;
}MyStruct;
void main()
{
vector str;
mystruct *t;
str.at(0) = t;
}
|
编译命令恐怕是原因
#include
using namespace std;
typedef struct mystruct
{
int x;
int y;
}MyStruct;
int main()
{
vector str;
mystruct *t = new MyStruct();
t->x = 3;
t->y = 4;
str.at(0) = t;
return 0;
}
g++ vector.cpp -Wall -g3 -o v
gcc vector.cpp -Wall -g3 -o v
/tmp/ccNLSFCp.o(.text+0x6d): In function `main':
/home/kenneth/test/vector.cpp:14: undefined reference to `operator new(unsigned)'
/tmp/ccNLSFCp.o(.text+0x9a):/home/kenneth/test/vector.cpp:14: undefined reference to `operator delete(void*)'
/tmp/ccNLSFCp.o(.gnu.linkonce.t._ZNKSt6vectorIP8mystructSaIS1_EE14_M_range_checkEj+0x22): In function `std::vector::_M_range_check(unsigned) const':
/usr/include/c++/3.2.2/bits/stl_construct.h:143: undefined reference to `std::__throw_out_of_range(char const*)'
/tmp/ccNLSFCp.o(.gnu.linkonce.t._ZNSt14__simple_allocIP8mystructSt24__default_alloc_templateILb1ELi0EEE10deallocateEPS1_j+0x1a): In function `std::__simple_alloc::deallocate(mystruct**, unsigned)':
/usr/include/c++/3.2.2/bits/stl_vector.h:304: undefined reference to `std::__default_alloc_template::deallocate(void*, unsigned)'
/tmp/ccNLSFCp.o(.eh_frame+0x12):/home/kenneth/test/vector.cpp:13: undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
#include
using namespace std;
typedef struct mystruct
{
int x;
int y;
}MyStruct;
int main()
{
vector str;
mystruct *t = new MyStruct();
t->x = 3;
t->y = 4;
str.at(0) = t;
return 0;
}
g++ vector.cpp -Wall -g3 -o v
gcc vector.cpp -Wall -g3 -o v
/tmp/ccNLSFCp.o(.text+0x6d): In function `main':
/home/kenneth/test/vector.cpp:14: undefined reference to `operator new(unsigned)'
/tmp/ccNLSFCp.o(.text+0x9a):/home/kenneth/test/vector.cpp:14: undefined reference to `operator delete(void*)'
/tmp/ccNLSFCp.o(.gnu.linkonce.t._ZNKSt6vectorIP8mystructSaIS1_EE14_M_range_checkEj+0x22): In function `std::vector::_M_range_check(unsigned) const':
/usr/include/c++/3.2.2/bits/stl_construct.h:143: undefined reference to `std::__throw_out_of_range(char const*)'
/tmp/ccNLSFCp.o(.gnu.linkonce.t._ZNSt14__simple_allocIP8mystructSt24__default_alloc_templateILb1ELi0EEE10deallocateEPS1_j+0x1a): In function `std::__simple_alloc::deallocate(mystruct**, unsigned)':
/usr/include/c++/3.2.2/bits/stl_vector.h:304: undefined reference to `std::__default_alloc_template::deallocate(void*, unsigned)'
/tmp/ccNLSFCp.o(.eh_frame+0x12):/home/kenneth/test/vector.cpp:13: undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
|
templates can be used only in C++ not C
so use g++ instead of gcc
so use g++ instead of gcc
|
str.push_back(t);
|
首先 vector应该#include
其次 记得using namespace std;
第三 void main应为int main
最后 你系统的学过C++ STL么?
其次 记得using namespace std;
第三 void main应为int main
最后 你系统的学过C++ STL么?
|
OS is redhat 8.0?
|
hai