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.
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 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.
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 7 years ago.
Improve this question
I am working on an object-oriented C++ coursework where I need to return error codes from the main function. How would one do this properly?
Unfortunately this is an assessed coursework so I cannot post my code here. But let's say the case is as follows:
I'm building an enigma machine with classes Plugboard, Reflector, and Rotor. I pass each of the configuration files as arguments in the command line. In this task, I'm provided with a file errors.h containing the following:
#define INSUFFICIENT_NUMBER_OF_PARAMETERS 1
#define INVALID_INPUT_CHARACTER 2
#define INVALID_INDEX 3
// and so on...
So I have in my program several functions to check the errors, for example a function to check whether the configuration file contains an invalid character (it has to be 0 to 25). I was thinking of setting this as a boolean function and then in my main function have the following:
if (!plugboard.check_invalid_character(/*some arguments*/)) {
cerr << "Invalid character!" << endl;
return 2;
}
But I'm not completely sure this is the right way to do it? Is it too superficial? Is there a more elegant way of returning error?
I hope my question is a little clearer this time. Thanks before.
You just need to return the value 4 in your main method like this:
int main() {
return 4;
}
Please note that your main function could also have the arguments vector and the argument count so there could be more in the brackets.
If KLibby is right and you use a method with returns the value you need to use something like that:
int doSomething() {
return 4;
}
int main() {
return doSomething();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do you call a function from a source file from a header file?
//h.h
extern string pic;
class takePic
{
public:
void warPic();
void artPic();
void fatePic();
void painPic();
void noPic();
};
// second part of the same header where it calls the function
takePic picture;
void pictureType()
{
if (pic == "war")
{
picture.warPic();
}
else if (pic == "fate")
{
picture.fatePic();
}
else if (pic == "pain")
{
picture.painPic();
}
else if (pic == "art")
{
picture.artPic();
}
else
{
picture.noPic();
}
}
When I do this it says that the linker is not working.
This is the error linker command failed with exit code 1.
What happens if you change
void pictureType()
to
inline void pictureType()
You should really tell us the whole error message, and perhaps try searching for that before asking a question.
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/