C ++编译器忽略重载的构造函数

时间:2013-12-22 01:56:22

标签: c++ qt

我的Qt类有两个构造函数:

explicit ConVar(const QString &name, const QString &def, 
                NGlobalCmd::VarCallback callback = NULL, 
                const QString &desc = "", 
                NGlobalCmd::CMDFLAGS flags = 0, 
                bool hasMin = false, float min = 0.0, 
                bool hasMax = false, float max = 0.0, 
                QObject *parent = 0);

explicit ConVar(const QString &name, const QString &def, 
                /* This is the new line */
                CommandManager* manager, ListedConsoleCommand** list,
                NGlobalCmd::VarCallback callback = NULL, 
                const QString &desc = "",
                NGlobalCmd::CMDFLAGS flags = 0, bool hasMin = false, 
                float min = 0.0f, bool hasMax = false, 
                float max = 0.0f, QObject* parent = 0);

但是,当我尝试使用适合第二个构造函数的参数创建类的实例时,我收到一条错误,指出CommandManager指针无法转换为回调指针(即编译器不会尝试使用第二个构造函数)。有什么理由会发生这种情况吗?奇怪的是,我创建的测试Qt项目引用了这个类的库没有遭受同样的错误,而我的实际应用程序项目(引用相同的库)却没有。


修改

定义类的头文件:

static const QString compileDate(__DATE__);
static const QString compileTime(__TIME__);
static ConVar g_compile_version("g_compile_version",
                                compileDate + " " + compileTime,
                                g_pCommandManager,
                                &g_pCommandList,
                                NULL,
                                "Date and time of last compile.",
                                NGlobalCmd::CMDFLAG_READONLY);

错误:

ConVar::ConVar(const QString &, const QString &,
               NGlobalCmd::VarCallback, const QString &, 
               NGlobalCmd::CMDFLAGS, bool, float, bool, 
               float, QObject*): 
cannot convert parameter 3 from 'ListedCommandManager *' 
                             to 'NGlobalCmd::VarCallback' 
- There is no context in which this conversion is possible.

ListedCommandManager是CommandManager的派生类。


修改

进一步缩小了问题范围。如果包含ListedCommandManager标头,则编译成功,但我不确定原因。

/**
 * @brief Extends the CommandManager class by providing functionality to
 * traverse a list of ListedConsoleCommands when the
 * manager is created.
 */
class ICONSOLESHARED_EXPORT ListedCommandManager : public CommandManager
{
public:
    /**
     * @brief Constructor.
     * @param parent QObject parent, if applicable.
     */
    explicit ListedCommandManager(QObject* parent = 0);

    /**
     * @brief Constructor with a pointer to a list to traverse.
     * @param listHead When constructed, the manager will traverse the list
     * whose head is pointed to by this parameter.
     * @param parent QObject parent, if applicable.
     */
    explicit ListedCommandManager(ListedConsoleCommand* listHead,
                                  QObject* parent = 0);

    /**
     * @brief Destructor.
     */
    virtual ~ListedCommandManager() {}

    /**
     * @brief Traverse the list specified by the head and register each
     * console command to the manager.
     * @param listHead Head element of the list of ListedConsoleCommand
     * objects.
     */
    void traverse(ListedConsoleCommand* listHead);
};

1 个答案:

答案 0 :(得分:1)

解决了 - 我在构造函数中尝试使用的全局命令管理器是在头文件中定义的,我包含了这个头文件,但ListedCommandManager只是向前声明的,因此不包含ListedCommandManager.h编译器不知道它是CommandManager的派生类。感谢所有提供帮助的人。