yet another forward declaration problem.

This is a discussion on yet another forward declaration problem. within the C++ Programming forums, part of the General Programming Boards category; I've got two files... sdl.h and ray.h. Two classes, Vector3 and Point3 (+ a bunch more..) are defined in sdl.h. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    3

    yet another forward declaration problem.

    I've got two files... sdl.h and ray.h.

    Two classes, Vector3 and Point3 (+ a bunch more..) are defined in sdl.h.

    One class, Ray is defined in ray.h. above the definition i put two forward declarations for the classes defined elsewhere that Ray uses.

    Also, sdl.h contains other class definitions that instantiate a 'Ray', but have been commented out for now.

    Code:
    #include "sdl.h"
    
    class Vector3;
    class Point3;
    
    class Ray {
     public:
      //Ray();
      Point3 start;
      Vector3 dir;
      //void setStart(Point3& p) {start.x=p.x; start.y=p.y; start.z=p.z;};
      //void setDir(Vector3& v) {dir.x=v.x ; dir.y=v.y ; dir.z=v.z;};
      //other fields and methods....
    };
    I see the following error:
    ray.h:20: error: field `start' has incomplete type
    ray.h:21: error: field `dir' has incomplete type


    Can someone explain what this error is and how to solve the problem?

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    I believe it would work if you removed the class Vector3; and class Point3; lines at the top of ray.h.
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    You can't create objects from forward declarations. Make them pointers instead.
    Code:
    #include "sdl.h"
    
    class Vector3;
    class Point3;
    
    class Ray {
     public:
      //Ray();
      Point3* start;
      Vector3* dir;
      //void setStart(Point3& p) {start = new Point3(); start.x=p.x; start.y=p.y; start.z=p.z;};
      //void setDir(Vector3& v) {dir = new Vector3(); dir.x=v.x ; dir.y=v.y ; dir.z=v.z;};
      //other fields and methods....
    };
    of course you probably want to do the new in the constructor, i just did it there to make your example work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. forward declaration in C++
    By ducttape in forum C++ Programming
    Replies: 6
    Last Post: 10-10-2009, 04:00 PM
  2. Forward declaration with g++
    By blakaxe in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2009, 12:44 PM
  3. Forward declaration with g++
    By blakaxe in forum Linux Programming
    Replies: 0
    Last Post: 06-15-2009, 10:22 AM
  4. Forward declaration owned my ass apparently...
    By RITZ in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2006, 12:44 AM
  5. problems with forward declaration
    By logicalhippo in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2003, 08:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21