C++ compiler error



  • Technically not a CodeSOD I suppose, but whatever.

    The following is an anonymized error I'm currently getting from VS2010 but not in VS2008 (project is compiled in multiple VS versions because we distribute DLLs and there's apparently weirdness with C++ runtime versions).

    error C2248: 'ProjectName::ClassName::NestedClassName::NestedClassConstructor' : cannot access private member declared in class 'ProjectName::ClassName::NestedClassName'
    

    Translation: Somehow this class does not have access to its own private members!



  • Is NestedClassConstructor a class?



  • No, it's the constructor for NestedClassName.

    Had to get another dev to handle it. I can do C and I can do C# python et al, but C++ is a monstrous steaming pile of WTF from everything I've seen so far. Turns out it was another case of "Here's a C++ compiler error that has nothing to do with what the actual error is, good luck with your psychic debugger!"



  • @mott555 said:

    No, it's the constructor for NestedClassName.

    Had to get another dev to handle it. I can do C and I can do C# python et al, but C++ is a monstrous steaming pile of WTF from everything I've seen so far. Turns out it was another case of "Here's a C++ compiler error that has nothing to do with what the actual error is, good luck with your psychic debugger!"

    I know I don't do C++ stuff on a regular basis, but I was under the impression that constructors in C++ had the same name as the class.



  • @powerlord said:

    I know I don't do C++ stuff on a regular basis, but I was under the impression that constructors in C++ had the same name as the class.

    They do...I just labeled it that way in my anonymization because I thought it was clearer than ProjectName::ClassName::NestedClassName::NestedClassName



  • @mott555 said:

    Had to get another dev to handle it....Turns out it was another case of "Here's a C++ compiler error that has nothing to do with what the actual error is, good luck with your psychic debugger!"

    So what was the deal‽ Don't leave us hanging.



  • @boomzilla said:

    So what was the deal‽ Don't leave us hanging.

    Not sure, something about a friend struct template thing that slightly changed behind the scenes between VS2008 and VS2010. It's over my head.



  • Usually friend is used to allow instances of other specific classes to modify an object's private properties.

    Or so I understand it.



  • @mott555 said:

    Somehow this class does not have access to its own private members!

    #include "chastitybelt.h"
    @boomzilla said:
    Don't leave us hanging.

    *snicker*


    Filed under: [It's Friday and I'm tired.](#tag9)


  • I'm roughly aware of friend. It's the templates and structs that are over my head. I gather templates are kind of like .NET generics, and my .NET understanding of struct probably has nothing to do with a C++ struct.



  • @mott555 said:

    I'm roughly aware of friend. It's the templates and structs that are over my head. I gather templates are kind of like .NET generics, and my .NET understanding of struct probably has nothing to do with a C++ struct.

    C++ structs are default public, while classes are default private. That's it. There is no other difference, and you can do anything in a struct you can otherwise do in a class.

    Templates, on the other hand, start off simple, but morph into scary beasts...

    At least your original error wasn't an out-and-out 'rejects-valid' bug in the compiler. Those are always fun, especially when the standardese gets involved.


  • Discourse touched me in a no-no place

    @tarunik said:

    Templates, on the other hand, start off simple, but morph into scary beasts...

    Codethulhu likes templates.



  • Just

    #define private public
    #define protected public
    

    and be happy. After all, if you've done nothing wrong, you have nothing to hide, right?



  • Out of curiosity, two questions:

    1. What is the access specification of the NestedClass constructor? (The default for class members is private.)

    2. Where is NestedClass instantiated?



  • @mott555 said:

    I gather templates are kind of like .NET generics

    The rule of thumb is that .NET generics and Java generics behave roughly the same way. Templates in C++ are much, much more complicated because they are still basically very, very fancy macros.

    What you can do with templates, however, is kind of functional-style programming. For example, imagine the following.

    class Drawable
    {
    public:
        void draw();
    };
    
    class Sprite
    {
        // define whatever sprite needs
    public:
        void draw(const vec2& position);
    }
    
    template <typename Drawable>
    void draw(Drawable item)
    {
        item.draw();
    }
    
    template <>
    void draw(Sprite sprite)
    {
        // do something special, get the position or something
        vec2 pos = state.getPositionFor(sprite);
        sprite.draw(pos);
    }
    

    While this is highly contrived (and non-working), you can do sort of polymorphism without the cost of inheritance. All still in type-safe manner. Also, another bonus is that modern C++ compilers are really good in optimizing this type of code.

    I'm not saying they are easy by any means. If compilers have bugs, they are mostly in templates. And everyone knows how clear the template-related compiler errors are...



  • There are 4 basic ways to control access to members of your class in C++.

    public: public members can be accessed from any function in your program.
    protected: protected members can be accessed from functions that are part of same class or classes that are inherited from that class.
    private: private members can only be accessed from functions that are part of the same class. They can not be accessed from inherited classes or anywhere else. Keep in mind that private does not prevent a member function from one object from interacting with private members of another object of the same class.
    friend: defining a friend function or class within a class allows that function to interact with private and protected members as if they were public. Keep in mind that the function declaration must match exactly.


  • Discourse touched me in a no-no place

    @disieh said:

    What you can do with templates, however, is kind of functional-style programming.

    Can you employ the CRTP with generics, or is that only possible with C++-style templates?

    (I was gonna let people google the term, but it turns out wikipedia's the first link for CRTP...which is a bit disappointing, somehow. I had hoped it would be some obscure trade association.)

    Also, NECRO. FU DISCURSE.


  • Discourse touched me in a no-no place

    @FrostCat said:

    Can you employ the CRTP with generics, or is that only possible with C++-style templates?

    It most certainly does crop up in Java code, but with everything having a common root class and using fully virtual method dispatch always, the implications are a lot less profound.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.