“<”无法解决 Class 模板、Visual Studio 编译器中的函数重载和参数不匹配问题
'<' unable to resolve function overload and Parameters mismatch in Class template, Visual Studio compiler
指出的错误在 }; 之前的末尾。
我什至没有看到任何函数重载或任何不匹配的参数。
这只是我试图实现的一个队列数据结构。但不幸的是,我遇到了这些编译器错误。我正在共享整个代码,以便您可以轻松提供帮助,因为我没有重载任何函数,甚至没有构造函数,也没有使用不匹配的参数。我想图像会帮助我们看到错误。
template<class T>
class Queue {
private:
T* box;
int front;
int rear;
int number_Of_Elements;
int capacity;
public:
Queue(int cap = 0) {
capacity = cap;
front = rear = 0;
number_Of_Elements = 0;
}
bool Empty() {
return size == 0;
}
int next(int i) {
return ((i + 1) % capacity);
}
int previous(int i) {
return ((i + (capacity - 1)) % capacity);
}
int get_Number_Of_Elements() {
return number_Of_Elements;
}
void double_Box() {
T* temp = new T[capacity * 2];
for (int i = 0; i < size; i++) {
temp[i] = box[i];
}
front = 0;
rear = number_Of_Elements;
delete[] box;
box = temp;
}
const T& peek() {
T a = box[front];
front = next(front);
return a;
}
void printQueue() {
cout << "Front is at : " << front << endl;
cout << "Rear is at : " << rear << endl;
for (int i = 0; i < number_Of_Elements; i++) {
cout << "box[" << i << "]" << " : " << box[i] << endl;
}
cout << "------------------------" << endl;
}
void Enqueue(const T& data);
const T& Dequeue();
~Queue() {
delete[] box;
}
// error is exactly here -> };
};
您的问题出在 size
。你还没有申报。
我认为编译器尝试使用一些名为 size 的全局函数来代替,但它失败了。
声明容量成员var后,添加
size_t size;
答案是,有问题,比如有些变量甚至没有初始化就在使用,内存计算错误。就像一个指向内存中某处实际上不存在的指针。
template<class T>
class Queue {
private:
T* box;
int front;
int rear;
int number_Of_Elements;
int capacity;
public:
Queue(int cap = 0) {
capacity = cap;
front = rear = 0;
number_Of_Elements = 0;
}
bool Empty() {
return size == 0;
}
int next(int i) {
return ((i + 1) % capacity);
}
int previous(int i) {
return ((i + (capacity - 1)) % capacity);
}
int get_Number_Of_Elements() {
return number_Of_Elements;
}
void double_Box() {
T* temp = new T[capacity * 2];
for (int i = 0; i < size; i++) {
temp[i] = box[i];
}
front = 0;
rear = number_Of_Elements;
delete[] box;
box = temp;
}
const T& peek() {
T a = box[front];
front = next(front);
return a;
}
void printQueue() {
cout << "Front is at : " << front << endl;
cout << "Rear is at : " << rear << endl;
for (int i = 0; i < number_Of_Elements; i++) {
cout << "box[" << i << "]" << " : " << box[i] << endl;
}
cout << "------------------------" << endl;
}
void Enqueue(const T& data);
const T& Dequeue();
~Queue() {
delete[] box;
}
// error is exactly here -> };
};
您的问题出在 size
。你还没有申报。
我认为编译器尝试使用一些名为 size 的全局函数来代替,但它失败了。
声明容量成员var后,添加
size_t size;
答案是,有问题,比如有些变量甚至没有初始化就在使用,内存计算错误。就像一个指向内存中某处实际上不存在的指针。