简体   繁体   中英

error C2143: missing syntax error: missing ';' before '*'

Having issues with the following code:

I've created a Camera class.

class Camera
{
private:
public:
   vec3 Position;
   vec3 Forward;
   vec3 Up;

   float speed;
   float angleSpeed;

   // Constructor with vectors
   void newCamera(void);
   Camera();    
   ~Camera(void);
};

Here is the .cpp code for the Camera class.

void Camera::newCamera(void)
{
    Position = vec3(0.0f, 2.0f, 0.0f);
    Forward = vec3(0.0f, 0.0f, -1.0f);
    Up = vec3(0.0f, 1.0f, 0.0f);
    speed = 0.2f;
    angleSpeed = 0.3f;
}

Camera::Camera()
{
}

Camera::~Camera(void)
{
}

And I'm instantiating it within another class.

class Surface
{
private:
public:
   Camera * cam;
   Surface();   
   ~Surface(void);
};

Here is the .cpp code for the Surface class.

Surface::Surface()
{
     cam->newCamera();
};

Surface::~Surface(void)
{
};

I'm currently getting an error that says- "error C2143: syntax error: missing ';' before '*'"

Your surface class does not know what a Camera is. You need to forward declare it (since it's of type pointer or reference; decreases compile time) and include the header in the source file; also, you can not call methods from a null instance:

Surface class definition:

#ifndef SURFACE_H
#define SURFACE_H

class Camera;

class Surface
{
    private:
    public:
       Camera * cam;
       Surface();   
       ~Surface(void);
};

#endif    

Surface class implementation:

#include "Surface.h"

#include "Camera.h"

Surface::Surface() : cam(new Camera)
{
     cam->newCamera();
};

Surface::~Surface(void)
{
    delete cam;
    cam = nullptr; //Not really needed in this case, but a good habit none-the-less
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM