C++ nullptr可以';t在数组中使用

C++ nullptr可以';t在数组中使用,c++,c++11,C++,C++11,为什么不能在构造函数中使用nullptr?(函数名:Wine)当我尝试这样做时,程序会崩溃并且没有任何错误报告,可能是因为我不知道原因 #ifndef WINE_H_ #define WINE_H_ #include<iostream> #include<string> #include<valarray> using std::string; using std::valarray; template<typename T1, typename T2&

为什么不能在构造函数中使用nullptr?(函数名:Wine)当我尝试这样做时,程序会崩溃并且没有任何错误报告,可能是因为我不知道原因

#ifndef WINE_H_
#define WINE_H_
#include<iostream>
#include<string>
#include<valarray>
using std::string;
using std::valarray;
template<typename T1, typename T2>
class Pair                                    //member of the wine
{
private:
    T1 a;
    T2 b;
public:
    T1 & first(){ return a; }
    T2 & second(){ return b; }
    T1 first()const{ return a; }
    T2 second()const{ return b; }
    Pair(const T1 & aval, const T2 & bval) :a(aval), b(bval){}
    Pair(){}
};
typedef valarray<int>ArrayInt;
typedef Pair<ArrayInt, ArrayInt>PairArray;

class Wine
{
private:
    string name;
    PairArray bt;
    int years;
public:
    Wine();                                          
    Wine(const char * a, int y,int b[], int c[]);    //no problem
    Wine(const char * a, int y);                    //here is that problem function
    void GetBottles();                              //no problem
    void Show()const;                               //no problem
    int Sum(){ return bt.second().sum(); }
};

Wine::Wine(const char * a, int y) :name(a), years(y), bt(ArrayInt(0, y), ArrayInt(0, y)){} 
**//When I am trying to use nullptr to instead 0 in the ArrayInt(0,y),the whole program will break down during work.**


Wine::Wine(const char * a, int y, int b[], int c[]) :bt(ArrayInt(b, y), ArrayInt(c, y))
{
    name = a;
    years = y;
}

Wine::Wine() :bt(ArrayInt(),ArrayInt())
{
    name = "null";
    years = 0;
}

void Wine::GetBottles()
{
    std::cout << "Please input the years and the bottles\n";
    for (int i = 0; i < years; i++)
    {
        std::cout << "input the year: ";
        (std::cin >> bt.first()[i]).get();
        std::cout << "input the bottles";
        (std::cin >> bt.second()[i]).get(); 
    }
}

void Wine::Show()const
{
    using std::cout;
    using std::endl;
    for (int i = 0; i < years; i++)
    {
        cout << bt.first()[i] << '\0' << bt.second()[i] << endl;
    }
}

#endif

#include<iostream>                          //test part
#include"wine.h"

int main(void)
{
    using std::cin;
    using std::cout;
    using std::endl;

    cout << "Enter name of wine: ";
    char lab[50];
    cin.getline(lab, 50);
    cout << "Enter number of years: ";
    int yrs;
    cin >> yrs;

    Wine holding(lab, yrs);
    holding.GetBottles();
    holding.Show();
    return 0;

}
\ifndef WINE\u H_
#定义葡萄酒_
#包括
#包括
#包括
使用std::string;
使用std::valarray;
模板
类对//葡萄酒的成员
{
私人:
T1α;
t2b;
公众:
T1&first(){return a;}
T2&second(){return b;}
T1 first()常量{return a;}
T2 second()常量{return b;}
配对(常数T1和aval,常数T2和bval):a(aval),b(bval){}
对(){}
};
typedef valarrayArrayInt;
typedef PairPairArray;
品酒
{
私人:
字符串名;
白三烯;
整数年;
公众:
葡萄酒();
Wine(const char*a,int y,int b[],int c[]);//没问题
Wine(const char*a,int y);//这是问题函数
void getVa瓶();//没问题
void Show()const;//没问题
int Sum(){返回bt.second().Sum();}
};
葡萄酒:葡萄酒(const char*a,int y):名称(a),年份(y),bt(ArrayInt(0,y),ArrayInt(0,y)){}
**//当我试图在ArrayInt(0,y)中使用nullptr代替0时,整个程序将在工作期间崩溃**
葡萄酒:葡萄酒(const char*a,inty,intb[],intc[]):bt(ArrayInt(b,y),ArrayInt(c,y))
{
名称=a;
年=y;
}
葡萄酒::葡萄酒():bt(ArrayInt(),ArrayInt())
{
name=“null”;
年=0;
}
void Wine::getballs()
{
std::cout bt.first()[i]).get();
std::cout>bt.second()[i]).get();
}
}
void Wine::Show()常量
{
使用std::cout;
使用std::endl;
对于(int i=0;icout这是一个有趣的例子。它在一个例子中被打断,而在另一个例子中没有被打断的原因如下:

对于
std::valarray
,有两个不同的构造函数(不仅如此,但这两个都很重要):

当您使用0(
valarray(0,y)
)时,您正在调用第一个版本-创建
y
元素的数组,其中每个元素都初始化为0

但是当您使用
nullptr
调用它时,您正在调用它的第二个版本-尝试使用第一个参数指向构造函数的数组的副本初始化新数组。但是您的第一个参数是
nullptr
,任何尝试将at用作数组的行为都会触发未定义的行为,并导致程序崩溃是的

valarray( const T& val, std::size_t count ); // 1
valarray( const T* vals, std::size_t count ); // 2