gpt4 book ai didi

c++ - 不明白为什么我会收到错误消息

转载 作者:行者123 更新时间:2023-11-30 04:11:44 25 4
gpt4 key购买 nike

我编写了这个程序,在来这里之前的最后两个小时里我一直在研究它。我收到错误消息 Function or variable unsafe consider using strcpy_s 我的书没有提到 strcpy_s。也许新的眼睛会看到我看不到的东西。这是我写的代码

  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>


using namespace std;

class HotelRoom
{
private:
char room_no [3];
char* guest;
int capacity;
int occupancy;
double rate;
public:
HotelRoom(char room[],int, double = 89.00, char* n_p = 0, int= 0);
~HotelRoom();
void Display_get_number();
void Display_guest();
int get_capacity();
int get_status();
double get_rate();
void change_rate(double);
bool change_status(int);

};
HotelRoom::HotelRoom (char room[], int cap, double rt, char*n_p, int occup)
{
strcpy(room_no, room);
guest = new char[strlen(n_p) + 1];
strcpy(guest, n_p);
capacity = cap;
occupancy = occup;
rate = rt;
cout << " defaut for the following " << room << endl << endl;
cout << " Capacity " << capacity << endl << endl;
cout << " rate " << rate << endl << endl;
cout << " Room Guest " << guest << endl << endl;
cout << " Occupancy " << occupancy << endl << endl;

}
HotelRoom::~HotelRoom()
{
cout << "\nHotelRoom " << room_no <<" terminated. ";
delete [] guest;
}
void HotelRoom::Display_get_number()
{
cout << room_no;
}
void HotelRoom::Display_guest()
{
cout << guest;
}
int HotelRoom::get_capacity()
{
return capacity;
}
int HotelRoom::get_status()
{

return occupancy;
}
double HotelRoom::get_rate()
{
return rate;
}

void HotelRoom::change_rate( double amount)
{
rate += amount;
}
bool HotelRoom::change_status(int occupancy)
{
if (occupancy <= capacity )
{
this-> occupancy = occupancy;
return true;
}
else
return false;
}



int _tmain(int argc, _TCHAR* argv[])

{

cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);

HotelRoom guest ("134",4,0, "Dennard Beale", 0);


cout << endl;
cout << " *****End of program***** " << endl;

system("pause");
return 0;

}

最佳答案

strcpy_sstrcpy 的一个版本,具有如 Security Enhancements in the CRT 中所述的安全增强功能.

strcpy 是“不安全的”,因为它会导致 Buffer Overflows .恶意用户可以 exploit this来控制你的程序。因此,Microsoft 引入了 strcpy_s(读作“字符串复制安全”),使 strcpy 弃用。您应该在 Visual Studio 中使用 strcpy_s 而不是 strcpy

或者,使用 std::string

关于c++ - 不明白为什么我会收到错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20114403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com