简体   繁体   English

有c ++默认构造函数的问题?

[英]Having trouble with c++ default constructor?

I already have a constructor in my .h and .cpp file which takes some arguments but I also need a default argument which I'm not sure how to make because the way I tried it compiles but I get errors when I run my testfile. 我已经在我的.h和.cpp文件中有一个构造函数,它接受一些参数,但我还需要一个默认参数,我不知道如何制作,因为我尝试编译的方式,但是当我运行我的测试文件时出现错误。 This is my .h file 这是我的.h文件

public:
Class();
Class(const std::string &, const int)
void getInfo();
std::string listItem();

private:

std::string name;
int quantity;

This is my .cpp file 这是我的.cpp文件

Class::Class()
: name(0), quantity(0)
{
Class::Class(const string &nam, const int quant)
: name(nam),quantity(quant)
{
}
void Class::getInfo()
{
cout << "Enter Name: ";
cin >> name
cout << "Enter quantity: ";
cin >> quantity;
}
string Class::listItem()
{
ostringstream outputString;
outputString << getName() << getQuantity();
return outputString.str();
}

And this is the part of my test causing trouble: 这是我的测试导致麻烦的一部分:

const int shortList = 2;
array<Class*, shortList> newList;

 for (int i=0; i< 2; i++){
        Class *p = new Class();
        p->getInfo();
        newList[i] = p;
 }
 cout << "newList contains: " << endl;
 for (Class* p : newList)
            cout << p->listItem() << endl;

I get : terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 我得到:在抛出'std :: logic_error'的实例后调用终止what():basic_string :: _ S_construct null无效

Is it a constructor issue or is it some syntax error? 它是构造函数问题还是一些语法错误?

The problem is in the default constructor's initialiser list: 问题出在默认构造函数的初始化列表中:

name(0)

This attempts to initialise the string using the constructor taking a C-style string pointer, char const* , with a null pointer value. 这会尝试使用带有空指针值的C样式字符串指针char const*的构造函数初始化字符串。 You then get a runtime error since you're not allowed to pass a null pointer to that constructor. 然后,您将收到运行时错误,因为您不允许将空指针传递给该构造函数。

To initialise the string to be empty, either specify default initialisation (or, pedantically, value-initialisation, which amounts to the same thing for this type) 要将字符串初始化为空,请指定默认初始化(或者,迂腐地,值初始化,这对于此类型来说相同)

name()

or leave it out of the initialiser list. 或者将其从初始化列表中删除。

Assuming there are no intentional typos in the above code, there are semi-colons missing in very key locations, as well as curly brace use looking incorrect and missing declarations in the header. 假设在上面的代码中没有故意的拼写错误,在非常关键的位置缺少分号,以及在标题中看起来不正确和缺少声明的大括号使用。

The new lines and/or characters are noted with a comment starting with // added ... 注释新行和/或字符,注释以// added ...开头

Starting with the header file: 从头文件开始:

class Class        // added
{                  // added
public:
    Class();
    Class(const std::string &, const int); // added semi-colon
    void getInfo();
    std::string listItem();

private:
    std::string name;
    int quantity;
};   // added closing curly brace and semi-colon

The .cpp source file: .cpp源文件:

Class::Class()
: name(""), quantity(0) // modified name initial value to the empty string
{
}  // added curly brace

Class::Class(const string &nam, const int quant)
: name(nam),quantity(quant)
{
}

void Class::getInfo()
{
    cout << "Enter Name: ";
    cin >> name;    // added semi-colon
    cout << "Enter quantity: ";
    cin >> quantity;
}
string Class::listItem()
{
    ostringstream outputString;
    outputString << getName() << getQuantity();
    return outputString.str();
}

Later on the code that is causing fits is: 稍后导致拟合的代码是:

const int shortList = 2;
array<Class*, shortList> newList;

for (int i=0; i< shortList; i++){ // changed bounds check for i to the const int shortList
     Class *p = new Class();
     p->getInfo();
     newList[i] = p;
}
cout << "newList contains: " << endl;

//
// changed to declare auto instead.  
// As a pointer declaration, there is a chance the Class copy constructor is being called
// inside the loop body prior to the dereference.  It should not be, but... 
// In my opinion, it is much more likely that setting the name to a zero initial value
// in the Class() constructor is the real problem cause as Mike says above.
//
for (auto p : newList)
     cout << p->listItem() << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM