Getting 'no matching function' trying to use new keyword

Hey guys,

I'm getting a weird error that's preventing my code from compiling and it's driving me nuts.

In my class 'PBQueue' (pointer based queue), I have a structure called qNode which holds a Passenger (in a variable called data) and a pointer to another qNode.

The issue is that when I try to create a new qNode in the enqueue function with the line:

qNode * node = new qNode;

I'm given the error:

no matching function for call to `PBQueue::qNode::qNode()'


The odd thing is, I swear to God this was working just this morning. I'm not sure what I did but now it's giving me this weird error. Any thoughts?

Thanks a lot in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <iostream>
#include "Passenger.hpp"

using namespace std;

class Passenger;

class PBQueue{
    public:
        struct qNode{
            Passenger data;
            qNode * next;
        };
        PBQueue();
        bool isEmpty();
        int getSize();
        Passenger getFront();
        void enqueue(Passenger data);
        Passenger dequeue();
        void displayQueue();
        ~PBQueue();
     
    private:
       qNode * front;
       qNode * back;
       int size;   
    
};

void PBQueue::enqueue(Passenger item){
    qNode * node = new qNode;
    node -> data = item;
    node -> next = 0;
    if(!front)
        front = node;
    else
        back -> next = node;
    back = node;    
    size++;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>
#include "Passenger.hpp"
#include "PBQueue.hpp"

using namespace std;

int main(){
    
    Passenger john("john"), paul("paul"), ringo("ringo"), george("george");
    PBQueue beatles;
    beatles.enqueue(john);
    beatles.enqueue(paul);
    beatles.enqueue(ringo);
    beatles.enqueue(george);
    beatles.displayQueue();
  
I've never used nodes before, but could it be that struct qNode needs a constructor?
Well here's the really weird part:

It works fine if I enqueue a Passenger that hasn't been made with a constructor. It only gives me the error if I create the Passenger with a constructor. See below:


MAIN THAT WORKS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include "Passenger.hpp"
#include "PBQueue.hpp"

using namespace std;

int main(){
    
    Passenger john, paul, ringo, george;
    PBQueue beatles;
    beatles.enqueue(john);
    beatles.enqueue(paul);
    beatles.enqueue(ringo);
    beatles.enqueue(george);
    beatles.displayQueue();

}


MAIN THAT DOES NOT WORK:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
#include "Passenger.hpp"
#include "PBQueue.hpp"

using namespace std;

int main(){
    
    Passenger john("john"), paul("paul"), ringo("ringo"), george("george");
    PBQueue beatles;
    beatles.enqueue(john);
    beatles.enqueue(paul);
    beatles.enqueue(ringo);
    beatles.enqueue(george);
    beatles.displayQueue();

}
Hmm i may just not be following this at all.. but the original error was no matching function for call to `PBQueue::qNode::qNode()' .

qNode() is a constructor for qNode, and its saying it doesnt exist(?). It seems as if you added a constructor for passenger, but i meant to add a constructor for qNode.
closed account (zb0S216C)
How is "Passenger" implemented?

On a side-note, get rid of that "using namespace std" from your header file -- it'll only cause problems.

Wazzak
Ok, so I found a solution, but I don't know WHY it works.

What I did was I created two constructors for Passenger: one that takes an argument and one that doesn't. For some reason when I do this, I don't get the qNode error.

Also: I appreciate the advice regarding the namespace std thing, but I get errors when I don't place it in. I was taught C++ using it, so I'm not sure how to NOT use it.

Anyway, here's the code I'm using, just for reference in case anyone has a similar issue:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Passenger::Passenger(string n){
    name = n;
    waitTime = 0;
    totalTime = 0;
    late = false;
    waiting = false;
}

Passenger::Passenger(){
    name = "John Doe";
    waitTime = 0;
    totalTime = 0;
    late = false;
    waiting = false;
}


Thanks for the assistance as always, guys!
It works fine if I enqueue a Passenger that hasn't been made with a constructor.


Both lots of your code use a constructor. The first uses the default constructor, the second a constructor that takes a single parameter for the name.

I've tried your code and it works fine for me (after adding the missing methods)

I can't see any issues with the code you've posted. So I'm now wondering what else is going on?

Andy

PS I create this little file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Passenger.h

#pragma once

class Passenger{
public:
	Passenger() {}
	Passenger(const std::string& n) : name(n) {}

	const std::string& getName() const	{
		return name;
	}

private:
	std::string name;
};


and added the following to main (I assumed class PBQueue was in PBQueue.h?)

1
2
3
4
5
6
7
8
9
10
11
12
13
PBQueue::PBQueue() : front(0), back(0), size(0){
}

PBQueue::~PBQueue(){
}

void PBQueue::displayQueue(){
	qNode* node = front;
	while(node){
		cout << node->data.getName() << endl;
		node = node->next;
	}
}
Last edited on
closed account (zb0S216C)
jojo212 wrote:
"What I did was I created two constructors for Passenger: one that takes an argument and one that doesn't. For some reason when I do this, I don't get the qNode error."

I'm assuming you added the default constructor (the constructor with no formal parameters) after the conversion constructor (the constructor with a single formal parameter)?

Because "PBQueue::qNode" had a "Passenger" data-member, "qNode"'s default constructor (called when you attempted to allocate a new "qNode") attempted to construct the "qNode::data" data-member. But because you overloaded the default constructor of "Passenger", the compiler was not able to call "qNode::data"'s default constructor so it had no choice but to call the conversion constructor, but that soon failed because it needed an actual parameter to be passed to it*. By explicitly declaring a default constructor for "Passenger", the compiler was able to make the call to the default constructor of "qNode::data" in order to construct the object.

*A compiler cannot assume the value of an actual parameter unless an actual parameter is specified as a default. Therefore, it cannot call a function unless either all formal parameters are given actual parameters by the calling function or all formal parameters have default actual parameters.

Wazzak
Topic archived. No new replies allowed.