C++ 链表中的多个构造函数 class: no-class type "ClassName"
Multiple constructors in a C++ LinkedList class: non-class type "ClassName"
我有一个 LinkedList
构造函数,我可以在其中传入一个数组并进行构建。然后我可以通过传入整数来向它添加额外的节点。
但是,我还想要构建 LinkedList
的选项,无需任何参数。在我的 LinkedList.h
文件中,我尝试创建一个构造函数来设置 first
和 last
指针。我的 add
方法应该构造一个 Node
.
但是在我的 main()
函数中,当我尝试使用这个构造函数时,我得到一个错误:
request for member ‘add’ in ‘l’, which is of non-class type ‘LinkedList()’
在 main.cpp
.
中调用的其他方法出现同样的错误
我的两个构造函数的结构哪里出错了?
main.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
int main()
{
//int A[] {1, 2, 3, 4, 5};
//LinkedList l(A, 5);
LinkedList l();
l.add(8);
l.add(3);
cout << l.getCurrentSize()<<endl;
l.display();
return 0;
}
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
protected:
struct Node
{
int data;
struct Node *next;
};
struct Node *first, *last;
public:
//constructor
LinkedList(){first=nullptr; last=nullptr;}
LinkedList(int A[], int n);
//destructor
virtual ~LinkedList();
//accessors
void display();
virtual int getCurrentSize() const;
virtual bool add(int newEntry);
};
#endif
LinkedList.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
//constructor
LinkedList::LinkedList(int A[], int n)
{
Node *t;
int i = 0;
first = new Node;
first -> data = A[0];
first -> next = nullptr;
last = first;
for(i = 1; i < n; i++) {
t = new Node;
t -> data = A[i];
t -> next = nullptr;
last -> next = t;
last = t;
}
};
//destructor
LinkedList::~LinkedList()
{
Node *p = first;
while (first) {
first = first -> next;
delete p;
p = first;
}
}
void LinkedList::display()
{
Node *p = first;
while(p) {
cout << p -> data << " ";
p = p -> next;
}
cout <<endl;
}
int LinkedList::getCurrentSize() const
{
Node *p = first;
int len = 0;
while(p) {
len++;
p = p -> next;
}
return len;
}
bool LinkedList::add(int newEntry)
{
Node *temporary;
temporary = new Node;
temporary -> data = newEntry;
temporary -> next = nullptr;
if (first==nullptr) {
first = last = temporary;
}
else {
last -> next = temporary;
last = temporary;
}
return true;
}
这个问题与你的构造函数本身无关。
LinkedList l();
是一个名为 l
的 函数 的声明,它不带参数,而 returns 是 LinkedList
。这就是编译器抱怨 l
是 非 class 类型 .
的原因
要默认构造一个名为 l
类型 LinkedList
的 变量 ,去掉括号:
LinkedList l;
或者,在 C++11 及更高版本中,您可以改用大括号:
LinkedList l{};
我有一个 LinkedList
构造函数,我可以在其中传入一个数组并进行构建。然后我可以通过传入整数来向它添加额外的节点。
但是,我还想要构建 LinkedList
的选项,无需任何参数。在我的 LinkedList.h
文件中,我尝试创建一个构造函数来设置 first
和 last
指针。我的 add
方法应该构造一个 Node
.
但是在我的 main()
函数中,当我尝试使用这个构造函数时,我得到一个错误:
request for member ‘add’ in ‘l’, which is of non-class type ‘LinkedList()’
在 main.cpp
.
我的两个构造函数的结构哪里出错了?
main.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
int main()
{
//int A[] {1, 2, 3, 4, 5};
//LinkedList l(A, 5);
LinkedList l();
l.add(8);
l.add(3);
cout << l.getCurrentSize()<<endl;
l.display();
return 0;
}
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
protected:
struct Node
{
int data;
struct Node *next;
};
struct Node *first, *last;
public:
//constructor
LinkedList(){first=nullptr; last=nullptr;}
LinkedList(int A[], int n);
//destructor
virtual ~LinkedList();
//accessors
void display();
virtual int getCurrentSize() const;
virtual bool add(int newEntry);
};
#endif
LinkedList.cpp
#include <iostream>
#include <string>
#include "LinkedList.h"
using namespace std;
//constructor
LinkedList::LinkedList(int A[], int n)
{
Node *t;
int i = 0;
first = new Node;
first -> data = A[0];
first -> next = nullptr;
last = first;
for(i = 1; i < n; i++) {
t = new Node;
t -> data = A[i];
t -> next = nullptr;
last -> next = t;
last = t;
}
};
//destructor
LinkedList::~LinkedList()
{
Node *p = first;
while (first) {
first = first -> next;
delete p;
p = first;
}
}
void LinkedList::display()
{
Node *p = first;
while(p) {
cout << p -> data << " ";
p = p -> next;
}
cout <<endl;
}
int LinkedList::getCurrentSize() const
{
Node *p = first;
int len = 0;
while(p) {
len++;
p = p -> next;
}
return len;
}
bool LinkedList::add(int newEntry)
{
Node *temporary;
temporary = new Node;
temporary -> data = newEntry;
temporary -> next = nullptr;
if (first==nullptr) {
first = last = temporary;
}
else {
last -> next = temporary;
last = temporary;
}
return true;
}
这个问题与你的构造函数本身无关。
LinkedList l();
是一个名为 l
的 函数 的声明,它不带参数,而 returns 是 LinkedList
。这就是编译器抱怨 l
是 非 class 类型 .
要默认构造一个名为 l
类型 LinkedList
的 变量 ,去掉括号:
LinkedList l;
或者,在 C++11 及更高版本中,您可以改用大括号:
LinkedList l{};