作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了这个程序,在来这里之前的最后两个小时里我一直在研究它。我收到错误消息 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_s是 strcpy
的一个版本,具有如 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/
我是一名优秀的程序员,十分优秀!