Issues with Static Variables

Hello everyone!

I am new to C++ and openFrameworks but I am not new to programming in general so I understand how static variables and methods work. I have encountered a problem that I am not sure how to solve, however.

I declared a static variable in ofApp.h like so:

#pragma once

#include "ofMain.h"
#include "ofxAndroid.h"

class ofApp : public ofxAndroidApp{

    static ofVec2f tap;

	void setup();
	void update();
	void draw();
	void keyPressed(int key);
	void keyReleased(int key);
	void windowResized(int w, int h);

	void touchDown(int x, int y, int id);
	void touchMoved(int x, int y, int id);
	void touchUp(int x, int y, int id);
	void touchDoubleTap(int x, int y, int id);
	void touchCancelled(int x, int y, int id);
	void swipe(ofxAndroidSwipeDir swipeDir, int id);

	void pause();
	void stop();
	void resume();
	void reloadTextures();

	bool backPressed();
	void okPressed();
	void cancelPressed();

Am attempting to reference it in the following locations:

 void ofApp::touchUp(int x, int y, int id){
    ofApp::tap.set(x, y);

 #include "Button.h"
 #include "ofApp.h"

 bool Button::tapped()
     return position.distance(ofApp::tap) < size;

But am receiving this error:

Can anyone help me figure out what I did wrong? I include ofApp.h in all files where I am attempting to reference the static variable. Thanks!

The class declaration in ofApp.h just declares the static member tap, but does not define/initialise it.

You need to initialise it, which can be done at the top of ofApp.cpp with a line such as:

ofVec2f ofApp::tap;

This line sits in the file outside any method definitions.

Hope that helps!

It did! Thanks for your help :blush: