"类型"不是指C++上的值

cle*_*ula 5 c++ pointers openframeworks

我在OpenFrameworks图片中遇到此错误.但似乎是一个简单的C++问题.

ofVec2f does not refer to a value
Run Code Online (Sandbox Code Playgroud)

当然我有指针问题,但我不明白为什么.我试图改变& - >*

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);
    }

//
Run Code Online (Sandbox Code Playgroud)

这是Ball类的构造函数;

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;
}
Run Code Online (Sandbox Code Playgroud)

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();


};
Run Code Online (Sandbox Code Playgroud)

sau*_*hts 7

如果使用dot(.)运算符而不是scope(::)运算符调用静态方法,也会导致此错误.


cle*_*ula 5

OP 在这里 - 就我而言,错误是由于未正确关闭方法而发生的,Canvas::createStuff() 缺少“}”。

  • 不,让它打开。让人们知道遇到这个问题是件好事......我应该想到,哦,坏类型也可能是畸形支具的明显迹象。 (5认同)

TJ *_*sky 1

不看整个内容就不多说了,但我突然想到,如果变量的类型也未声明,C++ 编译器会将变量名称级联为未声明,尽管您可能会看到假设无类型变量是一个int,导致各种愚蠢的事情。

除此之外,检查一下 ofVec2 是否包含在您所拥有的内容中,并查看它位于哪个命名空间中。例如,如果 ofVec2f 在某个命名空间中,您要么需要使用命名空间名称;要么使用命名空间名称。或者,更优选地,引用 ofVec2f 及其名称空间前缀。