简体   繁体   中英

c++: issue defining a class in a constructor

In the constructor of an object I have the following code:

ConfigReader::ConfigReader(){
    TiXmlDocument doc(CONFIGURATION_FILE_LOCATION);
    configDoc = TiXmlHandle(&doc);
}

When I try to compile I get the following warning:

no matching funciton for call to 'TiXmlHandle::TiXmlHandle();

configDoc is a TiXmlHandle defined in my h-file as a private variable for the class. Both of the classes are from the TinyXML C++ project, but that's hardly relevant. The compiler is correct that there is no default constructor for TiXmlHandle . However, since I'm constructing the TiXmlHandle using a valid constructor this, in theory, shouldn't matter.

So what is the syntax, if any, to tell C++ to not bother trying to create the TiXmlHandle with a default constructor since I'm just going to override it?

I appologize for the simple question, I know that this is the sort of thing I should be able to look up but I've tried and not been able to stumble upon the answer.

configDoc is being default constructed and then, later, you are trying to copy assign it to TiXmlHandle(&doc) . A TiXmlHandle apparently can't be default constructed, so this doesn't work. The default construction is happening implicitly because you haven't overridden it by using the member initialization list.

You use the member initialization list like so:

ConfigReader::ConfigReader()
    : configDoc(/*args*/)
{
    // ...
}

Obviously, to pass the proper argument to configDoc in that location you'll have to restructure your code a bit. Perhaps your TiXmlDocument should be a member too. There are a lot of refactoring options; You can figure that part out yourself.

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