首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在引发'std::logic_error‘what():basic_string::_M_construct null无效实例后调用的终止

在引发'std::logic_error‘what():basic_string::_M_construct null无效实例后调用的终止
EN

Stack Overflow用户
提问于 2018-09-02 22:36:26
回答 3查看 6.4K关注 0票数 3

以下是说明:

在第10章中,clockType类被设计成在程序中实现一天中的时间。某些应用程序,除了小时、分钟和秒外,可能需要存储时区。

通过添加一个成员变量来存储时区,从类extClockType派生类clockType。添加必要的成员函数和构造函数,以使类具有功能。另外,编写成员函数和构造函数的定义。最后,编写一个测试程序来测试您的类。

这是我的档案:

clockType.h

代码语言:javascript
运行
复制
//clockType.h, the specification file for the class clockType

#ifndef H_ClockType

#define H_ClockType

class clockType

{

public:

void setTime(int hours, int minutes, int seconds);

//Function to set the time.

//The time is set according to the parameters.

//Postcondition: hr = hours; min = minutes;

// sec = seconds

// The function checks whether the values of

// hours, minutes, and seconds are valid. If a

// value is invalid, the default value 0 is

// assigned.

​

void getTime(int& hours, int& minutes, int& seconds) const;

//Function to return the time.

//Postcondition: hours = hr; minutes = min;

// seconds = sec

​

void printTime() const;

//Function to print the time.

//Postcondition: The time is printed in the form

// hh:mm:ss.

​

void incrementSeconds();

//Function to increment the time by one second.

//Postcondition: The time is incremented by one

// second.

// If the before-increment time is 23:59:59, the

// time is reset to 00:00:00.

​

void incrementMinutes();

//Function to increment the time by one minute.

//Postcondition: The time is incremented by one

// minute.

// If the before-increment time is 23:59:53,

// the time is reset to 00:00:53.

​

void incrementHours();

//Function to increment the time by one hour.

//Postcondition: The time is incremented by one

// hour.

// If the before-increment time is 23:45:53, the

// time is reset to 00:45:53.

​

bool equalTime(const clockType& otherClock) const;

//Function to compare the two times.

//Postcondition: Returns true if this time is

// equal to otherClock; otherwise,

// returns false.

​

clockType(int hours, int minutes, int seconds);

//constructor with parameters

//The time is set according to the parameters.

//Postcondition: hr = hours; min = minutes;

// sec = seconds

// The constructor checks whether the values of

// hours, minutes, and seconds are valid. If a

// value is invalid, the default value 0 is

// assigned.

​

clockType();

//default constructor with parameters

//The time is set to 00:00:00.

//Postcondition: hr = 0; min = 0; sec = 0

private:

int hr; //variable to store the hours

int min; //variable to store the minutes

int sec; //variable to store the seconds

};

#endif

clockTypeImp.cpp

代码语言:javascript
运行
复制
//Implementation File for the class clockType

#include <iostream>

#include "clockType.h"

​

using namespace std;

void clockType::setTime(int hours, int minutes, int seconds)

{

if (0 <= hours && hours < 24)

hr = hours;

else

hr = 0;

​

if (0 <= minutes && minutes < 60)

min = minutes;

else

min = 0;

​

if (0 <= seconds && seconds < 60)

sec = seconds;

else

sec = 0;

}

​

void clockType::getTime(int& hours, int& minutes,

int& seconds) const

{

hours = hr;

minutes = min;

seconds = sec;

}

​

void clockType::incrementHours()

{

hr++;

if (hr > 23)

hr = 0;

}

​

void clockType::incrementMinutes()

{

min++;

if (min > 59)

{

min = 0;

incrementHours();

}

}

​

void clockType::incrementSeconds()

{

sec++;

​

if (sec > 59)

{

sec = 0;

incrementMinutes();

}

}

​

void clockType::printTime() const

{

if (hr < 10)

cout << "0";

cout << hr << ":";

​

if (min < 10)

cout << "0";

cout << min << ":";

​

if (sec < 10)

cout << "0";

cout << sec;

}

​

bool clockType::equalTime(const clockType& otherClock) const

{

return (hr == otherClock.hr

&& min == otherClock.min

&& sec == otherClock.sec);

}

​

clockType::clockType(int hours, int minutes, int seconds)

