"Type" does not refer to a value on C++
Asked Answered
J

3

11

I'm getting this error at OpenFrameworks artwork. But appears to be a simple C++ issue.

ofVec2f does not refer to a value

Certainly I'm having problems with pointers, but I could't understand why. I tried to change & -> *

canvas4.cpp

void Canvas4::createStuff() {
    ballCollection.clear();
    for (int i=0; i<num; i++) {
        ofVec2f org;
        org.set(ofRandom(edge, ofGetWidth()-edge), ofRandom(edge, ofGetHeight()-edge));
        float radius = ofRandom(50, 150);
        ofVec2f loc;
        loc.set(org.x+radius, org.y);
        float offSet = ofRandom(TWO_PI);
        int dir = 1;
        float r = ofRandom(1);
        if (r>.5) dir =-1;
        myBall = new Ball(org, loc, radius, dir, offSet);
        ballCollection.push_back(* myBall);
    }

//

This is the constructor of Ball class;

Ball::Ball(ofVec2f &_org, ofVec2f &_loc, float _radius, int _dir, float _offSet) {
// **** error occur right here.
// use of undeclared "_org"
    org = _org;
    loc = _loc;
    radius = _radius;
    dir = _dir;
    offSet = _offSet;
}

Header Canvas4.h

class Ball {
public:
    ofVec2f org;
    ofVec2f loc;
    float sz = 10;
    float theta, radius, offSet;
    int s, dir, d = 60;

    Ball(ofVec2f &_org, ofVec2f &_loc, float _radius, int _dir, float _offSet);


};


class Canvas4{
public:
    int fc = 100;
    int num = 100;
    int edge = 200;
    vector<Ball> ballCollection;
    Boolean save = false;
    ofFbo fbo;
    Ball *myBall;

    Canvas4();


};
Johanson answered 5/9, 2014 at 20:1 Comment(5)
In which constructor call does the error occur? In myBall = new Ball(org, loc, radius, dir, offSet); or in ballCollection.push_back(* myBall);?Clamber
@Clamber This one Ball::Ball(ofVec2f &_org, ofVec2f &_loc, float _radius, int _dir, float _offSet)Johanson
Got that. But what's next in the stack trace? From where is that very constructor called?Clamber
Canvas4::createStuff() is going to leak memory like a sieve in these lines: myBall = new Ball(org, loc, radius, dir, offSet); ballCollection.push_back(* myBall); Change it to ballCollection.push_back( Ball(org, loc, radius, dir, offSet) ); There's absolutely no reason to be dynamically allocating memory there.Gerome
Not the problem, but in C and C++, identifiers starting with an underscore (or containing a double-underscore) are reserved. If you use them and get unlucky, you can get clashes with compiler-specific keywords, standard library internals etc. A single trailing underscore (e.g. hello_) is OK. It's a mistake I used to make a lot years ago, having looked at compiler-supplied header files and thinking they were a good example to imitate. If you want a naming convention for parameters, the most common seem to use pname, p_name, pName etc. Prefixes using m for class members are common too.Phosgene
R
24

This error is also caused if you call a static method using dot(.) operator instead of scope(::) operator.

Revelationist answered 13/9, 2016 at 16:40 Comment(0)
J
6

OP here - In my case, the error happened due to not closing the method properly, the Canvas::createStuff() was missing "}".

Johanson answered 5/9, 2014 at 21:9 Comment(1)
Nah, leave it open. It's good for people to know that are running into this issue.... and I should have thought, oh, bad type could also be a tell tale sign of a malformed brace.Inae
I
1

Don't much more without looking at the whole of it, but off the top of my head, C++ compilers will cascade variable names as undeclared if their types are also undeclared, although you may see on out there assuming that the typeless variable is an int, leading to all sorts of goofiness.

Beyond that, check to see if ofVec2 is being included by what you have, and see what namespace it is in. Like, if ofVec2f is in some namespace, you will either need to do using namespacename; or, morepreferrably, refer to ofVec2f with its namespace prefix.

Inae answered 5/9, 2014 at 20:14 Comment(3)
ovVec2f is a simple 2D vector class, see docs here. If the header weren't correctly included or the implemention wrongly linked there would be compiler or linker errors respectively. But from the description above I get that OP gets a runtime error.Clamber
I downloaded the framework and looked at the source file for ofVec2f. It's not in any namespace, so maybe he's not including it. He is complaining about a compiler error. Maybe just, for the heck of it, he could try sticking #include "ofVec2f.h" at the top of his file. Then from there, maybe make sure the src is in his include path. But, without seeing the whole project, its hard to judge.Inae
In the first three words (i.e. "Don't much more") should there have been a verb? e.g. I don't know much more?Arbiter

© 2022 - 2024 — McMap. All rights reserved.