C++ 无法从对象访问函数
我正在做一个项目,我必须在main.cpp中包含一个头文件。头文件是使用模板文件的堆。由于某些原因,无法在主文件中“看到”插入和删除函数。我收到一条错误消息:C:/Users/Tito/Documents/C++proj/cs3304/Homework2\u 2/Homework10/main.cpp:58:17:错误:请求“enter1”中的成员“remove”,这是非类类型的“priority\u queue\u heap()”。有人能告诉我哪里出了问题吗?我会非常感激的 谢谢 以下是代码行: Main.cpp:C++ 无法从对象访问函数,c++,C++,我正在做一个项目,我必须在main.cpp中包含一个头文件。头文件是使用模板文件的堆。由于某些原因,无法在主文件中“看到”插入和删除函数。我收到一条错误消息:C:/Users/Tito/Documents/C++proj/cs3304/Homework2\u 2/Homework10/main.cpp:58:17:错误:请求“enter1”中的成员“remove”,这是非类类型的“priority\u queue\u heap()”。有人能告诉我哪里出了问题吗?我会非常感激的 谢谢 以下是代码行
/**
* Insert a few elements into a heap and the remove them
* one by one and see if we get them in the right.
*/
#include "priority_queue_heap.h"
#include "heap.h"
#include <iostream>
#include <ctime>
using namespace std;
int test1() {
heap<int> hp;
hp.insert(1);
hp.insert(2);
hp.insert(3);
hp.insert(4);
hp.insert(5);
hp.check_heap();
int x = hp.remove();
cout << "removed " << x << endl;
x = hp.remove();
cout << "removed " << x << endl;
x = hp.remove();
cout << "removed " << x << endl;
x = hp.remove();
cout << "removed " << x << endl;
x = hp.remove();
cout << "removed " << x << endl;
cout << "empty? " << hp.is_empty() << endl;
}
void test2() {
srand(time(NULL));
heap<int> hp;
for(int i = 0; i < 30; i++ ) {
hp.insert(rand());
}
while(!hp.is_empty()) {
int x = hp.remove();
cout << x << endl;
}
}
int main() {
/*test1();
test2();*/
priority_queue_heap<int> enter1();
enter1.insert(135);
enter1.insert(909);
enter1.insert(203);
cout<<endl;
cout<< "values to be removed" << endl;
cout << enter1.remove() << endl;
}
/**
*在堆中插入一些元素,然后删除它们
*一个接一个,看看我们是否把他们弄对了。
*/
#包括“优先级\队列\堆.h”
#包括“heap.h”
#包括
#包括
使用名称空间std;
int test1(){
堆高;
hp.插入(1);
hp.insert(2);
hp.insert(3);
hp.insert(4);
hp.insert(5);
hp.check_heap();
int x=hp.remove();
cout您应该删除main.cpp的第51行输入1后的“()”字符
< C++ >否则,C++将其作为函数调用,不调用构造函数。< p>您应该删除Min .CPP……/P>第51行的SUR1之后的“()”字符。
< C++ >否则,C++将其作为函数调用,不调用构造函数。 您的堆声明中有一个微妙的错误(main .CPP:57):
priority\u queue\u heap enter1();
这里您实际上是在为enter1
函数声明一个原型,该函数不带任何参数并返回优先级队列堆
。只需删除括号即可实际声明变量:
priority\u queue\u heap enter 1;
您的堆声明(main.cpp:57)中有一个细微的错误:
priority_queue_heap<int> enter1();
priority\u queue\u heap enter1();
这里您实际上是在为enter1
函数声明一个原型,该函数不带任何参数并返回优先级队列堆
。只需删除括号即可实际声明变量:
优先级\u队列\u堆输入1;
priority_queue_heap<int> enter1();
您正试图对编译器解释为函数的名称调用成员函数,因此它会告诉您该名称为非类类型。请从enter1中删除()
,这样您就可以
priority_queue_heap<int> enter1;
priority\u queue\u heap enter 1;
现在,enter1
将是一个类型为priority\u queue\u heap
您正试图对编译器解释为函数的名称调用成员函数,因此它会告诉您该名称为非类类型。请从enter1中删除()
,这样您就可以
priority_queue_heap<int> enter1;
priority\u queue\u heap enter 1;
现在,enter1
将是一个类型为priority\u queue\u heap
template <class T>
T priority_queue_heap<T>::remove()
{
return pri_que.remove();
}
template <class T>
void priority_queue_heap<T>::insert(const T& value)
{
pri_que.insert(value);
}
#ifndef PRIORITY_QUEUE_SIMPLE_H
#define PRIORITY_QUEUE_SIMPLE_H
/**
* This class implements a priority queue using a very simple strategy:
* Store the values in an array.
* Add new values at the end.
* When asked to remove a value, search for the largest (linear search)
*
*/
template <class T>
class priority_queue_simple {
public:
static const int CAPACITY = 30;
priority_queue_simple() {
size = 0;
}
bool is_empty() const {
return size == 0;
}
bool is_full() const {
return size == CAPACITY;
}
/**
* Remove the largest value from this priority queue and return it.
*
* Precondition: priority queue is not empty.
*/
T remove();
/**
* Inserts the 'value' into the priority queue.
*
* Precondition: priority queue is not full
*/
void insert(const T& value);
private:
T data[CAPACITY];
int size;
};
#include "priority_queue_simple.template"
#endif // PRIORITY_QUEUE_SIMPLE_H
priority_queue_heap<int> enter1();
cout << enter1.remove() << endl;
priority_queue_heap<int> enter1;