Thread: invalid use of incomplete type

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    219

    invalid use of incomplete type

    Js::Number extends Js::JVar extends Var
    Js::JVar uses Js::Number in it.

    So I've included jvar.h in Js::Number class. which compiles fine.
    and I've made forward reference for Js::Number in jvar.h which doesn't compile and fires compilation errors.

    Code:
    jvar.cpp:31: error: invalid use of incomplete type 'struct Js::Number'
    jvar.h:31: error: forward declaration of 'struct Js::Number'
    and here goes my source code

    jnumber.h
    Code:
    #include <../var.h>
    #include "jvar.h"
    
    namespace Js {
    class Number : public Js::JVar{
      private:
        Var num;
      public:
        Number();
        Number(unsigned int val);
        Number(unsigned long val);
        Number(unsigned short val);
        Number(double val);
        Number(float val);
        Number(int val);
        Number(short val);
        Number(long val);
        ~Number();
    };
    
    }
    Js::Number Compiles fine.

    jvar.h
    Code:
    #include <string>
    #include <memory>
    #include "../var.h"
    
    using std::string;
    using std::auto_ptr;
    
    namespace Js{
      class Number;//Line 31
      class String;
      class Array;
      class Object;
      class Function;
      class Bool;
    }
    
    namespace Js {
    class JVar: public Var{
      private:
        string lhs;
        Var* rhs;
      public:
        JVar();
        JVar(JVar*);
        JVar(long);
        .....
    }
    jvar.cpp
    Code:
    .....
    Js::JVar::JVar(long r): Var(), rhs(0x0){
    	Js::Number* x = new Js::Number(r);//Line 31
    	//rhs = auto_ptr<Var>(dynamic_cast<Var*>(x));
    }
    .....

  2. #2
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    It's hard to say, but jvar.cpp has to see jnumber.h when you create an instance of Js::Number. So you'll have to put a
    Code:
    #include "jnumber.h"
    somewhere

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just because your program can see that Js::Number is a name, doesn't mean it can see the constructor for it. Why can't you include jnumber.h (maybe instead of jvar.h, if you don't have inclusion guards) so that it can see the class definition?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Did you define Js::Var?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    It works when I include jnumber.h in jvar.cpp
    But it doesn't if I include jnumber.h in jvar.h instead of jvar.cpp
    Why ??
    as jvar.h is included by jvar.cpp all the files that jvar.h includes should be included in a chain (In Theory !!) right ?

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I remember this exact question coming up a week or two ago. I remember it being answered, at that.

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Is that in my thread ??

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by noobcpp View Post
    It works when I include jnumber.h in jvar.cpp
    But it doesn't if I include jnumber.h in jvar.h instead of jvar.cpp
    Why ??
    as jvar.h is included by jvar.cpp all the files that jvar.h includes should be included in a chain (In Theory !!) right ?
    jvar.h includes jnumber.h...
    jnumber.h includes jvar.h...
    jvar.h includes jnumber.h...
    jnumber.h includes jvar.h...
    As you can probably figure out, this does not work quite well and explains also why it works if you put it inside your .cpp file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    Thanks
    So #include is in .cpp so that it doesn't go into .hinclude Cycle

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  2. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  3. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM