Hello,
I'm having an issue that seems to be a direct error of the arduino compiler more than my direct code, and I don't know how to fix it.
Effectively, for no reason whatsoever, the compiler will fail to find a struct I've defined for some functions, but not others, and it seems entirely random. My code is split across 3 files, but the core issue is file number 2:
#include <cmath>
#include <LinkedList.h>
struct Vector2 { float x,y; };
float getDistance(Vector2 point1, Vector2 point2) {
return sqrt(
pow(point1.x - point2.x, 2) +
pow(point1.y - point2.y, 2)
);
}
LinkedList<Vector2> createLineDefinition(Vector2 pointA, Vector2 pointB, int dotsBudget) {
LinkedList<Vector2> result = LinkedList<Vector2>();
for(int i = 0; i < dotsBudget; i++) {
Vector2 point;
float perc = float(i)/float(dotsBudget-1);
point.x = pointA.x + (pointB.x-pointA.x)*perc;
point.y = pointA.y + (pointB.y-pointA.y)*perc;
result.add(point);
}
return result;
}
LinkedList<Vector2> createShape(LinkedList<Vector2> points, int dotsBudget) {
float perimeter = 0;
for (int i = 0; i < points.size()-1; i++) {
perimeter += sqrt(pow(points[i].x - points[i+1].x, 2) + pow(points[i].y - points[i+1].y, 2));
}
LinkedList<Vector2> result = LinkedList<Vector2>();
int dotsUsedTotal = 0;
for (int i = 1; i < points.size(); i++) {
int dotsAllowed = dotsBudget * (sqrt(pow(points[i].x - points[i-1].x, 2) + pow(points[i].y - points[i-1].y, 2))/perimeter);
dotsUsedTotal += dotsAllowed;
LinkedList<Vector2> tmp = createLineDefinition(points[i-1],points[i],dotsAllowed);
for (int x = 0; x < tmp.size(); x++) {
result.add(tmp[i]);
}
}
// Dot corrector
for(int i = 0; i < dotsBudget - dotsUsedTotal; i++) {
result.add(result.get(result.size() - 1));
}
return result;
}
For some reason, while functions "createShape" and "createLineDefinition" are totally 100% happy and compile just fine, "getDistance" throws a "Compilation error: 'Vector2' was not declared in this scope" error, and I can't for the life of me understand why. It's literally the exact same variable that compiles happily for the other 2 functions, but for some reason "getDistance" and another function later on "addDataToCurrentBuffers" seem to throw this same error for some reason. The only link I can see is that functions returning a LinkedList seem happy, but void functions and float functions fail for some reason.
This is truely perplexing to me, and I've tried taking into account the order of compilation and the way the compiler scans the file for linking variable types, but it just seems entirely random to me.
Using Arduino IDE 2.2.1. Tried restarting Arduino IDE, no difference still complains. Specifically the error is:
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:3:19: error: 'Vector2' was not declared in this scope
3 | float getDistance(Vector2 point1, Vector2 point2) {
| ^~~~~~~
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:3:35: error: 'Vector2' was not declared in this scope
3 | float getDistance(Vector2 point1, Vector2 point2) {
| ^~~~~~~
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:3:49: error: expression list treated as compound expression in initializer [-fpermissive]
3 | float getDistance(Vector2 point1, Vector2 point2) {
| ^
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:59:40: error: 'Vector2' was not declared in this scope
59 | void addDataToCurrentBuffer(LinkedList<Vector2> points) {
| ^~~~~~~
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:59:47: error: template argument 1 is invalid
59 | void addDataToCurrentBuffer(LinkedList<Vector2> points) {
| ^
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:3:49: error: 'float getDistance(Vector2, Vector2)' redeclared as different kind of entity
3 | float getDistance(Vector2 point1, Vector2 point2) {
| ^
C:\Users\aj200\Documents\GitHub\My-Projects\Active Projects\ESP32_Renderer\Rendering_Engine.ino:3:7: note: previous declaration 'float getDistance'
3 | float getDistance(Vector2 point1, Vector2 point2) {
| ^~~~~~~~~~~
exit status 1
Compilation error: 'Vector2' was not declared in this scope
Kind regards,
Andrey