It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have little knowlage in C++
so I have this code
bool is_successful = true;
ex_file_licensing exFileLicence;
std::string flexLMfilePath;
flexLMfilePath.append("C:/Desktop/QA-program/testsuite/tmp/");
std::string Message = exFileLicence.checkLicense(DI_MF,flexfilePath,is_successful);
and I was asked to move it outside the main and then call it in the main
and now I have no idea what to do
Could you please tell me what are the steps that I should follow
please be as specific as possible, I'm really bad at this thing
Thanks
You must create a function and call that function inside main:
void foo(); //this is called a function prototype
main()
{
...
foo() //your function in place of that code
}
void foo()
{
...//the code originally in main. This is called your function definition
}
this is how creating functions works and is basically how any code in c++ is written. Sometimes the functions appear in files outside the main file but its basically the same.
Check out C++ Functions. I'm assuming you have something as follows.
int main(){
//***your stuff
return
You need the following.
void function(){
//**your stuff
return;
}
int main(){
function();
return;
}
When the program starts it will automatically go to main and when it reaches the call:
function();
It will pass control to the code wrapped within
void function(){
return;
}
If I understand correctly I think you just need to put the code in a function, like this:
void CodeFunction()
{
bool is_successful = true;
ex_file_licensing exFileLicence;
std::string flexLMfilePath;
flexLMfilePath.append("C:/Desktop/QA-program/testsuite/tmp/");
std::string Message = exFileLicence.checkLicense(DI_MF,flexfilePath,is_successful);
}
and you can then call it from main by using CodeFunction().
Remember to put this above the main function, or if it's below declare it above main using
void CodeFunction();
Hope this helps.
You need to write a function, move the code to the function and then call the function from main - http://www.cplusplus.com/doc/tutorial/functions/
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 19 days ago.
This post was edited and submitted for review 18 days ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
Have function:
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
// ...
}
Need to use in:
BT.register_callback(btCallback);
Compiler error:
no known conversion for argument 1 from 'void(esp_spp_cb_event_t, esp_spp_cb_param_t*)' to 'void (**)(esp_spp_cb_event_t, esp_spp_cb_param_t*)'
As I understand it, he needs a pointer to function pointer. I don't know how to create it. I tried a function pointer (through &), does not fit.
Reproduction (PlatformIO / platform: espressif32, board: esp-wrover-kit, framework: arduino):
#include <Arduino.h>
#include <BluetoothSerial.h>
BluetoothSerial BT;
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
Serial.println("TEST");
}
void setup() {
Serial.begin(115200);
BT.begin("", true);
BT.register_callback(btCallback);
BT.connect("TEST");
}
void loop() { }
P.S. Is arduino-esp32 BluetoothSerial::register_callback function.
You need to make a pointer variable, and then take a pointer from it using the & operator.
void f()
{
// ...
}
void g(void (**p)())
{
// ...
}
int main()
{
void (*f_ptr)() = f;
g(&f_ptr);
}
Try if here.
As I understand it, he needs a pointer to function pointer. I don't
know how to create it. I tried a function pointer (through &), does
not fit.
In the previous example, taking &f doesn't have any effect. These two lines are equivalent!:
void (*f_ptr)() = f;
void (*f_ptr)() = &f;
Therefore, if you were doing:
g(&f);
you are actually passing a simple function pointer, not a pointer to function pointer.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First of all, I'm gonna say thank you to who help me
so recently I was working on a project which is called Green House in Arduino
then I was about to write a function that reads sensor, and a function that prints that value in function1 and I just came up with some problems,
Here is My code
First of all, I just defined every pin and then
written functions
and this is functions and the main code which has problems
void GetState();
void loop() {
// put your main code here, to run repeatedly:
GetState();
PrintState();
delay(2000);
}
void PrintState()
{
Serial.println("TEMP ");
Serial.println(temp);
Serial.println("Rotobate Khak");
Serial.println(soilstate);
Serial.println("Humidity");
Serial.println(hum);
Serial.println("LDR === ");
Serial.println(LDRSTATE);
Serial.print("\n");
}
void GetState()
{
DHT.read11(Sensor);
int LDRSTATE=analogRead(LDR);
return LDRSTATE;
int soilstate=analogRead(soil);
soilstate= map(soilstate,0,1023,100.00,0);
return soilstate;
int temp=DHT.temperature;
return temp;
int hum=DHT.humidity;
return hum;
}
and I get 'temp' was not declared in this scope error
Declare "int temp;" at the top. The same for all other variables. Do not declare the variables in GetState, just use them.
Remove all "return" lines from Getstate(). Just setting "temp=DHT.temperature;" sets the variable and is enough.
Put the GetState() function before loop() (where it is called). Or, if you prefer, you can add a prototype before loop():
void GetState();
void loop() {
...
Think you are quite new to programming. Please try giving prototypes of GetState() and PrintState() functions above their definition. This is because C/C++ compiler assumes that it returns int by default if there isn't any prototype.
Otherwise, you can create a header file and then include that header file in this program.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a XML-file holding among other things some groups with name and userlists. In my code in constructor I have set a dictionary for this list:
dictGroups= QMap<QString, QList<QString>>() ;
In headerfile it is declared as
public:
QMap<QString, QList<QString>> dictGroups;
Then I read the file: ReadConfig();
void AppConfig::ReadConfig(void)
{
...
while(!reader.atEnd())
{
ReadGroups(reader);
if (dictGroups.isEmpty()) qDebug()<<"ReadConfig_isEmpty";
}
...
This is my ReadGroups:
void AppConfig::ReadGroups(QXmlStreamReader &reader)
{
dictGroups.clear();
while(!reader.atEnd())
{
reader.readNext();
if (reader.error())
{
...
}
else
{
if (reader.isStartElement())
{
if (reader.name().toString().toLower()=="group"){
ReadGroup(reader);
if (dictGroups.isEmpty()) qDebug()<<"ReadGroups_isEmpty";
}
}
else if (reader.isEndElement())
{
if (reader.name().toString().toLower() == "groups")
{
if(dictGroups.count()<=0){
QList<QString> users= QList<QString>();
users.append(this->GetUsername());
dictGroups.insert("admin", users);
}
return;
}
}
}
}
}
My problem is, that the items inserted in dictGroups while ReadGroups get lost. I get the debug output
ReadConfig_isEmpty
but in ReadGroups seems everything is ok.
I'm at a loss, puzzling around for hours, can anybody help to find the reason?
You have this code:
dictGroups.clear();
Why do you expect the dictGroups to persist when you clear them on every iteration of the outer loop? Don't do that.
The clear statement belongs perhaps at the beginning of ReadConfig.
Your method name capitalizations are very much out of place in Qt code, though: capitalized names are by convention reserved for groups.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a class that will provide a function with an item seen which will return false the first time it's seen with a certain string, but true every time after that when the same string is called.
class Tracking
{
...
public:
bool itemseen(const char* str)
{
..
}
};
It sounds like you need/want either an std::set<std::string> or an std::unordered_set<std::string>.
When you receive an item, attempt to insert it into the [unordered_]set. Check the return value to see if that succeeded or not.
Note that searching for the item first, then attempting to insert if it's not present is fairly wasteful. You normally just want to attempt to insert it, and then check the return value to see if that succeeded:
class whatever {
std::set<std::string> strings;
public:
bool itemseen(std::string const &input) {
return !strings.insert(input).second;
}
};
If you do a search and then insert, you're forcing it to search the collection twice when/if it inserts a new object. Using the return value instead allows you to do the search only once. IOW, you can expect it to be roughly twice as fast (though caching is likely to make the second search faster, so the measured difference may be smaller than that).
Simplest way is by using STL:
#include <set>
#include <string>
std::set<std::string> seen;
bool itemseen(const char* str)
{
if (seen.find(str) == seen.end())
{
seen.insert(str);
return false;
}
return true;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
There is in Qt such functionality: QMetaObject::invokeMethod(QObject * obj, const char * member, ...), which invokes method from string (member). How can I do the same (or similar)? Thanks)
You can't do this in pure c++, but you can read about reflection/introspect in c++ in this question
A similar thing can be achieved with a map<std::string, std::function<void()>>:
std::map<std::string, std::function<void()>> funs;
funs["hello world"] = [] () { std::cout << "hello world"; };
funs["hello world"]();
The question is how similar do you want it? How "native" should the call look like?. You can do things like:
void foobar(int, float);
...
invoke("foobar", 5, 5.f);
but the implementation looks hacky and is non-trivial.
There is a related problem:
You can get pretty far with variadic templates and some template/virtual techniques. With the following codes, you'll be able to do something like:
std::string select_string (bool cond, std::string a, std::string b) {
return cond ? a : b;
}
int main () {
Registry reg;
reg.set ("select_it", select_string);
reg.invoke ("select_it", "1 John Wayne"));
reg.invoke ("select_it", "0 John Wayne"));
}
i.e. where the argument list is dissected into a real argument list for a native function call. With variadic templates you can do a lot of things; I am not sure if you should.