DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 17

Thread: instantiated

  1. #1
    Join Date
    Apr 2004
    Posts
    12

    instantiated

    In the context

    class Timestep {
    public:

    Timestep() {};
    ~Timestep() {};
    }

    I get the compiler error "instantiated from here" at the constructor line. There is a class above (not shown) that declares Timestep a friend class.

    (a) What does instantiated mean in the C++ programming context? (b) Should I declare the Timestep class before the class that calls Timestep a friend?

    thanks,
    DJ

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118

    Re: instantiated

    Originally posted by djones
    In the context

    class Timestep {
    public:

    Timestep() {};
    ~Timestep() {};
    }

    I get the compiler error "instantiated from here" at the constructor line. There is a class above (not shown) that declares Timestep a friend class.

    (a) What does instantiated mean in the C++ programming context? (b) Should I declare the Timestep class before the class that calls Timestep a friend?

    thanks,
    DJ
    Your code misses a semicolon after the class's declaration:
    class Timestep {
    ...
    };<---missing


    Instantiated means that the rest of the code after } is treated as the name of an object of class Timestamp that the compiler mistakenly assumes that you wanted to instantiate. Simply add the missing semicolon, this should solve the problem.
    Danny Kalev

  3. #3
    Join Date
    Apr 2004
    Posts
    12
    Curiously, the missing semicolon was just a copy error. The punctuation exists in the original source code. So, the error persists. Are you saying that the compiler thinks that the constructor prototype and defintion is an attempt to create an object of type Timestep?

    I have a new context for you. I get the same error, 'instantiated from here', in the line where E[0] starts below.

    void function() {
    real E[2];
    ...
    while ( Body* bdy = bdy_itr.loop() ) {
    E[0] += bdy->mass()*(bdy->vel()*bdy->vel());
    E[1] += bdy->mass()*bdy->pot();
    }

    }

    The bdy_itr object is an iterator for the class Body. The functions mass, pot, and vel are member functions of Body.

    I don't see where the compiler could mistake the line E[0] += ... as an attempt to create an object.

    I really appreciate your help, Danny. Thanks for taking the time.

    DJ

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    without looking at the definitions of bdy and other identifiers in your code, it's hard to tell what's going on here:

    while ( Body* bdy = bdy_itr.loop() ) {
    E[0] += bdy->mass()*(bdy->vel()*bdy->vel());
    E[1] += bdy->mass()*bdy->pot();


    I suggest that your break this code into individual statemenets. This way you can easily see what the precedence is and perhaps the compiler will understand your intent better. More comments and corrections below:

    while ( Body* bdy = bdy_itr.loop() ) //unbalanced )) !

    {
    double temp = bdy->mass()*(bdy->vel()*bdy->vel());
    E[0] += temp;

    temp=bdy->mass()*bdy->pot();
    E[1] += temp;

    }
    Danny Kalev

  5. #5
    Join Date
    Apr 2004
    Posts
    12
    real e1(0);
    real e2(0);

    Sometimes, "Instantiating an object" means "Initializing an object".

    And the name of the compiler is important.

    Do you think so?

  6. #6
    Join Date
    Apr 2004
    Posts
    12
    I'm using the Gnu C++ compiler, the standard release that comes with Red Hat Linux 9 distributions.

    The corrections to the while loop seem to work well. What is it about the old method of using a one-line assignment statement that tricks the compiler into initializing a new object? The objects (real E[3]) are already defined above the loop.

    Jacky, are you suggesting by
    e1(0)
    e2(0)
    to use two separate objects instead of an array?

  7. #7
    Join Date
    Apr 2004
    Posts
    12
    Danny, what do you mean by saying that the test in the while loop is "unbalanced"? Let me elaborate on its workings;

    bdy_itr is an iterator with a member function loop() that scrolls through all the available Body objects that match a certain specification (identified by a bitmask). bdy_itr.loop() returns a pointer to an object of type Body. A second pointer to an object of type Body (Body* bdy) is assigned this value. loop() returns a null if it is finished, which would of course terminate the while loop.

    thanks,
    DJ

  8. #8
    Join Date
    Dec 2003
    Location
    Okla, US
    Posts
    126
    Do you use forward declartion? What I mean, is if the class aboves declares it a friend, the compiler must know about it, so you will have to do a ....

    Code:
    class Timestep; // - Forward declaration
    
    class SomeOtherClass
    {
      friend class Timestep;
    };
    
    class Timestep
    {
    };
    gorshing
    newb

  9. #9
    Join Date
    Nov 2003
    Posts
    4,118
    Originally posted by djones
    Danny, what do you mean by saying that the test in the while loop is "unbalanced"? Let me elaborate on its workings;

    DJ
    count the number of ( and ) and you'll see that it isn't balanced.
    Danny Kalev

  10. #10
    Join Date
    Apr 2004
    Posts
    12
    Yesterday, I got many "instantiated from here " error messages given by GCC. I did not understand it at all. I used the BCB , the Borland compiler, to compile it. It gave me more readable error messages. After correcting the code, both of them said nothing.

    I suggest you to try it.

  11. #11
    Join Date
    Apr 2004
    Posts
    12
    Jacky, could you be more specific about the corrections you made? My code is also working fine now (after the corrections suggested by Danny were applied) but I would like to better understand this "instantiated from here" message.

  12. #12
    Join Date
    Apr 2004
    Posts
    12
    Jacky;

    Unfortunately I do not have a Borland compiler. Could you give a small example of a Borland error message in the areas where you got the 'instatiated from here' error from G++?

    Note: GCC and G++ are different compilers (evidenced by different behavior on the same source code - correct me if I'm mistaken), so I assume you were using G++.

  13. #13
    Join Date
    Apr 2004
    Posts
    12
    Yes. Not GCC but G++.

    My suggestion days before is based on an assumption that you wanted to compile the code as soon as possible and you were not interested in the compiler itself.

    I do not understand the message "instan..." at all. I hope this type of messages will disappear in future version.

  14. #14
    Join Date
    Apr 2004
    Posts
    8

    Lightbulb

    Let me see if I can clear up the meaning of the word instantiated for you. A class cannot be used until it is instantiated. What that means is that an instance of the class has to be created.

    Example:

    Class MyClass{


    };

    MyClass is just a class right now because it has not been instantiated. After you instantiate the class (i.e. Create an instance of the class) it becomes an object.

    Example:

    MyClass Test;


    Creates an object called Test, which is an instance of class MyClass. Once an object of the class has been created then you can use all of its attributes and member functions.


    SoloCoder

  15. #15
    Join Date
    Nov 2003
    Posts
    4,118
    ...and to elaborate on what SoloCoder said, when you have something like:

    class MyClass{
    } //note: no ; here

    Test; //the compiler considers anything after the } as an
    //instance of MyClass

    The problem is that an instance may also call the non-default ctor:


    Myclass Test(1,0);

    So your compiler fullishly assumed that the unbalanced parentheses in the loop were an attempt to instantiate an object. As an aside, one of the criteria for evaluating a compiler's quality is its messages intelligibility. And in this regard, your compiler isn't very impressive. I rememeber that 10 years ago and earlier, compiler vendors didn't pay too much attention to this because the common assumption was that "if there is an error, simply tell the user that there's some sort of an error and he or she will already know what the problem is by looking at the source file". This policy was bad then and it got even worse when templates started to be used extensively. Today, compilers that can't tell you an intelligent diagnostic are to be replaced. Spending so much time on interpreting cryptic error messages is too costly an option, although I appreciate the fact that you're looking "undre the hood":)
    Danny Kalev

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links