I have overloaded function
int put_message(int level, int sys_log_level, const char * inp_message);
int put_message(int level, int sys_log_level, const std::string &inp_message);
and call this function
put_message(0, LOG_ERR, "clock_gettime error");
Code is compiled and works
but Eclipse CDT Code analyzer says
Invalid arguments '
Candidates are:
int put_message(int, int, const char *)
int put_message(int, int, const ? &)
'
How can I fix this the error?
Update:
After modifying LOG_ERR to int(LOG_ERR) error disappears.
I have not add the in the header.
Adding solves the problem.
you are missing #include <string> or something related to string class
You need to cast the string to the correct type, the compiler treats "some string" as char[] so you need to cast it to const char*
Try with
put_message(0, LOG_ERR, (const char*)"clock_gettime error");
Related
I am trying to compile ustl C++ library with MinGW GCC compiler on Windows 10.
The 41st line in the C++ header below was able to solve thanks to this answer, but there is a problem at line 43. How should I solve it?
https://github.com/msharov/ustl/blob/master/fstream.h#L41
// int ioctl (const char* rname, int request, long argument = 0); // orig
int ioctl (const char* rname, int request, long argument = 0u);
inline int ioctl (const char* rname, int request, int argument) { return fstream::ioctl (rname, request, long(argument)); }
inline int ioctl (const char* rname, int request, void* argument) { return fstream::ioctl (rname, request, intptr_t(argument)); }
MinGW GCC compiler (10.3.0 (Rev5, Built by MSYS2 project)) error message:
fstream.h:43:132: error: call of overloaded 'ioctl(const char*&, int&, uintptr_t)' is ambiguous
43 | inline int ioctl (const char* rname, int request, void* argument) { return fstream::ioctl (rname, request, uintptr_t(argument)); }
--
Edit: Thanks for the hint.
With another answer, below code works for me:
https://github.com/msharov/ustl/blob/master/fstream.h#L43
// ... intptr_t(argument)); // orig
*((long*)(argument)));
You can find in the last few words that
fstream.h:43:132: error: call of overloaded 'ioctl(const char*&, int&, uintptr_t)' is ambiguous 43 | inline int ioctl (const char* rname, int request, void* argument) { return fstream::ioctl (rname, request, uintptr_t(argument)); }
intptr_t is actually a uintptr_t in your environment. And as for uintptr_t, you can find in cppreference that it's an unsigned integer.
According to ranking of implicit conversion sequence in overload resolution, unsigned sth => long and unsigned sth => int are equivalent(both conversion). So no winner, all loser, which leads to compilation error.
If you just want to avoid compilation error, convert intptr_t to long helps. But I think the best way is to use proper type that stands for its meaning. For example, if it's for an address, then just use uintptr_t in the beginning, and make it a convention.
I can run my program in codeblock or visual studio 2015 but it doesn't work in visual studio 2017
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void replacechar(char *filenguon, char ktc, char ktm)
{
fstream fs(filenguon, ios::in | ios::out);
if (!fs)
cout << "khong the tim thay" << endl;
else
{
char ch;
while (fs.get(ch))
{
if (ch == ktc)
{
int pos = fs.tellg();
pos--;
fs.seekp(pos);
fs.put(ktm);
fs.seekg(pos + 1);
}
}
}
}
int main()
{
replacechar("caua.txt", 'r', 'R');
return 0;
}
Error:
Error C2664 'void replacechar(char *,char,char)': cannot convert argument 1 from 'const char [9]' to 'char *'
Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "char *"
Warning C4244 'initializing': conversion from 'std::streamoff' to 'int', possible loss of data
I can run my program in codeblock or visual studio 2015 but it doesn't work in visual studio 2017
Change
void replacechar(char *filenguon, char ktc, char ktm)
to
void replacechar(const char *filenguon, char ktc, char ktm)
The rules about string literals changed in C++11 (I think). They are const data and so any function parameter to which you pass a string literal should be declared with const.
And, as stated in the comments, change
int pos = fs.tellg();
to
auto pos = fs.tellg();
The return from tellg is not an int, by using auto you are asking the compiler to use the correct type, whatever that is.
You are not allowed to pass a const char* (in your case the string literal "caua.txt" to a function accepting a non-const char*.
Change your signature to void replacechar(const char *filenguon, char ktc, char ktm).
Two methods:
1.
void replacechar(const char *filenguon, char ktc, char ktm)
{
//TODO
}
2.
char str[]={"caua.txt";};
replacechar(str, 'r', 'R');
that should be work, "caua.txt" is const char*,it change to char* by copy one by one or const_cast<char*>
I am trying to create a user defined literal but get an error message when using it.
GCC says
unable to find numeric literal operator ‘operator""_uint’
while clang tells me
error: no matching literal operator for call to 'operator""_uint' with argument of type 'unsigned long long' or 'const char *', and no matching literal operator template
I reduced the code to following MCVE:
#include <cinttypes>
unsigned int operator"" _uint(char const *, std::size_t) { return 0; }
int main() {
return 1_uint;
}
Which gives the mentioned error as you can see on ideone.
As you can read in detail on cppreference.com there is a multitude of different versions for literal operators.
The one in the OP is used exclusively for string literals and won't be available for integers. Instead the "fallback" version for integers which expects only a const char* without a second std::size_t parameter:
#include <cinttypes>
unsigned int operator"" _uint(char const *) { return 0; }
int main() {
return 1_uint;
}
Which works on ideone.
When compiling the below code, it gives me a warning, namely
deprecated conversion from string constant to 'char*'.
In what ways is it possible to remove the message (without explicitly suppressing the warning)?
I tried casting with (const char*), but to no avail.
#include <windows.h>
int main() {
typedef int * (*MyDownloadToUrl)(void*, char*, char*, DWORD, void*);
HINSTANCE LibHnd = LoadLibrary("Urlmon.dll");
MyDownloadToUrl MyDownloadFunction = (MyDownloadToUrl)GetProcAddress(LibHnd,"URLDownloadToFileA");
MyDownloadFunction(0, "http://MyWebsite.com", "Webpage.htm", 0, NULL);
}
You need to const_cast<char*>("my string literal") to get rid of the warning. In C++03 implicit conversion from a string literal (which is a const char*) to char* is deprecated. In C++11 such an implicit conversion is an error.
In this case though, URLDownloadToFile takes arguments of type LPCTSTR, which is defined as either const wchar_t* or const char* depending on the UNICODE prepossessor directive.
I am trying to compile some code that was given to me that I'm told compiles fine. Perhaps on a different compiler. I am using VS2010 and I have the following line:
char *dot = strrchr(filename, '.');
This causes the compiler error:
"error C2440: 'initializing': cannot convert from 'const char *' to
'char *'
How come? And how do I fix it?
The error message is pretty clear. strrchr returns a const char*. So you need:
const char *dot = strrchr(filename, '.');
If you really need a char*, you can use strcpy for conversion.
C++ has saner versions of strchr and strrchr than C thanks to overloading, so say:
const char * dot = strrchr(filename, '.');
In C, which has no overloading, you only have a single function char * strrchar(const char *, const char *), and it's up to you to decide whether the result is constant or mutable, depending on which type of pointer to feed into the function. C has many such type-unsafe functions.