请允许我解释一下我为什么会犯以下错误?
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);
}