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