push_back error with vector of objects

I have a class roster whose main property is a vector of member class objects. But I am having trouble in making a method to add a new member to this vector.

The roster class header file includes the following

1
2
3
4
5
6
7
8
9
10
11
12
class roster
{
public:

	void addMember(string name);
	// ...

private:

	int runningID;
	vector<member> members;
};


And the definition of addMember() is as follows:

1
2
3
4
5
6
7
8
9
10
void roster::addMember(string name) {
	
	member newMember(runningID);
	runningID++;

	// ...
	
	members.push_back(newMember); // <- line 21

}


But I am receiving an error on the above-marked line 21 (highlighting the .):

 
E0304	no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=member, _Alloc=std::allocator<member>]" matches the argument list


I have no clue what I'm doing wrong, this error message makes no sense to me, and I none of the topics I found in googling helped. My C++ is super rusty and the one class I took in it was terribly taught and two years ago.
Last edited on
Hello pg1908,

Most times it is best to post the whole code if possible.

In the "addMember" function it looks like you are creating an object of a class called "member" with an overloaded ctor. This is only a guess because if it is a class then "member" should start with a capital letter. As it is my first understanding of "member" is that it is a variable and not a class.

Saying "member" is a class the question would be are you sending the correct parameters to the overloaded ctor on line 3?

Sometimes an error message that tells you the line of code that is a problem may not be where the actual error starts.


In the file that contains the "addMember" function I do not see the "#include"s that you used or how they are put in the file. For now this is only a guess at what you did.

in both pieces of code you have "// ...". This does not tell me what is missing or if it might contribute to the problem.

I need more to work with to see what all you have done.

Andy
pg1908, is it possible in your code some local variable hides/shadows members declaration?
On line 3 you have a function newMember which returns member. Newer versions of c++ allow to use {} for calling a constructor, so you can try to change () to {}:

member newMember{runningID}; // Note: {} instead of ()
More about the "C++'s most vexing parse":
https://www.fluentcpp.com/2018/01/30/most-vexing-parse/


@Andy:
We are free to name types, functions, and variables as we see fit with only some restrictions.
Capitalization of typenames is not one of them.

Yes, there is a style/convention where classname starts with capital and variables/functions do not.
That does not make std::string a bad name for a class or std::cout bad name for a (global) variable.
Their naming simply follows a different convention.
Both // ... parts hide long sections of code dealing with aspects not related to the parts causing an issue. The first one is a bunch of property declarations, none of which are used yet anywhere. The second one covers assigning values to a bunch of properties of the newMember instance, the error persisted even if this was entirely deleted.

As mentioned initially, member is a class so newMember is an instance of that class. The newMember(runningID) is indeed calling the constructor. (I had never seen the {} syntax before for calling constructors.)

All that said though, the error has mysteriously disappeared this morning. Opened the same file I closed as it was giving me the error last night and it no longer shows the error, having changed nothing. I'm going to leave this open a little while until I am sure the error is gone though.

For reference though, here is the member class header and definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class member
{
public:

	member(int);

	// bunch of yet unused properties/methods

	int getID();

private:

	int id;

	// more unused properties
};


1
2
3
4
5
6
7
member::member(int newID) {
	id = newID;
}

int member::getID() {
	return id;
}
OK another update. It seems this happens every time I use a method of the members vector. And every time closing and reopening Visual Studio makes the error disappear.

I just tried using members.erase(i) and the same error occurred and then went away after closing and reopening Visual Studio.

I am unsure now whether this is an actual issue in my code (likely?) or Visual Studio being weird.
If restarting VS is actually fixing things then the problem might be that your project is a little wonky. Possibly "rebuild all" will fix it. Maybe try manually deleting the object files and also any pre-compiled header files. As a last resort you might try copy/pasting your code into a brand-new project.
Topic archived. No new replies allowed.