Declare global(?) variables like auto & string …

Dear openFrameworkers,

I just read the oF-code-style but I’m still not really sure, on what’s the way to declare global(?) variables—by global I mean, I want to initialize them in setup() but also access them in update() and draw().

Especially I’m talking about the variables of type auto and string. For example, int could just be declared like int intVar; in ofApp.h and initialized in ofApp.cpp like intVar = 23; … or we could initialize it like int intVar = 42; in ofApp.h …

But it seems auto and string cannot be just declared like auto foo; in ofApp.h …

So, what’s the ideal way on achieving this? Thank you!

you should declare them in the .h, inside the ofApp class.

auto type means that the compiler has to be able to know what type it is on compile time.

For example:

auto name; // does not work
auto name = “foo”; // works as the compiler knows it has to be a string

Below public: in the ofApp class?

And it’s no problem or inconvinience, that foo in this case it totally meaningless?

PS: When doing like in your example, I get auto not allowed in non-static class member

yeah you can’t use auto in the class declaration, you need to use the type. string or any other type is ok with or without initialization.

so both:

string a;
string b = "foo";

are valid. “foo” here is just an example you should use a value that makes sense in the context of you program or just leave it as it is which would default to an empty string in the case of strings.

Ok, thank you very much!

How would I find out, what type e.g. ofSplitString(ofToString(img.getColor(xPos, yPos)), ", "); needs to be? Because …

ofApp.h: string c;

ofApp.cpp in setup(): c = ofSplitString(ofToString(img.getColor(xPos, yPos)), ", ");

… gives me: No viable overloaded '='

Looking in the reference: http://openframeworks.cc/documentation/utils/ofUtils/#show_ofSplitString

It returns a vector of strings, as it has split the input string into many strings. So the type is vector<string> c;

By the way, what environment are you using to code? VisualStudio?

Ah, I see. For sure … thank you!

I’m using XCode (8.1 on mac OS 10.12.1).

The environment should tell you what return value is expected from the method. I’ve never used xcode, but the autocomplete should show information about the method, its parameters, and the return

1 Like