Trouble sharing functions within src files Pros 3

Hey all, I am having some trouble trying to share my functions within my other src files (opcontrol, autonomous and initialize) in pros 3 for V5.
I have created a .h file with header guards and put my pros::Motor and user-defined functions in it but it still gives me there errors “left_drive was not declared in the scope (of my src files)”
Here’s my code:
CompCode.zip (5.11 MB)

You are using


left_drive

as a numeric variable when it is actually an instance of a class.

Take a look at how C++ classes work:
https://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm

Also, for future reference, posting your code on Github (or similar) would make it a lot easier for people to assist you.

There are valid reasons not to do so, but, especially if you’re using PROS, I would strongly recommend doing so.

I agree with that, but I don’t think github allowed me to upload more than 100 files and the project holds 1000+ so… didn’t think it was possible
And thanks, did not know they were classes

How would I edit my code to make sure that the classes are accessable through all src files though?

Your class instances are accessible (unless I missed something looking at your code).

The problem is that you are trying to overwrite your class instances with numbers; the data types on the left of the equals sign (instance of


Motor

) and right (


int

) are incompatible.

Look at the link I posted – it will show you how to correctly work with classes.

The error coming up on my screen is


Use of unidentified identifier  'left_motor'

]
This is also the case with my functions which i prototype in functions.h and define in functions.cpp

setting a motor = to an integer is possible because PROS motors have operator overload, where it sets the motors power whatever number you set it equal too.

Sorry, I didn’t know that about the PROS 3 API.

All my previous advice is moot then.

Your header guards were close :slight_smile: You want


#ifndef FUNCTIONS_H
#define FUNCTIONS_H

not


#define FUNCTIONS_H
#ifndef FUNCTIONS_H

I changed the headers, but now it is saying


unknown type name 'Motor'

when I try to do


extern Motor left_motor; 

Two other comments:

  • You’ve defined

driveSpeed

,


liftSpeed

, and


capFlip

in both functions.cpp and autonomous.cpp, so the compiler doesn’t know which function it should use when you call it. This might just be from your personal debugging, but thought I should give you a heads up. You can remove either one.

  • This is pedantic, but I’d prefer to nitpick for the sake of learning. I’d recommend moving the

#include "functions.h"

into the


#ifdef __cplusplus

section a few lines down. It’s probably not going to be a problem, but the definitions you have in functions.h only work in C++. If you added a C file to your project, the C compiler will get confused when it processes


extern Controller master;

, which is a C++ class (although the C compiler won’t know).

Short answer:
To make the linter stop complaining about it, add


#include "main.h"

at the top of functions.h.

Long answer:
The linter will complain to you because you haven’t defined those types in the header file. The linter doesn’t know that it’ll work fine when you compile your project for real. The reason it works fine is that you


#include "functions.h"

in a file (the file is main.h) after you’ve included the PROS API, so the compiler has already seen the types there. When the linter takes a look at the file, it has no idea that you’ve only included it in main.h and that those types really do exist.

Did this, now it is saying my #ifndef FUNCTIONS_H is saying undetermined conditional directive

I tried putting all of my prototyped functions and motor classes in main.h and it seems to work fine and compiles
I think the issue im getting with #ifndef is because im includeing main.h