C++编程问题请大神赐教谢谢 5C

问一下 strcpy()那行为什么会报错
#include"stdafx.h"
#include
#include"strngbad.h"
using std::cout;

int StringBad::num_strings = 0;

StringBad::StringBad(const char*s)
{
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
cout << num_strings << ":\"" << str << "\"object created\n";

}

StringBad::StringBad()
{
len = 4;
str = new char[4];
std::strcpy(str, "C++");
num_strings++;
cout << num_strings << ":\"" << str << "\"default object created\n";
}

StringBad::~StringBad()
{
cout << "\"" << str << "\"object deleted,";
--num_strings;
cout << num_strings << "left\n";
delete[]str;
}

std::ostream & operator<<(std::ostream & os, const StringBad & st)
{
os << st.str;
return os;
}

2个回答

仅仅就可以通过编译的角度来说,

 #include<iostream>
#include<string>
using std::cout;

#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
private:
char*str;
int len;
static int num_strings;
public:
StringBad(const char*s);
StringBad();
~StringBad();
friend std::ostream & operator<<(std::ostream & os, const StringBad &st);
};

#endif



int StringBad::num_strings = 0;

StringBad::StringBad(const char*s)
{
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    num_strings++;
    cout << num_strings << ":\"" << str << "\"object created\n";  
}

StringBad::StringBad()
{
    len = 4;
    str = new char[4];
    std::strcpy(str, "C++");
    num_strings++;
    cout << num_strings << ":\"" << str << "\"default object created\n";
}

StringBad::~StringBad()
{
    cout << "\"" << str << "\"object deleted,";
    --num_strings;
    cout << num_strings << "left\n";
    delete[]str;
}

std::ostream & operator<<(std::ostream & os, const StringBad & st)
{
    os << st.str;
    return os;
}

int main()
{}

你没有main函数

如果strcpy还有报错,是因为你用了vc++ 2012或者更新的版本,默认不允许不安全的函数。

可以使用strcpy_s代替,修改如下:

 #include "stdafx.h"

#include<iostream>
#include<string>
using std::cout;

#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
private:
char*str;
int len;
static int num_strings;
public:
StringBad(const char*s);
StringBad();
~StringBad();
friend std::ostream & operator<<(std::ostream & os, const StringBad &st);
};

#endif



int StringBad::num_strings = 0;

StringBad::StringBad(const char*s)
{
    len = std::strlen(s);
    str = new char[len + 1];
    strcpy_s(str, len, s);
    num_strings++;
    cout << num_strings << ":\"" << str << "\"object created\n";  
}

StringBad::StringBad()
{
    len = 4;
    str = new char[4];
    strcpy_s(str,len, "C++");
    num_strings++;
    cout << num_strings << ":\"" << str << "\"default object created\n";
}

StringBad::~StringBad()
{
    cout << "\"" << str << "\"object deleted,";
    --num_strings;
    cout << num_strings << "left\n";
    delete[]str;
}

std::ostream & operator<<(std::ostream & os, const StringBad & st)
{
    os << st.str;
    return os;
}

int main()
{}

补充一楼,还有一种不推荐的方式 项目->属性->c/c++->SDL检查改为否

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问