user1494136
user1494136

Reputation: 89

Why am i getting an error when adding private variable to class [C++]

I have this mesh class which is spread out over two files (mesh.h and mesh.cpp). now it worked perfectly until i added a position variable to it (of type Vector2d). when i added the variable it still ran as normal but as soon as i used it in one of the functions it gave me an error saying a bunch of my variables are not declared. let me show you exactly what i did:

class Mesh{
  public:
  Mesh() {
     Rectangle;
     position = Vector2d(0,0);
  }

  Mesh(Vector2dVector Vertices) {
    Set(Vertices);
    position = Vector2d(0,0);
  };

  Vector2dVector GetVetices() { return vertices; };
  Vector2d SetPosition(Vector2d p) { position = p; }
  float GetVertexCount() { return vertexCount; };
  float GetTriangleCount() { return triangleCount; }

  void  Set(Vector2dVector Vertices){
    Triangulate::Process(Vertices,vertices);
    vertexCount = vertices.size();
    triangleCount = vertexCount/3;
  };

  void Render();
  static Mesh Circle();
  static Mesh Rectangle();
  static Mesh Triangle();

  private:
  Vector2dVector vertices;
  Vector2d position;
  int triangleCount;
  int vertexCount;
};

this ran ok but when i added this to the function render at file mesh.cpp it gave me an error

#include "mesh.h"

void Mesh::Render() {
 glBegin (GL_TRIANGLES); 
     for (int i=0; i<GetTriangleCount(); i++) {
         const Vector2d &p1 = vertices[i*3+0];
         const Vector2d &p2 = vertices[i*3+1];
         const Vector2d &p3 = vertices[i*3+2];
         glVertex2f(position.GetX() + p1.GetX(),position.GetY() + p1.GetY());
         glVertex2f(position.GetX() + p2.GetX(),position.GetY() + p2.GetY());
         glVertex2f(position.GetX() + p3.GetX(),position.GetY() + p3.GetY());
     }
 glEnd ();
}

here's the error message

In file included from main.cpp:3:
mesh.h: In constructor `Mesh::Mesh()':
mesh.h:5: error: no matching function for call to `Vector2d::Vector2d()'
triangulation.h:43: note: candidates are: Vector2d::Vector2d(const Vector2d&)
triangulation.h:46: note:                 Vector2d::Vector2d(float, float)
mesh.h: In constructor `Mesh::Mesh(Vector2dVector)':
mesh.h:10: error: no matching function for call to `Vector2d::Vector2d()'

so after i deleted all of the position.Get.. method calls from the function it still gave me the same error eventhough it didn't before i added and deleted. i have been working with computers for about 4 years now and i've never ever seen i computer act like this. if someone could please explain to me what i'm doing wrong that would be very helpful. btw i'm using DevC++ version 4.9.9.2 so i think it mite have something to do with this.

thanks for reading and sorry it's soooooo long.

Upvotes: 0

Views: 157

Answers (2)

Greg Hewgill
Greg Hewgill

Reputation: 992767

You have a Mesh::Mesh() constructor. The compiler is complaining that it cannot call an appropriate constructor (with no arguments) for the Vector2d class (for the position instance variable). You must either:

  • provide a Vector2d::Vector2d() constructor, or
  • call one of the existing Vector2d constructors for position with appropriate parameters from Mesh::Mesh(), like this:

    Mesh(): position(0, 0) {
    }
    

    (I took out Rectangle; because as written that would have no effect. Did you perhaps want to call that function with Rectangle();?)

Upvotes: 5

Zeta
Zeta

Reputation: 105876

You have to initialize the vector in the constructor's initializer list, not in the constructor body:

Mesh(): position(0,0) {
   Rectangle;
}

Mesh(Vector2dVector Vertices) : position(0,0) {
  Set(Vertices);
};

Otherwise position is going to be default constructed, which would fail since you didn't define a default constructor.

Upvotes: 4

Related Questions