【问题标题】:Accessing constructor in C++在 C++ 中访问构造函数
【发布时间】:2013-04-13 18:28:03
【问题描述】:

我正在尝试使用类 Node 创建一个 Node 对象:

int main(){
    for(int i=0; i< 20; i++)
        Node *handle = new Node(i, 10);
}

class Node{
 public:
    static vector<Node> map;
    static int totalNodes;
    vector<Node> connections;   
    int NodeID;

    Node(int ID, int weight){
    NodeID = ID;
    CreateConnections(weight);
    totalNodes++;
    map.push_back(*this);
}

由于某种原因我得到了

'Node' : undeclared identifier
'Node' handle : undeclared identifier
 syntax error : identifier node

在上课后将 main() 向下移动

unresolved external symbol 
for Node::map and Node::totalNodes

我对 C++ 有点陌生,所以任何提示都将不胜感激。

【问题讨论】:

    标签: c++ oop class constructor


    【解决方案1】:

    你必须用 ; 结束你的类定义

     class Node
     {
        //....
        map.push_back(*this);
     };  //^^^Cannot miss ;
    

    同时,您需要将Node 的声明放在main 之前。

    还有一点:

     Node(int ID, int weight){
         NodeID = ID;
         CreateConnections(weight);
         totalNodes++;
         map.push_back(*this);
     }//missing closing } on constructor definition
    

    您未定义对maptotalNodes 的引用的原因是: 类的静态成员必须在类外初始化。因此,当您内联构造函数尝试访问它们时,它们没有被定义。所以你有未定义的引用。

    您应该执行以下操作:

     class Node{
     public:
        static vector<Node> map;
        static int totalNodes;
        vector<Node> connections;   
        int NodeID;
        Node(int ID);
     };
    
    
    int Node::totalNodes = 0;  //definition of static variables
    vector<Node> Node::map;
    
    //^^^define constructor outside class body
    Node::Node(int ID){ //your second parameter for ctor not used, so remove it
        NodeID = ID;
        totalNodes++;
        map.push_back(*this);
    }
    

    【讨论】:

    • 我认为你的意思是声明而不是定义。
    • 好的,我在 main 之前添加了 class Node; 并在构造函数的末尾添加了分号,现在它给了我在我的问题中列出的未解决的外部错误。抱歉,右括号,我没有粘贴成员函数并忘记了 };
    【解决方案2】:

    您在使用后声明Node。只需将声明移到main 之前就可以了:

    class Node { ... };
    int main() { ... }
    

    你也可以对类做一个forward-declaration(声明没有定义的标识符):

    class Node;
    int main() { ... }
    class Node { ... };
    

    【讨论】:

      【解决方案3】:

      你要么需要做一个 Node 的前向声明

      class Node;
      
      int main(){
         for(int i=0; i< 20; i++)
         Node *handle = new Node(i, 10);
      }
      
      class Node { }; //impelementation
      

      或在定义class Node 之后放置main

      【讨论】:

      • 这不起作用,因为创建新节点需要声明构造函数。
      • 废话!这是一个前向声明,只是类名是已知的。编译器将完成剩下的工作。
      • liveworkspace.org/code/2wbHmQ$0source.cpp:8:34: 错误:无效使用不完整类型'class Node'
      • 你在开玩笑吗?你看过评论吗?为了可读性,我没有粘贴整个代码。
      • 是的,我读过。如果涉及构造函数,我仍然看不到前向声明如何提供帮助。即使您复制整个实现,也无济于事。
      【解决方案4】:

      在第一行插入下面一行:

      class Node;
      

      【讨论】:

        【解决方案5】:

        您需要将 Node 声明放在使用它的 main 之前。
        仅仅提出声明是不够的,因为 main 中的新节点需要声明节点。
        至于未解析的外部,需要定义Node::totalNodes

        int Node::totalNodes = 0;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-23
          • 1970-01-01
          • 1970-01-01
          • 2011-05-08
          • 1970-01-01
          • 1970-01-01
          • 2023-04-02
          • 2017-10-10
          相关资源
          最近更新 更多