【问题标题】:How do I resolve: "error C2039: '{ctor}' : is not a member of" in Visual Studio 2005?如何解决:“错误 C2039:'{ctor}':不是 Visual Studio 2005 中的成员”?
【发布时间】:2026-02-14 16:45:01
【问题描述】:

我在 Visual Studio 2005 中使用 C++ 扩展了一个模板类。 当我尝试使用以下方式扩展模板基类时,它给了我一个错误:

template <class K, class D>
class RedBlackTreeOGL : public RedBlackTree<K, D>::RedBlackTree  // Error 1
{
 public:
  RedBlackTreeOGL();
  ~RedBlackTreeOGL();

当我尝试实例化对象时出现第二个错误:

RedBlackTreeOGL<double, std::string> *tree = new RedBlackTreeOGL<double, std::string>; // error 2

错误 1:

**redblacktreeopengl.hpp(27) : error C2039: '{ctor}' : is not a member of 'RedBlackTree' 和 [ K=双倍, D=std::字符串 ] **

错误 2:

main.cpp(50) : 查看对正在编译的类模板实例化“RedBlackTreeOGL”的引用

【问题讨论】:

    标签: c++ visual-studio-2005 templates class-design visual-c++-2005


    【解决方案1】:

    代码试图继承构造函数,而不是类:-)

    类声明的开始应该是

    template <class K, class D>
    class RedBlackTreeOGL : public RedBlackTree<K, D>
    

    【讨论】:

      【解决方案2】:

      我的天啊,我觉得好傻.....看我自己的代码太久了!

      这是一个非常基本的东西,我不知道我是怎么错过的!

      感谢 James(和 SDX2000),这是通过将声明末尾的“构造函数”从 James 所说的内容中取出来实现的。

      谢谢你:)

      【讨论】:

      • 嘿,事情发生了!我认为 RedBlackTree 是一个内部类,但错过了外部类与内部类同名的事实,这是不可能的,因此第二个 RedBlackTree 是 ctor。
      【解决方案3】:

      RedBlackTree&lt;K, D&gt;::RedBlackTree 有默认构造函数吗?如果您有其他参数化构造函数(ctors),C++ 本身不会定义默认构造函数。

      【讨论】:

        【解决方案4】:

        @SDX2000:

        是的,我在 RedBlackTree::RedBlackTree: 中定义了一个构造函数:

        template <class K, class D>
        class RedBlackTree
            {
            public:
                RedBlackTree();
                // Deleting a storage object clears all remaining nodes
                ~RedBlackTree();
        

        我还为 RedBlackTree 类的构造函数和析构函数实现了一个主体

        【讨论】: