C++ &引用;“错误”中包含的文件中;带向量

C++ &引用;“错误”中包含的文件中;带向量,c++,vector,C++,Vector,我试图创建一个聚合类Pile,它以卡片向量作为数据 在过去的3个小时里,我一直在试图弄清楚这个错误到底是怎么回事,我被难住了。以下是错误: In file included from /usr/include/c++/5/vector:62:0, from Pile.h:16, from Pile.cpp:15: /usr/include/c++/5/bits/stl_construct.h: In instantiation

我试图创建一个聚合类Pile,它以卡片向量作为数据

在过去的3个小时里,我一直在试图弄清楚这个错误到底是怎么回事,我被难住了。以下是错误:

In file included from /usr/include/c++/5/vector:62:0,
                 from Pile.h:16,
                 from Pile.cpp:15:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = Card; _Args = {}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:519:18:   required from ‘static _ForwardIterator std::__uninitialized_default_n_1<_TrivialValueType>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Card*; _Size = long unsigned int; bool _TrivialValueType = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:575:20:   required from ‘_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Card*; _Size = long unsigned int]’
/usr/include/c++/5/bits/stl_uninitialized.h:637:44:   required from ‘_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Card*; _Size = long unsigned int; _Tp = Card]’
/usr/include/c++/5/bits/stl_vector.h:1311:36:   required from ‘void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Card; _Alloc = std::allocator<Card>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]’
/usr/include/c++/5/bits/stl_vector.h:279:30:   required from ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Card; _Alloc = std::allocator<Card>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Card>]’
<span class="error_line" onclick="ide.gotoLine('Pile.cpp',27)">Pile.cpp:27:23</span>:   required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: no matching function for call to ‘Card::Card()’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
In file included from Pile.h:17:0,
                 from Pile.cpp:15:
card.h:30:5: note: candidate: Card::Card(const Card&)
     Card(const Card& old);     //copy constructor
     ^
card.h:30:5: note:   candidate expects 1 argument, 0 provided
card.h:29:5: note: candidate: Card::Card(int, char)
     Card(int r, char s); //parameterized constructor
     ^
card.h:29:5: note:   candidate expects 2 arguments, 0 provided

Call Stack
#   Function    File:Line
Local Variables
Variable    Value
Display Expressions
Expression  Value   
Breakpoints and Watchpoints
    #   Description 
在/usr/include/c++/5/vector:62:0中包含的文件中,
从桩h:16,
来自Pile.cpp:15:
/usr/include/c++/5/bits/stl_-construct.h:在“void std::_-construct(_-T1*,_-Args&&&…[带_-T1=Card;_-Args={}]”的实例化中:
/usr/include/c++/5/bits/stl_uninitialized.h:519:18:来自“静态”前向迭代器std::“未初始化”前向迭代器默认值n_1:::“未初始化”前向迭代器默认值n(_前向迭代器,_大小)[带_前向迭代器=卡*;_大小=长无符号整数;布尔值类型=假]”
/usr/include/c++/5/bits/stl_uninitialized.h:575:20:必须来自“_forwarditeratorstd::_uninitialized_default_n(_ForwardIterator,_Size)[带_ForwardIterator=Card*;_Size=long unsigned int]”
/usr/include/c++/5/bits/stl_uninitialized.h:637:44:从“_forwarditeratorstd::_uninitialized_default_n_a(_ForwardIterator,_Size,std::allocator&)[带_ForwardIterator=Card*;_Size=long unsigned int;_Tp=Card]”中的必填项
/usr/include/c++/5/bits/stl_vector.h:1311:36:必须来自“void std::vector::_M_default_initialize(std::vector::size_type)[带Tp=Card;_Alloc=std::Alloc=分配器;std::vector::size_type=long unsigned int]'
/usr/include/c++/5/bits/stl_vector.h:279:30:std::vector::vector(std::vector::size_-type,const-allocator_-type&)中的必填项[withTp=Card;Alloc=std::Alloc=allocator;std::vector::size_-type=long unsigned int;std::vector::allocator-type=std::allocator]
桩号cpp:27:23:此处需要
/usr/include/c++/5/bits/stl_construct.h:75:7:错误:调用“Card::Card()”时没有匹配的函数
{::新建(静态_-cast(u-p))_-T1(标准::转发(u-args));}
^
在Pile.h:17:0中包含的文件中,
来自Pile.cpp:15:
卡片h:30:5:注:候选人:卡片::卡片(常量卡片&)
卡片(常卡和旧卡)//复制构造函数
^
card.h:30:5:注意:候选者需要1个参数,提供0个参数
card.h:29:5:note:candidate:card::card(int,char)
卡片(整数r,字符s)//参数化构造函数
^
card.h:29:5:注意:候选者需要2个参数,提供0个
调用堆栈
#函数文件:行
局部变量
可变值
显示表达式
表达式值
断点和观察点
#描述
我觉得我很接近,我有一大堆其他的错误,我可以解决,但我不能通过这个“在文件中包括从”的错误,因为我不明白它甚至意味着什么。我已经单独测试了我的card类,它按预期运行

这是我的桩号。h代码:

#ifndef PILE_H
#define PILE_H
#include <vector>
#include "card.h"

using namespace std;

class Pile 
{
protected:
    vector<Card> p;

public:

    Pile();
    Pile(vector<Card> p);

    Card dealCard();
    int getCount();
    void shuffle();
    void clear();
    Pile operator + (const Card& c) const;
    Pile operator + (Pile& b);
    friend ostream& operator << (ostream& out, const Pile& p);

};
\ifndef PILE\u H
#定义桩号
#包括
#包括“card.h”
使用名称空间std;
类桩
{
受保护的:
向量p;
公众:
桩();
桩(向量p);
Card dealCard();
int getCount();
无效洗牌();
无效清除();
桩操作员+(常数卡和c)常数;
桩操作员+(桩和b);
friend-ostream&运算符p=p;
}
//从堆的顶部绘制卡片并将其从向量中移除
卡片堆::dealCard()
{
如果(p.size()>0)
{
Card tempCard=p.前();
p、 擦除(p.begin());
返回临时卡;
}
其他的
抛出标准::长度_错误(“桩为空”);
}
int Pile::getCount()
{
返回p.size();
}
void-Pile::shuffle()
{
srand(无符号(时间(空));
随机洗牌(p.begin(),p.end());
}
空堆::清除()
{
p、 清除();
}
桩桩::操作员+(常数卡和c)常数
{
Pile tempPile(p);//复制要返回的桩
tempPile.p.push_back(c);//将新卡推到牌堆的末端
return tempPile;//返回新的堆
}
桩桩::运算符+(桩和b)
{
桩温桩(p);
而(b.p.尺寸()>0)
{
tempPile.p.push_back(b.p.front());
b、 p.erase(b.p.begin());
}
返回临时堆;
}
ostream和运算符0)
{
out s='H';
else如果(s=='s'| | s=='s')//黑桃
这->s='s';
else如果(s=='c'| | s=='c')//
这->s=='C';
其他的
抛出std::无效的_参数(“诉讼必须有效(H,S,C,D”);
}
卡::卡(常量卡和旧卡)
{
r=旧的。r;
s=旧的。s;
}
无效卡片::设置卡片(int r,char s)
{
if(r0)
这个->r=r;
其他的
throw std::无效的_参数(“排名必须有效(1-13)”;
如果(s=='d'| | s=='d')//钻石
这->s='D';
else如果(s=='h'| | s=='h')//hearts
这->s='H';
else如果(s=='s'| | s=='s')//黑桃
这->s='s';
else如果(s=='c'| | s=='c')//
这->s=='C';
其他的
抛出std::无效的_参数(“诉讼必须有效(H,S,C,D”);
}
int Card::getRank()
{
返回r;
}
布尔卡::运算符==(常数卡c)常数
{
如果(r==c.r)
返回true;
其他的
返回false;
}
bool卡::操作员>(常数卡c)常数
{
如果(r>c.r)
返回true;
其他的
返回false;
}

C++类的Orths&运算符< P>默认构造函数丢失。当用户定义的构造函数(带有参数或没有参数)存在于类中时,C++不生成默认构造函数。
在代码中,默认的构造函数是在Pr.CPP中需要的。CP/C++的默认构造函数是27。

默认构造函数。当用户定义的构造函数(带有参数或没有参数)存在时,C++不生成默认构造函数。


在您的代码中,Pile.cpp:27中需要默认构造函数。

问题是这一行:

p = vector<Card>(0);
p=vector(0);
以这种方式初始化向量需要一个默认构造函数。请参阅。您可以简单地删除该行,因为它实际上是不必要的。一个
std::vector
无论如何都会以空开头,因此不需要执行冗余操作
#include <cstdlib>
#include "card.h"
#include <sstream>
#include <stdexcept>
using namespace std;

Card::Card(int r, char s)
{
    if(r <= 13 && r > 0)
        this->r = r;
    else
        throw std::invalid_argument("Rank must be valid (1-13)");

    if(s == 'd' || s == 'D')        //diamonds
        this->s = 'D';
    else if(s == 'h' || s == 'H')   //hearts
        this->s = 'H';
    else if(s == 's' || s == 'S')   //spades
        this->s = 'S';
    else if(s == 'c' || s == 'C')   //clubs
        this->s == 'C';
    else
        throw std::invalid_argument("Suit must be valid (H, S, C, D");
}

Card::Card(const Card& old)
{
    r = old.r;
    s = old.s;
}

void Card::setCard(int r, char s)
{
    if(r <= 13 && r > 0)
        this->r = r;
    else
        throw std::invalid_argument("Rank must be valid (1-13)");

    if(s == 'd' || s == 'D')        //diamonds
        this->s = 'D';
    else if(s == 'h' || s == 'H')   //hearts
        this->s = 'H';
    else if(s == 's' || s == 'S')   //spades
        this->s = 'S';
    else if(s == 'c' || s == 'C')   //clubs
        this->s == 'C';
    else
        throw std::invalid_argument("Suit must be valid (H, S, C, D");
}

int Card::getRank()
{
    return r;
}

 bool Card:: operator == (const Card c) const
 {
     if(r == c.r)
         return true;
     else
         return false;
 }

 bool Card:: operator >(const Card c) const
 {
     if(r > c.r)
         return true;
     else
         return false;
 }

 ostream& operator << (ostream& out, const Card c)
 {
     if(c.r == 1)
         out << "A" << c.s;
     else if(c.r > 1 && c.r <= 10)
         out << c.r << c.s;
     else if(c.r == 11)
         out << "J" << c.s;
     else if(c.r == 12)
         out << "Q" << c.s;
     else      //must be king
         out << "K" << c.s;
 }
#ifndef CARD_H
#define CARD_H

#include <string>
#include <iostream>
using namespace std;

class Card
{
private:
    int r;      
    char s;

public:

    Card(int r, char s); //parameterized constructor
    Card(const Card& old);     //copy constructor

    void setCard(int r, char s);
    int getRank();
    bool operator ==(const Card c) const;
    bool operator >(const Card c) const;
    friend ostream& operator << (ostream& out, const Card c);

};
p = vector<Card>(0);