Can't create vector of self defined class

I’m defining a class in a separate file which I’m trying to use in a vector definition in ofApp.h. Here’s the .h file of my class:

class Staff
    Staff(int x, int y, int z, int w, int dist, int numer, int denom);
    void setCoords(int x, int y, int z, int w, int dist, int numer, int denom);
    int getXPos();
    void createStaff();
    int numLines;
    int xStartPnt;
    int yStartPnt;
    int xEndPnt;
    int yEndPnt;
    int yDist;
    int numerator;
    int denominator;
    ofImage clef;
    ofTrueTypeFont meter;

and here’s the .cpp file:

#include "scores.h"

void Staff::setCoords(int x, int y, int z, int w, int dist, int numer, int denom) {
  xStartPnt = x;
  yStartPnt = y;
  xEndPnt = z;
  yEndPnt = w;
  yDist = dist;
  numerator = numer;
  denominator = denom;
  clef.resize(yDist*3, yDist*7);

int Staff::getXPos() {
  int longestNum = 0;
  if (meter.stringWidth(ofToString(numerator)) > longestNum) {
    longestNum = meter.stringWidth(ofToString(numerator));
  if (meter.stringWidth(ofToString(denominator)) > longestNum) {
    longestNum = meter.stringWidth(ofToString(denominator));
  // below should be (yDist*3.5) like it's in meter.drawString()
  // in createStaff() below, but we need a little bit more whiteSpaceExtraOffset
  // so we multiply by 5
  return xStartPnt + (yDist*5) + longestNum;

void Staff::createStaff() {
  for (int i = 0; i < numLines; i++) {
    int yStart = yStartPnt;
    int yEnd = yEndPnt;
    yStart = yStartPnt + (i*yDist);
    yEnd = yEndPnt + (i*yDist);
    ofDrawLine(xStartPnt, yStart, xEndPnt, yEnd);
  clef.draw(xStartPnt+(yDist/2), yStartPnt-(yDist*1.5));
  int numeratorY = yStartPnt+(yDist*2)-(yDist/4);
  int denominatorY = meter.stringHeight(ofToString(denominator));
  denominatorY += (yStartPnt+(yDist*2)+(yDist/4));
  meter.drawString(ofToString(numerator), xStartPnt+(yDist*3.5), numeratorY);
  meter.drawString(ofToString(denominator), xStartPnt+(yDist*3.5), denominatorY);

Staff::Staff(int x, int y, int z, int w, int dist, int numer, int denom) {
	numLines = 5;
	meter.load("LinuxLibertineBold-BYM3.ttf", dist*3);
	setCoords(x, y, z, w, dist, numer, denom);


In ofApp.h I include my class’s header file like this #include scores.h, and define a vector of objects of this class like this vector<Staff> staffs;. When I try to compile I get this errors:

/home/alexandros/Applications/openFrameworks/of_v0.11.2_linux64gcc6_release/apps/myApps/LiveLily/src/ofApp.h:75:24: error: ‘Staff’ was not declared in this scope
   75 |                 vector<Staff> staffs;
      |                        ^~~~~
/home/alexandros/Applications/openFrameworks/of_v0.11.2_linux64gcc6_release/apps/myApps/LiveLily/src/ofApp.h:75:29: error: template argument 1 is invalid
   75 |                 vector<Staff> staffs;

Perhaps this is something trivial for C+±savvy people, but I’m not one of them. It worked when I included the class definition in ofApp.h, but the file was getting a bit too big, and for clarity, I want to move it to its own file. Any ideas how I can achieve this?

Staff has OF dependencies - Do you have something like

#include "ofMain.h"

at the top of scores.h?

Just tried it, but it still won’t work.

Can you share your ofApp.h file too?
I’m curious where your include is located, if inside or outside ofApp class.
I sometimes include bits of functionality even inside ofApp class, maybe it is wrong or lazy but works for me :smiley:
Screen Shot 2022-08-22 at 20.48.46

Finally, I made it work. I changed the following: I had a macro definition in ofApp.h that I also needed in my class so I was doing #include "ofApp.h" in my class’s .h file. That didn’t work. I moved the macro in my class’s .h file and removed the ofApp.h inclution, and the program now compiles.
So, another question is this: is there a way to include macros from ofApp.h in another .h file that is included in ofApp.h?

if you had a variable in ofApp, like testVariable, that you wanted to access somewhere else, you can do this in another cpp file:

#include "ofApp.h"
((ofApp *) ofGetAppPtr())-> testVariable

you can’t include ofApp.h in another h file ofApp includes because it would be a recursive include problem. if you search on the forum of ofGetAppPtr() you can find some examples of usage / discussion…


I know about the recursive include problem, but I thought that the line pragma once was protecting the compiling from this issue. Anyway, thanks for the hint, I’ll give a look at ofGetAppPtr() in the forum.

So, using ofGetAppPtr() was as simple as you posted. In the .cpp file of my class I included the ofApp.h file and then I did this:
lineWidth = ((ofApp*)ofGetAppPtr())->lineWidth; and the lineWidth int variable in the .cpp file of my class got assigned the value of lineWidth int in the ofApp.h file.

1 Like

that’s a good hidden trick.

1 Like