【发布时间】:2020-02-06 08:44:04
【问题描述】:
"dlist_test.cc:16: 错误:'testList' 未在此范围内声明。
dlist_test.cc:16: 错误:'Dlist' 未在此范围内声明。"
我一直在查看有关循环依赖或命名空间的其他线程,但我只有一个头文件,并且我没有为 dlist.h 或 dlist.cc 使用命名空间。我在哪里没有正确声明?是 Makefile 的问题吗?任何帮助将不胜感激,感谢您的宝贵时间。
dlist.h:
#ifndef DLIST_H
#define DLIST_H
struct ListNode
{
/* define your list node type */
int val;
ListNode* next;
ListNode* prev;
};
class DList
{
public:
DList();
/* implement copy constructor, assignment, destructor if needed */
void add_to_front(int value);
void add_to_back(int value);
int first();
int last();
void remove(ListNode* node);
ListNode* previous(ListNode* node);
ListNode* next(ListNode* node);
ListNode* search_value(int value);
private:
/* declare your data */
ListNode* head;
ListNode* tail;
};
#endif
dlist.cc
#include "dlist.h"
#include <cstddef>
#include <stdlib.h>
class Dlist{
public:
Dlist(){
head = NULL;
tail = NULL;
}
void add_to_front(int value){
struct ListNode* newhead = (struct ListNode*) malloc(sizeof(struct ListNode));
newhead->val = value;
newhead->prev = NULL;
newhead->next = head;
if(head != NULL)
head->prev = newhead ;
head = newhead;
}
void add_to_back(int value){
if (tail == NULL){
struct ListNode* firstValue = (struct ListNode*)malloc(sizeof(ListNode));
firstValue->val = value;
firstValue->prev = NULL;
firstValue->next = NULL;
tail = firstValue;
}else{
struct ListNode* newtail = (struct ListNode*)malloc(sizeof(ListNode));
newtail->val = value;
newtail->next = NULL;
newtail->prev = tail;
tail->next = newtail;
tail = newtail;
}
}
int first(){
return head->val;
}
int last(){
return tail->val;
}
void remove(ListNode* node){
if (head == NULL || node == NULL){
return;
}
if(head == node){
head = node->next;
}
if (node->next != NULL){
node->next->prev = node->prev;
}
if (node->prev != NULL){
node->prev->next = node->next;
}
free(node);
}
ListNode* previous(ListNode* node){
if(node->prev != NULL){
return node->prev;
}
}
ListNode* next(ListNode* node){
if(node->next != NULL){
return node->next;
}
}
ListNode* search_value(int value){
while(head->next != NULL){
if(head->next->val == value){
return head;
}else{
head = head->next;
}
}
}
private:
ListNode* head;
ListNode* tail;
};
dlist_test.cc
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "dlist.h"
#include <bits/stdc++.h>
using namespace std;
int main (int argc, char* argv[])
{
int N = -1;
if (argc == 2) {
N = atoi (argv[1]);
assert (N > 0);
}
testList = Dlist();
int i = 0;
while(i<N){
testList.add_to_back(i+1);
i++;
}
int randn = rand() % N + 1;// randn in the range 1 to N
//
time_t start, end;
time(&start);
ios_base::sync_with_stdio(false);
struct ListNode* loc = testList.search_value(randn);
testList.remove(loc);
time(&end);
double time_taken = double(end - start);
cout << "Time taken by program is : " << fixed
<< time_taken << setprecision(5);
cout << " sec " << endl;
//
testList.add_to_front(N);
return 0;
}
生成文件:
default:
@echo "=================================================="
@echo "To build your sorting code, use:"
@echo "make dlist_test or make queue_test"
@echo "=================================================="
# Queue driver
queue_test: queue.o
# Doubly linked list driver
dlist_test: dlist.o dlist_test.o
g++ -o dlist_test dlist.o dlist_test.o
dlist.o: dlist.cc dlist.h
g++ -c dlist.cc
dlist_test.o: dlist_test.cc
g++ -c dlist_test.cc
clean:
rm -f core *.o *~ queue_test dlist_test
# eof
【问题讨论】:
-
改为 testList = Dlist();定义为 Dlist testList;
-
与您的问题无关,但请don't include
<bits/stdc++.h>。 -
@user1438832,不幸的是,即使我写“Dlist testList”,同样的问题仍然存在。
-
在 C 中有时需要在变量声明中的类型名称之前写
struct,如struct ListNode* newhead,但实际上在 C++ 中总是多余的。请不要这样做,因为它可能会产生意想不到的副作用。 -
那也看看Why should I not #include <bits/stdc++.h>?。鉴于您同时包含其他标准库头文件,您似乎不知道它实际上做了什么。也不要包含
<[...].h>版本的 C 库头文件,而是使用<c[...]>版本。