这是我是在Linux系统上编写的C++代码。
在main()函数中创建了一个grid类的vector对象。
当我想要使用push_back往vector<grid>尾部添加元素时发现编译出错了。
#include <iostream>
#include <vector>
using namespace std;
class grid
{
public:
//构造、析构函数
grid()
{
number.resize(10,true);
number[0] = false;
count = 9;
result = 0;
}
grid(int n)
{
if(n>0 && n<10)
{
number.resize(10,false);
number[0] = true;
number[n] = true;
count = 1;
result = n;
}
else
{
number.resize(10,true);
number[0] = false;
count = 9;
result = 0;
}
}
grid(grid& san);
{
number = san.getNum();
count = san.getCount();
result = san.getResult();
}
vector<bool> getNum() {return number;}
int getCount() const {return count;}
int getResult() const {return result;}
~grid(){}
protected:
vector<bool> number;
int count;
int result;
};
int main()
{
vector<grid> Nata;
grid Nan;
Nata.push_back(Nan); //如果没有这句的话能够正常编译
//但是我无法看出是哪出了问题
return 0;
}
不知道有没有哪位大佬能帮我解释一下,这种情况要怎么处理。
下面是系统编译出错的提示代码。
In file included from /usr/include/i386-linux-gnu/c++/4.8/bits/c++allocator.h:33:0,
from /usr/include/c++/4.8/bits/allocator.h:46,
from /usr/include/c++/4.8/string:41,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from line.cpp:1:
/usr/include/c++/4.8/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = grid; _Args = {const grid&}; _Tp = grid]’:
/usr/include/c++/4.8/bits/alloc_traits.h:254:4: required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = grid; _Args = {const grid&}; _Alloc = std::allocator<grid>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8/bits/alloc_traits.h:393:57: required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = grid; _Args = {const grid&}; _Alloc = std::allocator<grid>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8/bits/stl_vector.h:906:34: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = grid; _Alloc = std::allocator<grid>; std::vector<_Tp, _Alloc>::value_type = grid]’
line.cpp:186:20: required from here
/usr/include/c++/4.8/ext/new_allocator.h:120:4: error: no matching function for call to ‘grid::grid(const grid&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
/usr/include/c++/4.8/ext/new_allocator.h:120:4: note: candidates are:
In file included from line.cpp:3:0:
grid.h:16:2: note: grid::grid(grid&)
grid(grid& san);
^
grid.h:16:2: note: no known conversion for argument 1 from ‘const grid’ to ‘grid&’
grid.h:15:2: note: grid::grid(int)
grid(int n);
^
grid.h:15:2: note: no known conversion for argument 1 from ‘const grid’ to ‘int’
grid.h:14:2: note: grid::grid()
grid();
^
grid.h:14:2: note: candidate expects 0 arguments, 1 provided
In file included from /usr/include/c++/4.8/vector:62:0,
from line.cpp:2:
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = grid; _Args = {grid}]’:
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<grid*>; _ForwardIterator = grid*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<grid*>; _ForwardIterator = grid*]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<grid*>; _ForwardIterator = grid*; _Tp = grid]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:281:69: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = grid*; _ForwardIterator = grid*; _Allocator = std::allocator<grid>]’
/usr/include/c++/4.8/bits/vector.tcc:415:43: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {const grid&}; _Tp = grid; _Alloc = std::allocator<grid>]’
/usr/include/c++/4.8/bits/stl_vector.h:911:27: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = grid; _Alloc = std::allocator<grid>; std::vector<_Tp, _Alloc>::value_type = grid]’
line.cpp:186:20: required from here
/usr/include/c++/4.8/bits/stl_construct.h:75:7: error: no matching function for call to ‘grid::grid(grid)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
/usr/include/c++/4.8/bits/stl_construct.h:75:7: note: candidates are:
In file included from line.cpp:3:0:
grid.h:16:2: note: grid::grid(grid&)
grid(grid& san);
^
grid.h:16:2: note: no known conversion for argument 1 from ‘grid’ to ‘grid&’
grid.h:15:2: note: grid::grid(int)
grid(int n);
^
grid.h:15:2: note: no known conversion for argument 1 from ‘grid’ to ‘int’
grid.h:14:2: note: grid::grid()
grid();
^
grid.h:14:2: note: candidate expects 0 arguments, 1 provided
若你要使用vector的push_back,拷贝构造函数的参数必须为常引用,原因可以自己查下。
拷贝构造函数,这里的形参使用了const,该形参类中的所有函数都要使用const来修饰,所以你这里的getNum()也要加上const修饰。
修改后如下
#include <iostream>
#include <vector>
using namespace std;
class grid
{
public:
//构造、析构函数
grid()
{
number.resize(10, true);
number[0] = false;
count = 9;
result = 0;
}
grid(int n)
{
if (n>0 && n<10)
{
number.resize(10, false);
number[0] = true;
number[n] = true;
count = 1;
result = n;
}
else
{
number.resize(10, true);
number[0] = false;
count = 9;
result = 0;
}
}
grid(const grid& san)
{
number = san.getNum();
count = san.getCount();
result = san.getResult();
}
vector<bool> getNum() const{ return number; }
int getCount() const { return count; }
int getResult() const { return result; }
~grid(){}
protected:
vector<bool> number;
int count;
int result;
};
int main()
{
vector<grid> Nata;
grid Nan;
Nata.push_back(Nan);
return 0;
}
给你改了一点。拷贝构造函数需要加个const修饰,详细原因可以看看https://www.cnblogs.com/engraver-lxw/p/7580403.html
#include <iostream>
#include <vector>
using namespace std;
class grid
{
public:
//构造、析构函数
grid();
grid(int n);
grid(const grid& san);
~grid() {};
vector<bool> getNum() { return number; }
int getCount() const { return count; }
int getResult() const { return result; }
protected:
vector<bool> number;
int count;
int result;
};
grid::grid()
{
number.resize(10, true);
number[0] = false;
count = 9;
result = 0;
}
grid::grid(int n)
{
if (n > 0 && n < 10)
{
number.resize(10, false);
number[0] = true;
number[n] = true;
count = 1;
result = n;
}
else
{
number.resize(10, true);
number[0] = false;
count = 9;
result = 0;
}
}
grid::grid( const grid& san)
{
number = san.number;
count = san.count;
result = san.result;
}
int main()
{
vector<grid> Nata;
grid Nan;
Nata.push_back(Nan); //如果没有这句的话能够正常编译
//但是我无法看出是哪出了问题
return 0;
}
好几个错误
1.把number的定义放到上面去
2.grid(grid& san); 把这后面的分号去掉
3.grid(const grid& san) 加上const
学到了,谢谢大佬们。
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y