Ok, so I am trying to fix my C++ assignment, but when I use strcpy_s it only works for my array and not my *pointer.. here is what I am working with:
HotelRoom::HotelRoom(char Num[], int cap, double daily, char* name, int Stat)
{
strcpy_s(room_Num, Num); //copy first argument into room_Num[]
guest = new char[strlen(name) +1]; //create space for the name
strcpy_s(guest, name); //copy second argument into new space
capacity = cap;
dailyRate = daily;
occupancyStat = Stat;
}
This is the error I get when using it in this manner strcpy_s(guest, name); :
"no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char*, char*)".
The non-standard strcpy_s
takes one extra parameter than std::strcpy
that is the max size you want to copy.
errno_t strcpy_s(char *s1, size_t s1max, const char *s2);
What you need is the standard C function std::strcpy
.
char *strcpy(char *s1, const char *s2);
Take a look at the documentation: http://msdn.microsoft.com/en-us/library/td1esda9%28v=vs.90%29.aspx
When the size cannot be determined automatically because a statically sized array is not being passed you must provide it.
#include <string.h>
int main()
{
char src[] = "Hello World!\n";
char staticDest[100];
size_t dynamicSize = strlen(src) + 1;
char* dynamicDest = new char[dynamicSize];
//Use the overload that can determine the size automatically
//because the array size is fixed
//template <size_t size> errno_t strcpy_s(char(&strDestination)[size], const char *strSource);
strcpy_s(staticDest, src);
//Use the overload that requires an additional size parameter because
//the memory is dynamically allocated
//errno_t strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
strcpy_s(dynamicDest, dynamicSize, src);
return 0;
}
以下应该有效:
strcpy_s(guest, strlen(name), name); //copy second argument into new space
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.