简体   繁体   中英

Public struct inside c++ class

I'm trying to define a public struct inside a C++ class, and can't figure out how to make it compile. In my header, I have the following:

class AtomUtility
{
    public:
        struct BoundingBox;
        void doWork(struct AtomUtility::BoundingBox bounds);
};

And in the source:

#include"AtomUtility.h"
struct AtomUtility::BoundingBox
{
    double xMin, xMax;
};
int main()
{
    AtomUtility::BoundingBox *myBox = new AtomUtility::BoundingBox;
    myBox->xMin = 0;
    myBox->xMax = 10;
    AtomUtility *myUtility = new AtomUtility;
    myUtility->doWork(*myBox);
    delete myUtility;
}
void AtomUtility::doWork(struct AtomUtilty::BoundingBox bounds)
{
    //do things...
}

When I attempt to compile this, I get an error: "class "AtomUtility" has no tag member named "BoundingBox".

You've missed the semi-colon after the definition of AtomUtility::BoundingBox and your compiler is getting confused and giving a poor diagnostic.

You're also trying to pass a AtomUtility::BoundingBox* to a function expecting a AtomUtility::BoundingBox

If your compiler gives poor diagnostics it's often useful to try your code on a different compiler. Several are available online, eg Comeau , Clang , or a pastebin that compiles, such as http://codepad.org

Quoting https://stackoverflow.com/a/6368118/1483826 :

you can only declare pointer or reference to the later class (...). You cannot have objects of later class.

To fix this, you'd need to declare the struct before the class using it as a field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM