zychin
zychin

Reputation: 65

Passing a reference of an object to an instance variable of a class, No viable overloaded '='

I'm more of a Java programmer, but I currently have to use C++ to do a project.

I'm stuck on trying to pass a reference of an object to an instance variable of another object, (a IS-A relation). Basically, assigning a reference of ofApp to the MyMesh class &context variable.

However, doing so returns a no viable overloaded '=' error.

Here is mesh.hpp, mesh.cpp:

#ifndef mesh_hpp
#define mesh_hpp

#include <stdio.h>

class ofApp;

class Triangle {
public:
    Triangle(int i1, int i2, int i3) { vertInd[0] = i1, vertInd[1] = i2, vertInd[2] = i3; }
    int vertInd[3];
};

class MyMesh {
public:
    MyMesh(ofApp& context);
    
    
    vector<glm::vec3> verts;
    vector<Triangle> triangles;
     
//    vector<std::array<int, 3>> triangleIndices;
    
    ofApp &context;
    
    
};

#endif /* mesh_hpp */

#include "mesh.hpp"

class ofApp;

MyMesh::MyMesh(ofApp& acontext){
    context = acontext; //no viable overloaded '='
}

Here is ofApp.h, ofApp.cpp:

#pragma once

#include "ofMain.h"
#include "mesh.hpp"

class MyMesh;

class ofApp : public ofBaseApp{

    public:
        ofApp();

        MyMesh &mesh;
        
};

ofApp::ofApp(){
    mesh = MyMesh(*this);
}

Upvotes: 0

Views: 114

Answers (1)

Exlife
Exlife

Reputation: 99

in c++, the reference variable must be initialised at declaration and you cannot change the reference binding, the initialization canot be delayed. So if you want to set the reference in later time, you should use pointer.

Upvotes: 3

Related Questions