问题遇到的现象和发生背景
我在学习C++ Primer plus 第十二章时,按照书中的内容,在源代码中加了复制构造函数(P355),但是运行的时候程序依旧崩了,提示"string is not full terminated" 代码停止位置和报错图如下:
问题相关代码,请勿粘贴截图
全部代码如下:
//"strngbad.h"
#include <iostream>
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
public:
StringBad(const char * s); // constructor
StringBad(); // default constructor
~StringBad(); // destructor
StringBad(const StringBad& st); //复制构造函数
StringBad& operator=(const StringBad & st);//赋值重载
// friend function
friend std::ostream & operator<<(std::ostream & os,
const StringBad & st);
};
#endif
//头文件内函数的实现文件
// strngbad.cpp -- StringBad class methods
#include <cstring> // string.h for some
#include "strngbad.h"
using std::cout;
using namespace std;
// initializing static class member
int StringBad::num_strings = 0;
// class methods
// construct StringBad from C string
StringBad::StringBad(const char * s)
{
len = strlen(s); // set size
str = new char[len + 1]; // allot storage
strcpy_s(str, len+1 , s); // initialize pointer
num_strings++; // set object count
cout << num_strings << ": \"" << str
<< "\" object created\n"; // For Your Information
}
StringBad::StringBad() // default constructor
{
len = 4;
str = new char[4];
strcpy_s(str,4, "C++"); // default string
num_strings++;
cout << num_strings << ": \"" << str
<< "\" default object created\n"; // FYI
}
StringBad::~StringBad() // necessary destructor
{
cout << "\"" << str << "\" object deleted, "; // FYI
--num_strings; // required
cout << num_strings << " left\n"; // FYI
delete [] str; // required
}
//以下就是在书中源码12.2的基础上,新加的拷贝构造函数。
StringBad::StringBad(const StringBad& st) {
num_strings++;
len = st.len;
str = new char[len + 1];
strcat_s(str, len +1, st.str);
cout << num_strings << ": \"" << str << "\" object created\n";
}
//以下就是在书中源码12.2的基础上,新加的重载函数。
StringBad& StringBad::operator=(const StringBad& st)
{
if (this == &st)return *this;
delete[] str;
len = st.len;
str = new char[len + 1];
strcpy_s(str, len+1 , st.str);
return *this;
}
std::ostream & operator<<(std::ostream & os, const StringBad & st)
{
os << st.str;
return os;
}
//主函数
#include <iostream>
using std::cout;
#include "strngbad.h"
void callme1(StringBad &); // pass by reference
void callme2(StringBad&); // pass by value
int main()
{
using std::endl;
{
cout << "Starting an inner block.\n";
StringBad headline1("Celery Stalks at Midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Leaves Bowl for Dollars");
cout << "headline1: " << headline1 << endl;
cout << "headline2: " << headline2 << endl;
cout << "sports: " << sports << endl;
callme1(headline1);
cout << "headline1: " << headline1 << endl;
callme2(headline2);
cout << "headline2: " << headline2 << endl;
cout << "Initialize one object to another:\n";
StringBad sailor = sports; //代码断点设置到这以后就报错了,所以问题应该在这,但不知道为什么?
cout << "sailor: " << sailor << endl;
StringBad sailor = sports;
//如果将上述的拷贝构造函数换成如下的,用重载= 就没有问题。 但目的是用拷贝方式的初始化,改用这种无意义
/* StringBad sailor;
sailor = sports;
cout << "sailor: " << sailor << endl;
*/
cout << "Assign one object to another:\n";
StringBad knot;
knot = headline1;
cout << "knot: " << knot << endl;
cout << "Exiting the block.\n";
}
cout << "End of main()\n";
// std::cin.get();
return 0;
}
void callme1(StringBad & rsb)
{
cout << "String passed by reference:\n";
cout << " \"" << rsb << "\"\n";
}
void callme2(StringBad &sb)
{
cout << "String passed by value:\n";
cout << " \"" << sb << "\"\n";
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
到底哪里出错了? 谁能解答一下,困扰好几天了