0

"dlist_test.cc:16: error: 'testList' was not declared in this scope.

dlist_test.cc:16: error: 'Dlist' was not declared in this scope."

I have been looking at other threads on circular dependences or namespace but I only have one header file, and I'm not using a namespace for dlist.h or dlist.cc. Where am I not declaring this correctly? Is it a Makefile problem? Any help would be appreciated, thank you for your time.

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;
}

Makefile:

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
BDL
  • 21,052
  • 22
  • 49
  • 55
Nicholas Chiu
  • 67
  • 1
  • 7
  • Instead testList = Dlist(); Define it as Dlist testList; – user1438832 Feb 06 '20 at 08:48
  • Unrelated to your problem, but please [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – Some programmer dude Feb 06 '20 at 08:51
  • @user1438832, unfortunately, the same issue occurs still even if i write "Dlist testList". – Nicholas Chiu Feb 06 '20 at 08:54
  • This is not the error you are seeing right now, but you also are implementing the class wrong. Instead of implementing the member functions in the `dlist.cc` file, you are re-defining the class, which will cause an error as well. I suggest you have another look at how classes are supposed to be implemented in your instructional material (or get some [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)). – walnut Feb 06 '20 at 08:59
  • Furthermore, `malloc`/`free` is almost always wrong in C++. In your specific case here it is technically undefined behavior. You must use `new`/`delete` instead or rather, you should use `std::unique_ptr`. – walnut Feb 06 '20 at 09:01
  • 1
    Writing `struct` before a type name in a variable declaration as in `struct ListNode* newhead` is sometimes required in C, but is practically always redundant in C++. Please don't do this, since it can have unintended side effects. – walnut Feb 06 '20 at 09:02
  • 1
    Then also have a look at [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Given that you are including other standard library headers at the same time with it, it seems that you don't know what it actually does. Also don't include the `<[...].h>` versions of C library headers, but use the `` versions instead. – walnut Feb 06 '20 at 09:03
  • Instead of `NULL` you should always use `nullptr` since C++11. – walnut Feb 06 '20 at 09:06
  • @walnut, thank you for the pointers. I typically code in C and am learning the ropes of C++. This is all very helpful as I'm not too sure of what I'm doing wrong myself. – Nicholas Chiu Feb 06 '20 at 09:16

1 Answers1

1

These are two different problems:

1) C++ distinguishes uppercase letters from lowercase ones. You declared the class as DList so you need write this name exactly this way. Dlist (with lower case "L") is considered an entirely different name.

2) You've never created variable testList so C++ is right to tell that it doesn't exist. It happens to the best ;) Just change the line

testList = Dlist();

to

Dlist testList = Dlist();

or

Dlist testList;

The both variants are equivalent. C++ will use a constructor without parameters by default.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • I was figuring that it was something I couldn't put my finger on. I've been staring at this for two hours already and I did not notice the miscapitalization at all. After implementing your suggestions, the scope issue has been solved. However, there are new issues that come up such as "dlist_test,cc:(.text+0x5a: undefined reference to 'DList::DList()'" and 'Dlist::add_to_back(int)', etc. Am i calling the method functions incorrectly? – Nicholas Chiu Feb 06 '20 at 09:04
  • @NicholasChiu This looks like a linker error. Here is some information about this type of error: https://stackoverflow.com/q/12573816/3052438 If this doesn't solve the problem, please ask another question. – Piotr Siupa Feb 06 '20 at 09:10
  • @NicholasChiu Actually, I can see the problem. walnut has pointed it in a comment earlier. You syntax in `dist.cc` looks like another class declaration. You need to define it instead. – Piotr Siupa Feb 06 '20 at 09:18