Reputation: 173
I've already searched through this site and on Google, but I couldn't find anything helping me. I'm a total C++ noob and started coding yesterday, today I've spent couple of hours trying to figure out what I did wrong and I was already able to resolve like 10 errors (which is a lot in such a small code section, I know ._.). What did I do wrong? I do have small Java knowledge and a few scripting languages like LUA and Autoit3.
This is just a small program to test the GPIO pins on my Raspberry PI and to get used to the C++ syntax and to learn a little about programming.
Edit: The error occurrs twice, I've noted that down using comments
#include <wiringPi.h>
#include <stdio.h>
#include <iostream>
using namespace std;
//Declaration of global variables
int delayTime;
//Gets executed once to setup parameters
static int setup() {
delayTime = 750;
}
//Gets executed until ESC is pressed
static int program() {
set(0, 1); //FIRST ERROR
set(5, 0);
delay(delayTime);
set(0, 0);
set(5, 1);
delay(delayTime);
}
//Main function getting called at program start
int main() {
bool running = true;
setup();
puts("Press 'STRG+C' to exit the program. Then run './off'");
if (wiringPiSetup() == -1)
return 1;
pinMode(0, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
set(4, 1); //SECOND ERROR
while (running) {
program(); //Run the code above
}
digitalWriteByte(0);
return 0;
}
//Shortcuts for basic functions
static int set(int pin, int state) {
digitalWrite(pin, state);
return 0;
}
static int get(int pin) {
return digitalRead(pin);
}
Upvotes: 0
Views: 6657
Reputation: 7582
C/C++ compilers are dumb — they proceed top-down. At the time that you call set()
from main()
, the compiler still doesn't know what set()
is yet, because it hasn't seen the definition.
One solution is to write an explicit declaration of set()
before main()
is defined. An easier approach would be to move main()
to the end, as is conventional — precisely because of this issue.
Upvotes: 2
Reputation: 74595
You are calling set()
before it has been declared. Either move the function up in the file, so that it is above the first place it is called, or add a forward declaration like this:
static int set(int pin, int state);
Upvotes: 3
Reputation: 77293
C++ compilation is dependent on order. If you call something that was not yet declared, you get an error. Either move your functions body to a place before it's called, or place a forward declaration there.
Upvotes: 1