Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ g+出现意外的编译问题+-std=c++;0x_C++_C++11 - Fatal编程技术网

C++ g+出现意外的编译问题+-std=c++;0x

C++ g+出现意外的编译问题+-std=c++;0x,c++,c++11,C++,C++11,当使用g++-std=c++0x进行编译时,我在将T类型的元素推回向量时遇到了一些编译问题 这是一个简单的例子: #include <vector> using namespace std; class A { public: A() { } A& operator=(A &orig) { return *this; } }; int main(int argc, char **argv) { A a;

当使用g++-std=c++0x进行编译时,我在将T类型的元素推回向量时遇到了一些编译问题

这是一个简单的例子:

#include <vector>

using namespace std;

class A {
public:
    A() { }

    A& operator=(A &orig) {
        return *this;
    }
};

int main(int argc, char **argv) {
    A a;
    vector<A> b;
    A c = a; // This is fine
    b.push_back(a); // This is not, but only when compiling with -std=c++0x!
    return 0;
}
#包括
使用名称空间std;
甲级{
公众:
A(){}
A运算符=(A运算符和原始运算符){
归还*这个;
}
};
int main(int argc,字符**argv){
A A;
载体b;
A c=A;//这很好
b、 push_back(a);//这不是,但只有在使用-std=c++0x进行编译时才是!
返回0;
}
使用g++-Wall-pedantic可以很好地编译,但使用g++-Wall-pedantic-std=c++0x编译时会出现以下错误:

 In file included from /usr/include/c++/4.4/vector:69,
                 from min.cpp:1:
