C++实现String类遇到的问题

环境是VS2015,想自己动手实现String类,主要代码如下:
Str.h :

 #ifndef STR_H
#define STR_H

#include <iostream>
using namespace std;

class Str {

private:
    char* m_data;

public:

    friend ostream& operator << (ostream& os, const Str& str);

    char* get_pointer() const {
        return m_data;
    }

    // 构造函数
    Str(const char* cstr = 0);

    // 拷贝构造
    Str(const Str& str);

    // 拷贝赋值
    Str& operator = (const Str& str);

    // 析构函数
    ~Str();
};

#endif // !STR_H

Str.cpp :

 #include "Str.h"
#include <cstring>

Str::Str(const char* cstr) {
    if (cstr) {
        m_data = new char[strlen(cstr) + 1];
        strcpy_s(m_data, 40, cstr);
    }
    else {
        m_data = new char[1];
        *m_data = '\0';
    }
}

Str::Str(const Str& str) {
    m_data = new char[strlen(str.m_data) + 1];
    strcpy_s(m_data, 40, str.m_data);
}

Str::~Str() {
    delete[] m_data;
}

// 拷贝赋值
Str& Str::operator = (const Str& str) {
    if (this == &str) {
        return *this;
    }
    delete[] m_data;
    m_data = new char[strlen(str.m_data) + 1];
    strcpy_s(m_data, 40, str.m_data);
    return *this;
}

ostream& operator << (ostream& os, const Str& str) {
    os << str.m_data;
    return os;
}

没加入 << 重载时,main函数中新建对象没有错误。加入 << 重载后,新建Str对象程序就会崩溃。求各路大神给个解答

运行了下,没毛病哇???

格式是不是有错误,感觉你的格式有问题

1.首先<<的原型和实现是没有问题的, 把你的main程序发出来看看

一般这种问题都是指针错误,能把main函数发出来吗?

一般问题都是出在指针上,能把main函数发出来吗?