strcpy関数のビルドエラーについて
下記のようにstrcpy関数を使ったプログラムをビルドすると→
「warning C4996: 'strcpy': This function or variable may be unsafe、、、」の様なエラー表示されるので、赤字の部分を追加する
【header.h】
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
using namespace std;
//Canクラスの宣言
class Car{
private:
int num;
double gas;
char* pName;
public:
Car::Car(char *pN, int n, double g);
~Car();
void show();
};
//Canクラスメンバ関数の定義
Car::Car(char *pN, int n, double g)
{
pName = new char[strlen(pN) + 1];
strcpy(pName, pN);
num = n;
gas = g;
cout << pName << "を作成しました。" << endl;
}
Car::~Car()
{
cout << pName << "を破棄します。" << endl;
delete[] pName;
}
void Car::show()
{
cout << "車のナンバーは" << num << "です。" << endl;
cout << "ガソリン量は" << gas << "です。" << endl;
cout << "名前は" << pName << "です。" << endl;
}
【main.cpp】
#include"Header.h"
//Carクラスの利用
int main()
{
Car car1("mycar", 1234, 25.5);
car1.show();
return 0;
}
※コメント投稿者のブログIDはブログ作成者のみに通知されます