Класс не объявлен в области видимости в main - PullRequest
0 голосов
/ 06 февраля 2020

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

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

1 Ответ

1 голос
/ 06 февраля 2020

Это две разные проблемы:

1) C ++ отличает прописные буквы от строчных. Вы объявили класс DList, поэтому вам нужно написать это имя именно так. Dlist (в нижнем регистре "L") считается совершенно другим именем.

2) Вы никогда не создавали переменную testList, поэтому C ++ прав, говоря, что она не существует. Это случается с лучшими;) Просто измените строку

testList = Dlist();

на

Dlist testList = Dlist();

или

Dlist testList;

Оба варианта эквивалентны. C ++ будет использовать конструктор без параметров по умолчанию.

...