/usr/include/c++/4.4/bits/vector.tcc: In member function ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const A&, _Tp = A, _Alloc = std::allocator<A>]’:
/usr/include/c++/4.4/bits/stl_vector.h:741:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>]’
min.cpp:20:   instantiated from here
/usr/include/c++/4.4/bits/vector.tcc:314: error: no match for ‘operator=’ in ‘__position.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = A*, _Container = std::vector<A, std::allocator<A> >]() = ((const A&)((const A*)std::forward [with _Tp = const A&](((const A&)((const A*)__args#0)))))’
min.cpp:11: note: candidates are: A& A::operator=(A&)
In file included from /usr/include/c++/4.4/vector:61,
                 from min.cpp:1:
/usr/include/c++/4.4/bits/stl_algobase.h: In static member function ‘static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = A*, _BI2 = A*]’:
/usr/include/c++/4.4/bits/stl_algobase.h:595:   instantiated from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = A*, _BI2 = A*]’
/usr/include/c++/4.4/bits/stl_algobase.h:605:   instantiated from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = A*, _BI2 = A*]’
/usr/include/c++/4.4/bits/stl_algobase.h:676:   instantiated from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = A*, _BI2 = A*]’
/usr/include/c++/4.4/bits/vector.tcc:308:   instantiated from ‘void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = const A&, _Tp = A, _Alloc = std::allocator<A>]’
/usr/include/c++/4.4/bits/stl_vector.h:741:   instantiated from ‘void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = A, _Alloc = std::allocator<A>]’
min.cpp:20:   instantiated from here
/usr/include/c++/4.4/bits/stl_algobase.h:561: error: no match for ‘operator=’ in ‘* -- __result = std::move [with _Tp = A&](((A&)(-- __last)))’
min.cpp:11: note: candidates are: A& A::operator=(A&)
在/usr/include/c++/4.4/vector:69中包含的文件中,
从min.cpp:1:
/usr/include/c++/4.4/bits/vector.tcc:在成员函数“void std::vector::_M_insert_aux”(uu gnu_cxx::u normal_迭代器,_Args&&…[带有_Args=const A&,_Tp=A,_Alloc=std::分配器]:
/usr/include/c++/4.4/bits/stl_vector.h:741:从“void std::vector::push_back(const _Tp&)[使用_Tp=A,_Alloc=std::allocator]实例化”
min.cpp:20:从此处实例化
/usr/include/c++/4.4/bits/vector.tcc:314:error:in'.'位置中的'operator='不匹配。'uuuuu gnu cxx:::'uu normal_迭代器::operator*[with u iterator=A*,u Container=std::vector]()=((const A&)((const A*)std::forward[with u Tp=const A&]((const A&)()((const A*))u args#
min.cpp:11:注:候选项为:A&A::operator=(A&)
在/usr/include/c++/4.4/vector:61中包含的文件中,
从min.cpp:1:
/usr/include/c++/4.4/bits/stl_algobase.h:在静态成员函数“static”中,std::_copy_move_backward::_copy_move_b(_BI1,_BI1,_BI2)[带_BI1=A*,_BI2=A*]:
/usr/include/c++/4.4/bits/stl_algobase.h:595:从“_bi2std::_复制_向后移动_a”(_BI1,_BI1,_BI2)[带bool _IsMove=true,_BI1=a*,_BI2=a*]”实例化
/usr/include/c++/4.4/bits/stl_algobase.h:605:从“_-BI2 std::_-copy_-move_-backward_-a2”(_-BI1,_-BI1,_-BI2)[带bool-IsMove=true,_-BI1=A*,_-BI2=A*]实例化
/usr/include/c++/4.4/bits/stl_algobase.h:676:从“_bi2std::move_backward(_BI1,_BI1,_BI2)[带_BI1=A*,_BI2=A*]”实例化
/usr/include/c++/4.4/bits/vector.tcc:308:从“void std::vector::_M_insert_aux(__gnu_cxx::_normal_iterator,_Args&&…[带有_Args=const A&,_Tp=A,_Alloc=std::allocator]”实例化
/usr/include/c++/4.4/bits/stl_vector.h:741:从“void std::vector::push_back(const _Tp&)[使用_Tp=A,_Alloc=std::allocator]实例化”
min.cpp:20:从此处实例化
/usr/include/c++/4.4/bits/stl_-algobase.h:561:错误:在'*-\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
min.cpp:11:注:候选项为:A&A::operator=(A&)

所以它似乎没有找到A的正确运算符。为什么?为什么在我通过A时用_Iterator=A*表示

我确信这是一个安全特性。在标准容器中使用可能会变异右侧的复制赋值运算符(或复制构造函数)的类型是不安全的—例如(现在已弃用的)
std::auto_ptr
,如果存储在容器中,它将严重损坏


旧的C++03库实现允许这种不安全的代码,但显然,它们在C++0x版本中实现了编译时检查——可能与启用容器的移动相结合。

标准对复制分配运算符的定义是(第节
[class.copy]
):

用户声明的复制赋值运算符
X::operator=
是类
X
的非静态非模板成员函数,只有一个参数类型为
X
X&
常量X&
volatile X&
常量volatile X&

但是
X&
volatile X&
变体可能与容器不兼容,前提是可以从r值RHS进行赋值

注意:按值传递,例如,
X::operator=(X)
是复制和交换习惯用法的基本部分。

语言标准对标准容器元素施加的可赋值要求要求
t=u
表达式有效,即使
u
是常量对象。自C++98(见23.1/4)以来,需求就是这样定义的

您违反了该要求,因为赋值运算符不接受常量对象。这立即意味着类
A
不能用作容器元素类型

它在C++03中工作的原因与此无关。它是偶然起作用的。从错误消息中可以明显看出,库的C++0x实现使用了一些特定于C++0x的特性(如
std::move
),这正是上述要求发挥作用的原因。但是无论如何,C++03实现(甚至C++98实现)也可能无法为
a
编译

你的例子是
A c=A
是不相关的,因为它根本不使用赋值运算符(为什么在这里?)


为了修复错误,您应该通过常量引用或值来接受参数。

如果赋值运算符采用
const
-reference(它总是应该这样做),那么它会起作用。如果我将它声明为&operator=(a*orig),那么它也会起作用,但为什么?为什么它能与“plain”g++一起工作?
A&operator=(A*orig)
是赋值运算符,但不是复制赋值运算符,因此编译器会隐式地为您定义默认的复制赋值运算符(就像您根本没有定义赋值运算符一样)。这将完全回答我的问题,虽然选择的错误消息会很糟糕:)感谢您的明确解释。最后,您同意JohannesD的观点,为什么它在C++03中失败,而在C++0x中失败?谢谢。可分配的需求只在C++0x中定义,或者它只是执行者