简体   繁体   English

c ++错误:未在此范围内声明的对象

[英]c++ error: An object not declared in this scope

Could anyone please explain me why I get the following error?谁能解释一下为什么我收到以下错误?

A.cpp: In member function ‘void A::NewObject(int)’:
A.cpp:11: error: ‘list_a’ was not declared in this scope

I tried declaring list_a i various places.我尝试在各个地方声明 list_a 。 Now it is in prog.cpp, in main().现在它在 prog.cpp 中,在 main() 中。 I don't get it why it isn't allegedly in the scope.我不明白为什么据称它不在范围内。

My simplified code is below.我的简化代码如下。 My idea was to add (NewObject) an object of a class A to a list (Add) while additionally performing some sort of a test (Compare) defined within the A class.我的想法是将 A 类的对象添加 (NewObject) 到列表 (Add),同时另外执行在 A 类中定义的某种测试 (Compare)。

I am a C++ beginner so I'd especially appreciate detailed answers.我是 C++ 初学者,所以我特别感谢详细的答案。 Thanks in advance.提前致谢。 Here are the files:以下是文件:

//A.h

#ifndef A_H
#define A_H

class A
{
private:
    int id;
    int Compare(int, int);
public:
    A()
    {
        id = 0;
    }
    A(int i)
    {
        id = i;
    }
    void NewObject(int i);
};
#endif

//A.cpp

#include "A.h"
#include "List.h"

void A::NewObject(int i)
{   
    list_a->Add(i);
}

int A::Compare(int a, int b)
{
    if ( a>b ) return 1;
    if ( a<b ) return -1;
    else return 0;
}


//List.h

#ifndef LIST_H
#define LIST_H

template<typename T>
class Node
{
public:
    T* dana;
    Node *nxt, *pre;
    Node()
    {
        nxt = pre = 0;
    }
    Node(const T el, Node *n = 0, Node *p = 0 )
    {
        dana = el; nxt = n; pre = p;
    }
};

template<typename T, typename U>
class List
{
public:
    List()
    {
        head = tail = 0;
    }
    void Add(const U);
protected:
    Node<T> *head,*tail;
};

#endif


//List.cpp

#include <iostream>
#include "List.h"

template<typename T, typename U>
void List<T,U>::Add(const U el)
{
    int i = 5;
    Node<T> *hlp = new Node<T>();
    head = hlp;
    if ( Compare(el,i) > i )
        std::cout << "Ok" << std::endl;
}

//prog.cpp
#include "List.h"
#include "A.h"

int main()
{
    int i = 5;
    List<class A, int> *list_a = new List<class A, int>();
    A obj;

    obj.NewObject(i);
}

Well the answer is simple: you have never declared a variable named list_a in class A. Thats it.答案很简单:您从未在 A 类中声明过名为 list_a 的变量。仅此而已。

Do it that way:这样做:

class A
{
private:
    List <int> list_a;
    int id;
    int Compare(int, int);
public:
    A()
    {
        id = 0;
    }
    A(int i)
    {
        id = i;
    }
    void NewObject(int i);
};

Remove the U template parameter from your list class.从列表类中删除 U 模板参数。

When this type of error occurs, there might be a mismatch of class name in the main function.发生此类错误时,可能是main函数中的类名不匹配。 Check whether they are same.检查它们是否相同。 Only then, you will get an error like not declared in this scope , which means "they are not declared properly".只有这样,您才会收到类似not declared in this scope的错误,这意味着“它们未正确声明”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM