Error: Cannot add two pointers
It gives the error in the title about this piece of code:
string DDateTime::date2OracleDate(DATE Date)
{
string s;
s="TO_DATE('" + DateFormat("%d/%m/%Y",Date) + "','dd/MM/YYYY')";
return s;
}
I don't understand how that is possible, no pointers involved....
EDIT:
string DDateTime::Dat开发者_运维知识库eFormat(string sFormat,DATE Date)
{
struct tm tmTemp;
RipOf_AfxTmFromOleDate(Date, tmTemp);
RipOf_AfxTmConvertToStandardFormat(tmTemp);
char sFormatted[MAX_TIME_BUFFER_SIZE];
strftime(sFormatted, MAX_TIME_BUFFER_SIZE, sFormat.c_str(), &tmTemp);
return sFormatted;
}
The following should work better:
string DDateTime::date2OracleDate(DATE Date)
{
string s = "TO_DATE('";
s += DateFormat("%d/%m/%Y",Date);
s += "','dd/MM/YYYY')";
return s;
}
The three strings you're trying to add are C-style strings; each is a pointer to the contents of the string. At least, I'm assuming that DataFormat
returns a C-style string; it's not a standard function, so I don't know what it does.
In C++ you can't simply "add" two of these together to get a C++ string, since there's no way of knowing whether a char*
is actually a string, or a pointer to some arbitrary data. You have to convert one of them into a std::string
, then you can "add" C-style strings to that.
string s = "TO_DATE(";
s += DateFormat(whatever);
s += "','dd/MM/YYYY')";
or
string s = string("TO_DATE(") + DateFormat(whatever) + "','dd/MM/YYYY')";
Your literal strings are char*
. I am not sure what your string
is, but replacing each quoted string with a constructed string
should do it.
s=string("TO_DATE('") + DateFormat("%d/%m/%Y",Date) + string("','dd/MM/YYYY')");
The probelm is only coming from the return type of DateFormat being char* - std::strings can normally be concatenated with string literals without any problem.
E.g. the following works:
std::string a, b;
b = "foo";
a = "literal" + b + "literal";
but the following will not work:
std::string a, b;
b = "foo";
a = "literal" + b.c_str() + "literal";
The only change required should be to convert the result from DateFormat to a std::string.
s="TO_DATE('" + std::string(DateFormat("%d/%m/%Y",Date)) + "','dd/MM/YYYY')";
Should do the trick.
Edit 1
Seeing that DateFormat returns string there should have been no problems concatenating literal + std::string + literal. So I'm stumped.
Edit 2
Following up after Amardeep's noticing that DateFormat actually returns a char* despite the signature suggesting otherwise (nice catch). I tested and it indeed appears no errors or warnings are raised, even compiling with -Wall and -Wextra on gcc, the function behaves as if it were simply defiend char*.
So my solution should still work for the initial reasons. (and there is no actual problem with concatenating string literals with std::string, the problem is only char* with string literals).
It would probably be a neater solution to fix the root of the problem - the bad return type of DateFormat by converting sFormatted to a std::string.
return std::string(sFormatted);
s="TO_DATE('" + DateFormat("%d/%m/%Y",Date) + "','dd/MM/YYYY')";
means (to the compiler) that you are adding a const char*
pointer to something else (possibly a char*
?) returned by / converted from DateFormat
, and then add to it another const char*
pointer.
Try this to force your compiler to find the correct string
overloads for operator+
:
s=string("TO_DATE('") + DateFormat("%d/%m/%Y",Date) + "','dd/MM/YYYY')";
精彩评论