How to create a custom error during compilation?


I'm trying to display a custom error when compiling my code if col and greater than x.

Function prototype:
void Coffee :: print42 (int col)

Google for: c++ preprocessor error message

Not sure, is this what you wanted ?

#define col 10
#define test 5

#if col > test
#error col must be less than test
1 Like

I don't think you cant do that in your case.

you have a member function (print42()) and it takes a parameter. The compiler has no clue what value will be passed at run time and the parameter could be the outcome of a runtime calculation say myCoffeeInstance.print32(analogRead(A0));. ➜ there is no way thus the compiler could figure this out.

if this class is in a library, it could be even compiled without anyone calling the function and only the linker will bring that code in when you develop a project

➜ you need to handle that at runtime and fail in a managed way that won't crash the code

I told myself that what I wanted was not possible, but I prefer to ask.

Thank you for your answers.

Is there a way to close a subject, or do you have to wait 4 months?

#include <stdio.h>

int main(void)
    printf("I ❤️ Arduino");

Before coming I had already searched without finding what I want but thank you anyway

I marked your answer as the solution, that will soft close the topic

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.