【问题标题】:Class not declared in scope in main类未在 main 范围内声明
【发布时间】: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 &lt;bits/stdc++.h&gt;
  • @user1438832,不幸的是,即使我写“Dlist testList”,同样的问题仍然存在。
  • 在 C 中有时需要在变量声明中的类型名称之前写 struct,如 struct ListNode* newhead,但实际上在 C++ 中总是多余的。请不要这样做,因为它可能会产生意想不到的副作用。
  • 那也看看Why should I not #include <bits/stdc++.h>?。鉴于您同时包含其他标准库头文件,您似乎不知道它实际上做了什么。也不要包含 &lt;[...].h&gt; 版本的 C 库头文件,而是使用 &lt;c[...]&gt; 版本。

标签: c++ makefile scope


【解决方案1】:

这是两个不同的问题:

1) C++ 区分大写字母和小写字母。您将类声明为DList,因此您需要完全以这种方式编写此名称。 Dlist(带有小写“L”)被认为是一个完全不同的名称。

2) 您从未创建过变量testList,因此 C++ 正确地判断它不存在。它发生在最好的情况下;) 换行就行了

testList = Dlist();

Dlist testList = Dlist();

Dlist testList;

这两种变体是等价的。默认情况下,C++ 将使用不带参数的构造函数。

【讨论】:

  • 我认为这是我无法触及的东西。我已经盯着这个看了两个小时了,我根本没有注意到大写错误。实施您的建议后,范围问题已解决。但是,出现了一些新问题,例如“dlist_test,cc:(.text+0x5a: undefined reference to 'DList::DList()'”和“Dlist::add_to_back(int)'等。我在打电话吗?方法运行不正确?
  • @NicholasChiu 这看起来像一个链接器错误。以下是有关此类错误的一些信息:stackoverflow.com/q/12573816/3052438 如果这不能解决问题,请提出另一个问题。
  • @NicholasChiu 实际上,我可以看到问题所在。核桃在早些时候的评论中指出了这一点。 dist.cc 中的语法看起来像另一个类声明。你需要定义它。
猜你喜欢
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多