I want to call my function with string parameter - c++

void GenerateDecryptedData("/home/merve/merve.enc", "/home/merve/merve.dec","dEneMe!1234");
I want to call my function like dEneMe!1234.
void GenerateDecryptedData(const char* pathToEncryptedFile,
const char* pathToDeccryptedFile,
std::string Pwd);
But when I wrote the function prototype like this, I'm taking string has not been declared- error! How can I take my password in string type?

You need the string header at the top of your source file: #include <string>. I'd consider Googling error messages more often. :)
string header - http://www.cplusplus.com/reference/string/
cstring header - http://www.cplusplus.com/reference/cstring/

What's wrong with:
void GenerateDecryptedData(const char* pathToEncryptedFile, const char* pathToDeccryptedFile, const char* Pwd);

Related

Function signature doesn't match called function, difference between const char[] and char*?

I am using PubSubClient.h, as defined here: https://pubsubclient.knolleary.net/api.html
The instance is created as the following:
WiFiClientSecure secureCl;
PubSubClient mqttCl(secureCl);
PubSubClient has method called setCallBack, which takes in the parameter callback.
PubSubClient setCallback (callback)
callback : a pointer to a message callback function called when a message arrives for a subscription created by this client.
The following signature is given for function callback:
void callback(const char[] topic, byte* payload, unsigned int length)
But, within the example code I were given, inside setup the callback function used is the receivedCallback function:
mqttCl.setCallback(receivedCallback); //called inside setup()
And the function is given as:
void receivedCallback(char* topic, byte* payload, unsigned int length) {
//some code here
}
Now, I can see that it has char* topic, instead of const char[] and I can't get my head around how it is not const char[], as defined by the signature. Could someone explain in simple terms why is it like that?
It seems a bug, in this context the problem is given by the const attribute e not by the [] or the *. In fact const char[] and const char* are equal, also char[] and char* are equal, but const char[] and char* can create something that is incompatible.
The difference is in the missing const of your callback.
Basically char* topic as an argument means that you are allowed to manipulate the content of topic as in topic[0] = 'A';.
On the other side const char* or const char[] guarantees that there will not be a change to the underlying string and so a pointer to an string from the read-only memory could be given.
mqttCl could for example do the following:
byte payload[10];
callback("test-topic", payload, 10);
And your recievedCallback is allowed to do:
topic[0] = 'b';
The compiler is allowed to store "test-topic" in the read-only memory of the process in which case the program would crash. To prevent this the const part in type signatures must match.

Qt "tcpserver->write(string)"

i have a very simple question here, How can i send a string with
tcpserver->write(string);
I tried:
tcpserver->write("string")
and it works, but if i want to input a string in there, i get a "no matching function to call to 'QtcpSocket::write(QString)'"
error,
so i tried converting the string to "data" and then send it, but i got a ton of errors...
And my question is: How can i easly send a string thru my tcpserver?
(I should also mention, that i am very new to programming)
You need to convert string to QByteArray, for example:
tcpserver->write(string.toLocal8Bit());
tcpserver->write(string.toUtf8());
Try tcpserver->write((const char *)string.data(), string.length()*sizeof(QChar));
QTcpSocket has 3 overloads for write () function
qint64 write (const char *data);
qint64 write (const char *data, qint64 len);
qint64 write (const QByteArray &data);
So Convert QString to any of them. Just try
tcpserver->write (string.toLatin1 ());

Best way to design this code?

I have a log function which takes a parameter, which prints out the name of some HW
logEvent("LOG THIS HW select = %s", NAME[selection]);
To determine what to print I have:
const char* NAME[] =
{
"A"
"B"
}
This was in the header but then I got multiple implementations problems, I want this to be accessed by many files. How can I put this kind of data in a header?
You put a declaration in the header:
extern const char* NAME[];
and put the definition in one cpp file:
const char* NAME[] = {"A", "B"};
Adjust logEvent so selection is passed as a parameter. You can then keep the string table local to that function.

Qt 5: const char * QString cast

In Qt 4 it is possible to automatically cast a QString to a "const char *", e.g. I could pass a QString to a function that expected a "const char *".
void myFunction(const char *parameter);
QString myString;
myFunction(myString); //works in QT4
In Qt 5 I would however get an "error C2440: 'type cast' : cannot convert from 'QString' to 'const char *'" (that is the Visual C++ 2008 compiler, other compilers would throw something similar). If I understand the documentation correctly, it is because the Qt 3 compatibility layer is not included anymore in QT5.
Of course, I could change the function call from
myFunction(myString);
to
myFunction(myString.toLatin1().data());
However, I have a huge code base that compiles fine with Qt 4 and I would really like to have my old code compile with Qt 5 without modifying it. Is there any way to achieve this?
You could create a macro or inline function for your purpose to minimize the changes, but since that would also require a grep alike operation, there is not much difference.
#define QString2charp(myString) myString.toLatin1().data()
or
inline char* QString2charp (const QString &myString)
{
return myString.toLatin1().data();
}
and then:
myFunction(QString2charp(myString));
BUT
of course, in an ideal world, it would be nice if your "myFunction" could get an overload expecting a QString argument.
void myFunction(const char *parameter);
void myFunction(QString parameter);
and then the overload would be implemented like this:
void myFunction(const QString &parameter)
{
myFunction(myString.toLatin1().data());
}
of course, this would require the constructor being explicit so that no implicit conversion can happen, otherwise the compiler will complain about ambiguity in presence of both when trying to pass const char*, but if you always use QString, it should just work with Qt 5.
This is somewhat equal to rewriting the original function to a different signature expecting QString though because unfortunately the corresponding constructor is not explicit. I imagine that was not changed in Qt 5 for compatibility as it would have broken too much code.
As you can see, you do not need to change anything in your code this way, other than adding a one-liner overload. It is neat, isn't it?
You could either
Change the signature of your function to MyFunction( const QString & parameter ) (of course that works only if you don't need the one that takes char * any more)
Overload MyFunction:
void myFunction(const char *parameter); //this is your normal function
void myFunction( const QString & parameter )
{
char * c_str = nullptr;
<convert parameter to char * in any way that suits you>
myFunction( c_str );
}
If the above is impossible for some reason, the best approach is probably to write a similar wrapper:
void MyFunctionWrapper( const QString & parameter )
{
<same as above>
}
It is no longer possible to cast QString to const char* in >= Qt 5, because QString is UTF-16 and no longer has an entry for caching the result of converting to ASCII.
The way to achieve your aim is
a) Add overloads to the functions expecting const char* which can handle QStrings,
b) Add the explicit conversion code to the call-site, or
c) Go to some char-based string, like std::string.
None of those fulfill your aim of nearly no changes though.
I found this on QT forum
const QByteArray byteArray = mytext = textBox.text().toUtf8();
const char *mytext = byteArray.constData();
This resolved my issue

Workaround for ambiguous call to overloaded function

void printOutput(std::string text);
void printOutput(std::string& text);
Both functions print some text out to the console, but I wanted to handle each case where:
std::string testOutput = "asdf";
output->printOutput(testOutput); // Gives the error as it can use either function
In some cases I may want to:
output->printOutput("asdf"); // Only the first function can be used
Rather new to all this, is there a way I can handle this?
Pass by const reference:
void printOutput(const std::string &text);
Both forms can bind to that, and you shouldn't have to modify what you print.
Unless you're planning to modify the string passed in by reference, a single
void printOutput(std::string const& text);
will work.
Or are you hoping to do something different in each version?