{

if (0 <= hours && hours < 24)

hr = hours;

else

hr = 0;

​

if (0 <= minutes && minutes < 60)

min = minutes;

else

min = 0;

​

if (0 <= seconds && seconds < 60)

sec = seconds;

else

sec = 0;

}

​

clockType::clockType() //default constructor

{

hr = 0;

min = 0;

sec = 0;

}

​

(给出了前两个文件)

(从这里开始,我创建代码)

extClockType.h

代码语言:javascript
运行
复制
​

#ifndef H_extClockType

#define H_extClockType

#include<iostream>

#include "clockType.h"

using namespace std;

​

class extClockType: public clockType

{

public:

extClockType();

extClockType(int, int, int, string);

void setTime(int hours, int minutes, int seconds, string zone);

string printTimezone();

string getTimezone();

private:

string zone;

};

#endif

extClockTypeImp.cpp

代码语言:javascript
运行
复制
#include <iostream>

#include "clockType.h"

#include "extClockType.h"

​

using namespace std;

extClockType::extClockType(): clockType()

{

zone = "na";

}

​

extClockType::extClockType(int hours, int minutes, int seconds, string time_zone)

{

clockType::setTime(hours, minutes, seconds);

zone = time_zone;

}

void extClockType::setTime(int hours, int minutes, int seconds, string zone)

{

clockType::setTime(hours, minutes, seconds);

}

string extClockType::getTimezone()

{

return zone;

}

​

string extClockType::printTimezone()

{

clockType::printTime();

cout << " " << zone << endl;

return 0;

}

main.cpp

代码语言:javascript
运行
复制
#include <string>

#include <iomanip>

#include <iostream>

#include "clockType.h"

#include "extClockType.h"

​

using namespace std;

​

int main()

{

extClockType time1(5, 10, 34, "CST");

cout << "Time 1: ";

time1.printTimezone();

cout<<endl;

extClockType time2;

time2.setTime(12, 45, 59, "PST");

cout<< "Time 2: ";

time2.printTimezone();

cout<<endl;

​

time2.incrementSeconds();

cout<< "After incrementing time 2 by one second, Time 2:";

time2.printTime();

}

​

这是我的错误信息:

代码语言:javascript
运行
复制
Time 1: 05:10:34 CST

terminate called after throwing an instance of 'std::logic_error'

what(): basic_string::_M_construct null not valid

很抱歉我的帖子太长了,我只想展示我正在处理的所有文件。如果有人愿意帮助我,那将是一个巨大的帮助!

EN

回答 3

Stack Overflow用户

发布于 2018-09-02 23:16:06

此错误只意味着您已经调用了字符串构造函数:

代码语言:javascript
运行
复制
std::string::string(const char *p);

使用NULL指针,这不是一件有效的事情。

使用调试器可以找出代码的确切位置,并将其修复为不这样做。

票数 4
EN

Stack Overflow用户

发布于 2018-09-02 23:18:09

一个错误是:

代码语言:javascript
运行
复制
string extClockType::printTimezone()
{
    clockType::printTime();
    cout << " " << zone << endl;
    return 0;  // This is not legal, converting an int to a std::string
}

返回时,您将返回0,并且正在尝试将其转换为std::stringstd::string没有接受int参数的构造函数(在本例中为0)。相反,将引发异常。

您应该不返回任何内容(因此函数应该声明为返回void),或者返回一些创建有效std::string的内容。

注意:为std::string调用的构造函数是接受单个const char *参数(见构造函数(5))的构造函数。在这种情况下,0被转换为nullptr,因此在返回时尝试构造一个std::string(nullptr)

票数 1
EN

Stack Overflow用户

发布于 2018-09-02 23:21:25

毕竟很容易

代码语言:javascript
运行
复制
string extClockType::printTimezone() {
   ... 
   return 0;
}

将其更改为此,并更新原型:

代码语言:javascript
运行
复制
void extClockType::printTimezone() {
   ... 
   return;
}

这将尝试构造带有0的std::string,将其解释为nullptr。根据使用情况,返回值可能为空。

修复后,输出如下:

时间1: 05:10:34科技委

时间2: 12:45:59 na

增加时间2秒后,时间2:12:46:00

请注意,“na”是

代码语言:javascript
运行
复制
void setTime(int hours, int minutes, int seconds, string zone);

实际上没有设置“zone”成员变量。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52141476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档