While coding opcontrol, I defined all my motors and sensors just before opcontrol function. When I went to the autonomous file to code auton, I realized that I can’t code the motors in and I must redefine them all. For autonomous and opcontrol, is there a way to define all motors and sensors “globally” or must I redefine them in each folder?
there is no way. the sensors and motors are just like regular variables and cant be automatically declared in all the cpp files.
You can:
put the autonomous and opcontrol function in the same cpp file so you dont need to declare it multiple times
OR
Use header files like this:
globals.cpp
pros::Motor myMotor(1);
globals.h
extern pros::Motor myMotor;
opcontrol.cpp
#include "globals.h"
void opcontrol(){
}
autonomous.cpp
#include "globals.h"
void autonomous(){
}
simply saying, you can use all the motors by including the globals.h file
This is some basic c++ knowledge to learn about before using pros
So I took your advice and created the new files, but it’s telling me the motors and such don’t exist? and I don’t know what I did wrong. Could you or someone reading this who knows the problem help?
can u copy and paste the error? can you also send out globals.cpp
I believe that the issue lies in
using namespace pros;
and
extern pros::Controller Controller1;
The linker is not recognizing where Controller1 as it doesn’t know which object you are referencing since you are using namespace pros
.
It’s basically the same issue as doing
using namespace std;
and then doing
std::cout << "hi";
You need to either remove all instances of pros::
or remove the using namespace pros
which I would recommend the latter as it’s bad practice anyway
no. you can overwrite namespaces, so even if you are :using namespace std;
you can still use pros::Motor or something like that. the compiler wont solely add in the namespace, but will try to find.the matching function first.
No, what @Arman_R said is completely wrong (sorry). It is fine practice to do using
on a namespace you use a lot (except for std
because of potential naming conflicts). There are no side effects to do using
except for global namespace pollution.
Your problem is probably that you have not saved the changes made to your files.
The blue dots mean that your changes have not been saved yet.
Hit CTRL-S on each file. You could also install a package that saves all files at once when you press CTRL-S.
At first it didn’t work but after restarting PROS after saving each file, my problems were gone, thx!
It was probably because the linter was out of sync.