-
error C2039
I'm new to C++ and this is example code (only part of the code where I'm recieving the error) from my text book. I'm receiving the follow error:
C:\CSC311\CHAP03\RealEstate\String2.cpp(153) : error C2039: 'strcpy' : is not a member of 'std'
#include "STRING2.h"
#include <cctype>
#include <fstream>
#include <iostream>
#include <cstring>
void StrType::CopyString(StrType& newString)
{
using namespace std;
std::strcpy(newString.letters, letters);}
void StrType::operator=(const StrType& otherString)
{
using namespace std;
std::strcpy(letters, otherString.letters);
}
Can anyone give me a hint or clue as to why or where to look?
Thanks for any help.
Rex
-
Just use strcpy() instead of std::strcpy().
-
Thanks. That worked like a charm!
-
>Can anyone give me a hint or clue as to why or where to look?
Your compiler is too old to realize that all of the non-deprecated C library is in the std namespace. That's why, and you can get around it simply by not qualifying the C functions that you use or to be more portable with compliant compilers, use a using directive:
Code:
using namespace std;
Though that has problems of its own, it's the method I used for some time with VC++ 6.0 because good style can avoid the problems most of the time. ;) Those are two ways to work around the problem. Another is to add any of your C headers to the std namespace:
Code:
namespace std {
#include <cstring>
}
But that's not really the best idea. A slightly better solution would be to declare your functions manually within the std namespace:
Code:
#include <cstring>
namespace std {
char *strcpy(char *dst, const char *src);
}
That's not really the best idea either, because the declarations are easy to get wrong, and it's rather verbose for most tastes, so you should stick to the aforementioned non-portable style or the using directive.