English 中文(简体)
c++ 错误: 未在此范围内宣布的物体
原标题:c++ error: An object not declared in this scope
  • 时间:2012-04-21 23:50:25
  •  标签:
  • c++
  • g++

请允许我解释一下我为什么会犯以下错误?

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

我试图宣布“一”名单。 现在主要见原。 我看不出为什么在范围上是 t。

我简化的法典如下。 我的想法是,在清单(Add)中添加(新目标)A类物体,同时进行A类中界定的某种测试(Compare)。

I am a C++ beginner so I d especially appreciate detailed answers. 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);
}
问题回答

答案很简单:你从未在A类中宣布一份有变名的清单。 这就是说。

这样做:

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

将U模板参数从你名单上删除。

当出现这种错误时,在<代码>main功能中可能会出现不同类别的名称。 检查是否相同。 只有到那时,你才会出现像<代码”这样的错误,即“这些错误没有适当申报”。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签