【发布时间】:2017-12-30 08:20:24
【问题描述】:
我正在编写一个相当长的程序。 问题是由于某种原因我不能使用头文件 我收到多个错误。
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
using namespace std;
template <typename P>
class marray {
public:
P* a;
int length;
int mid;
marray(int len)
{
length = len;
a = new P[length];
}
P& operator[](int i)
{
assert(i >= 0 && i < length);
return a[i];
}
marray<P>& operator=(marray<P>& b)
{
if (a != NULL)
delete[] a;
a = b.a;
b.a = NULL;
length = b.length;
return *this;
}
};
template <typename T>
class newtype {
T x;
T y;
public:
T getx() { return x; };
T gety() { return y; };
newtype& operator=(newtype& b)
{
x = b.x;
y = b.y;
};
void setx(T X)
{
x = X;
};
void sety(T Y)
{
y = Y;
};
};
template <typename T>
class RangeTree;
template <typename T>
class Node {
public:
Node()
{
left = NULL;
right = NULL;
point = NULL;
}
newtype<T> key;
Node* left;
Node* right;
RangeTree<T>* point;
};
template <typename T>
class RangeTree {
Node<T>* root;
int size;
public:
RangeTree(Node<T>* v)
{
root = v;
}
void setsize(int SIZE) { size = SIZE; };
int getsize() { return size; };
};
template <typename T>
Node<T>* BUILD2DRANGETREE(marray<newtype<T> > P)
{
Node<T>* v = new Node<T>;
RangeTree<T>* assoc = new RangeTree<T>(BUILDASSOC(MergeSort(P, 0, P.length - 1, 'y')));
v->point = assoc;
if (P.length == 1)
v->key = P[0];
else {
int i;
int k;
if (P.length % 2 == 0) {
P.mid = P.length / 2 - 1;
marray<newtype<T> > Pleft(P.length / 2), Pright(P.length / 2);
}
else {
P.mid = P.length / 2;
marray<newtype<T> > Pleft(P.length / 2 + 1), Pright(P.length / 2);
}
for (i = 0; i <= P.mid; i++)
Pleft[i] = P[i];
for (; i < P.length; i++)
Pright[k++] = P[i];
ν->left = BUILD2DRANGETREE(Pleft);
ν->right = BUILD2DRANGETREE(Pright);
v->key = P[P.mid];
}
return ν;
}
这不是完整的代码。
问题是:
错误 1 错误 C2065: 'Pleft' : 未声明的标识符 f:\rangetreee\rangetreee\source.cpp 106 1 Rangetreee
错误 2 错误 C2065: 'Pright' : 未声明的标识符 f:\rangetreee\rangetreee\source.cpp 108 1 Rangetreee
错误 3 错误 C2065: 'ν' : 未声明的标识符 f:\rangetreee\rangetreee\source.cpp 110 1 Rangetreee
错误 10 错误 C2065: 'νsplit' : 未声明的标识符 f:\rangetreee\rangetreee\source.cpp 218 1 Rangetreee
等等……
【问题讨论】:
-
请修正您的格式。您的代码非常难以阅读。还可以尝试将其缩小到重现错误的minimal reproducible example。
-
缺少格式导致阅读困难,但看起来您在 if/else 块中声明了 pleft 和 pright,因此当您尝试使用它们时它们超出了范围。跨度>
-
@Ian4264 谢谢我试试