【问题标题】:compiler suggests two candidates for class编译器建议两个候选类
【发布时间】:2021-10-22 12:25:39
【问题描述】:

我正在学习 c++98,在编译 Node 类时出现此错误:

Node.cpp:13:1: error: no declaration matches ‘Node::Node(Node::dataType&)’
   13 | Node::Node(dataType& initData){
      | ^~~~
In file included from Node.cpp:7:
node.h:13:7: note: candidates are: ‘Node::Node(const Node&)’
   13 | class Node{
      |       ^~~~
node.h:19:5: note:                 ‘Node::Node(const dataType&)’
   19 |     Node(const dataType&);
      |     ^~~~
node.h:13:7: note: ‘class Node’ defined here
   13 | class Node{
      |       ^~~~

这是我的 Node.cpp

#include "node.h"
#include <cstdlib>

using namespace std;


Node::Node(dataType& initData){
  data = initData;
  next = NULL;
  prev = NULL;
}

void Node::setData(dataType& newData){
  data = newData;
}

void Node::setNext(Node* nextLink){
  next = nextLink;
}

void Node::setPrev(Node* prevLink){
  prev = prevLink;
}

dataType Node::getData(){
  return data;
}

Node* Node::getPrev(){
  return prev;
}

Node* Node::getNext(){
  return next;
}

这是我的 Node.h

#ifndef TYLER_NODE
#define TYLER_NODE

#include <iostream>
#include <cstdlib>
#include "EToll.h"

class Node{
  public:
    //TYPEDEF
    typedef EToll dataType;

    //CONSTRUCTOR
    Node(const dataType&);


    void setData(const dataType&);
    void setNext(Node*);
    void setPrev(Node*);

    dataType getData() const;
    const Node* getPrev() const;
    Node* getPrev();
    const Node* getNext() const;
    Node* getNext();

  private:
    dataType data;
    Node* next;
    Node* prev;
};
#endif

它应该是一个简单的类,它包含 EToll 的实例(来自 EToll.h)以在链表中使用,但我收到此错误。看起来编译器将 Node 类误认为是 Node 构造函数,但我仍在学习,所以我不太确定

【问题讨论】:

  • 这个错误解释了一切:在你的类定义中你只定义了一个签名为Node(const dataType&amp;)的构造函数,但是你定义了一个在你的.cpp文件中不存在的构造函数(它的签名是Node::Node(dataType&amp;))(你有一个与setData类似的问题)。
  • 顺便说一句,您可能会删除示例中的很多代码,但仍然会重现错误。见How to create a Minimal, Reproducible Example
  • 你为什么不学习 C++20 而不是 C++98?
  • @Antonio 这节课是关于数据结构的,课程协调员说我们必须完成所有作业。我也觉得很傻,如果我们需要学习语言我们还不如学习最新版本

标签: c++ c++98


【解决方案1】:

这里的混淆源于编译器在报告候选人时的彻底。第一个是 implicitly 声明的复制构造函数,它在空间上引用类名,因为它是隐式的。在这里看到这不是很有帮助,因为无论如何您都无法定义隐式声明的函数,但是该检查“您是否匹配任何构造函数?”之后进行,您没有这样做是因为const.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多