CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Posts
    3

    Forward Declaration

    I have a class Rule and another class Condition. Rule.hh file has a pointer declaration to Condition class. The definition looks like -

    //--------------------- Rule.hh ------------------
    #ifndef __RULE_HH__
    #define __RULE_HH__

    #include <iostream>
    #include "Condition.hh"

    class Condition;

    class Rule {
    public:
    void Condition( const char* );
    private:
    Condition *condition;
    }

    //----------------------- Rule.cc -------------
    void
    Rule::Condition( const char *c) {
    condition = new Condition( c );
    }

    And then Condition class is defined in Condition.hh file.

    Problem - Even though I forward declare Condition class in Rule.hh, I am getting an error while compiling -
    "ISO C++ forbids declaration of 'Condition' with no type"
    "expected ';' before '*' token"

    Can someone please help me solve this problem?

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Forward Declaration

    My eyes are bleeding. Use code-tags, please.

    Quote Originally Posted by GotToDream
    Can someone please help me solve this problem?
    You're doing this:
    Code:
    class Foo {};
    
    class Bar
    {
        void Foo() {}
    
        Foo foo;
    };
    You're confusing the compiler. Change "void Rule::Condition( const char* );" and you'll be set.

  3. #3
    Join Date
    Jun 2008
    Posts
    3

    Re: Forward Declaration

    Thanks. That obviously worked. Feeling bad for wasting time on a silly mistake.

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: Forward Declaration

    also it appears that you are both forward declaring condition and including condition.h in rule.h. You don't need to do both, in fact the forward declaration is pointless if you also include condition.h
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #5
    Join Date
    Jun 2008
    Posts
    3

    Re: Forward Declaration

    Right. But I do a condition = new Condition() in Rule.cc file. So when I comment the #include "Condition.hh" statement, I get an error
    "invalid use of undefined type 'struct Condition'"
    "forward declaration of 'struct Condition'

    Do you know why I am getting this error?

  6. #6
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: Forward Declaration

    I think you can not do because compilar would not be able to see the contructor you are invoking by calling new.

    Now you have to provide declaration for constructor as well..


    So better when you are writting defnition that is CPP include .h file and not forward declaration.. While if you are creating writting header file you can use forward declaration.

    Code:
    MYclass1.h
    
    class HelloPrashant //in header file use forward declaration.
    
    class MyClasss1
    {
    ...
    ....
    private:
      HelloPrashant* h;
    }
    
    MyClass1.cpp
    
    #include "MYclass1.h"
    #include "HelloPrashant.h" //in cpp include header file of the class
    
    MyClass1::SomeMethod()
    {
       h = new HelloPrashant();
    }
    Dont forget to rate my post if you find it useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured