简体   繁体   English

错误:在'*'标记之前预期')'

[英]error: expected ')' before '*' token

I'm having a problem with the constructor signature in the header below. 我在下面的标题中遇到了构造函数签名的问题。 The compiler gives me the message: 编译器给我的消息:

error: expected ')' before '*' token 错误:在'*'标记之前预期')'

Can anybody tell me what I might be missing here? 谁能告诉我这里可能缺少什么?

#ifndef PRIORITYQUEUE_H
#define PRIORITYQUEUE_H

#include <iostream>
#include <cstdlib>  //We'll need to use srand() and rand() as well as clock()
#include <ctime>
#include <vector>
#include <list>
#include "Graph.h" //header for Graph class

using namespace std;

class PriorityQueue
{

public:

    PriorityQueue(Graph*):infiniteDist(9999);

    void set_previous_node(int, int);

    int get_node_value(int);

    void set_node_value(int, int);    //Change the node value of an element

    void markVisited(int);

    bool contains(int);   //Does the queue contain a particular vertex?

    void insertIntoQueue(int);

    int top(); //pick an unvisited node with the shortest distance. 

    int queueSize();

    void print();

private:

    class vertexNode {
    public:
        int nodeNum;
        int nodeValue;
        int previousNode;   //previous node visited with shortest distance from source
        bool wasVisited;
    };

    vector<vertexNode> nodeValues;
    const int infiniteDist;          //value to represent infinite distance
    int nodeQuantity;


};

#endif // PRIORITYQUEUE_H

The actual constructor is used as in: 实际构造函数用于:

PriorityQueue::PriorityQueue(Graph* graph):infiniteDist(9999)
{
...
}

You are trying to partially declare the constructor by using an initialiser expression, in the declaration of PriorityQueue(Graph*):infiniteDist(9999); 您试图通过在PriorityQueue(Graph*):infiniteDist(9999);的声明中使用初始化表达式来部分声明构造函数PriorityQueue(Graph*):infiniteDist(9999); . This is not allowed. 这是不允许的。 The declaration (generally in the .h file) should just be: 声明(通常在.h文件中)应该只是:

PriorityQueue(Graph* graph);

The definition (generally in the .cpp file) should then be: 定义(通常在.cpp文件中)应该是:

PriorityQueue::PriorityQueue(Graph* graph)
     : infiniteDist(9999)
{
...
}

The reason is simply that the initaliser list is already part of the definition, ie what the method does , rather than just a declaration of the name and return type. 原因很简单,初始化列表已经是定义的一部分,即方法的作用 ,而不仅仅是名称和返回类型的声明。 Imagine that you'd use a different number (say 42) in the declaration and another (9999) in the definition, which one should be used? 想象一下,你在声明中使用了不同的数字(比如42),在定义中使用了另一个数字(9999),应该使用哪一个? Hence it is not allowed. 因此不允许这样做。

change this 改变这一点

PriorityQueue(Graph*):infiniteDist(9999);

to

PriorityQueue(Graph*);

PriorityQueue(Graph*):infiniteDist(9999); is wrong. 是错的。

Either you define your whole constructor in the header 9ie add the body) or you only have to to declare it with PriorityQueue(Graph*); 可以在标题中定义整个构造函数9ie添加正文)或者您只需要使用PriorityQueue(Graph*); 声明PriorityQueue(Graph*);

The solution 2 is the best one. 解决方案2是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM