名称后跟“::"必须是类或命名空间错误,即使“::"跟在类名之后 [英] Name followed by '::' must be class or namespace error even though the '::' is following a class name

查看:209
本文介绍了名称后跟“::"必须是类或命名空间错误,即使“::"跟在类名之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C++ 实现优先级队列.我创建了一个名为priority_queue.h"的头文件,其中包含我的类的定义,以及一个定义泛型类型的模板.我还有一个文件,我在一个名为priority_queue.cpp"的文件中实现类定义中定义的方法,其中包含priority_queue.h".我的文本编辑器和编译器都在抛出错误,尽管它们似乎不同.

I'm trying to make a priority queue implementation in C++. I have created a header file called 'priority_queue.h' which contains the definition of my class, alongside a template defining a generic type. I also have a file where I implement my method defined in the class definition in a file called 'priority_queue.cpp' where I include 'priority_queue.h'. Both my text editor and compiler are throwing errors, although they seem to be different.

我在 StackOverflow 和其他网站上查找了其他问题,我什至直接从在线答案中复制粘贴了一些代码,这些代码本来是正确的,但这些代码甚至都不起作用.

I've looked up other questions on StackOverflow and other sites, I've even straight up copy-pasted some code from online answers that were supposedly correct and even those didn't work.

这是我的priority_queue.h"头文件中的代码

Here is the code in my 'priority_queue.h' header file

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

template <typename T>

class pqueue{
    public:
        pqueue();
        void insert(T element, int priority);

    private:
        vector<T> elements(); //This vector will store our data in the correct order

};

这是我的priority_queue.cpp"文件中的代码

Here is the code from my 'priority_queue.cpp' file

#include <iostream>
#include <iterator>
#include <vector>
#include "priority_queue.h"

using namespace std;

template <typename T>

void pqueue::insert(T element, int priority){

}

编译器抛出一个错误并说'template;class pqueue' 不带模板参数使用.

The compiler throws an error and says 'template<class T> class pqueue' used without template parameters.

我的文本编辑器给了我一个错误,说 name 后跟 '::' 必须是一个类或命名空间名称

My text editor gives me an error that says name followed by '::' must be a class or namespace name

我使用的是戴尔 XPS 13 9370,配备 i7 8550U、16GB RAM 和 256GB SSD,运行 Windows 10 家庭版 1903,我的文本编辑器是 Microsoft Visual Studio Code Insiders.

I'm on a Dell XPS 13 9370 with the i7 8550U, 16GB of RAM and 256GB SSD running Windows 10 Home Version 1903 and my text editor is Microsoft Visual Studio Code Insiders.

推荐答案

必须在头部定义类模板的成员函数.并写

You have to define the member function of the class template in the header. And write

template <typename T>
void pqueue<T>::insert(T element, int priority){

}

这篇关于名称后跟“::"必须是类或命名空间错误,即使“::"跟在类名之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