Passing Objects As Function Parameters

My research has not helped me solve this one.
edge.h:
1
2
3
4
5
6
7
8
9
class Edge
{
	private:
		Vertex start;
		Vertex end;
		int weight;
	public:
		Edge(Vertex&, Vertex&, int); //different failed versions include "const Vertex&", "Vertex*", and "Vertex"
};

edge.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector>
#include <string>
#include "vertex.h"
#include "edge.h"

Edge::Edge(Vertex& vStart, Vertex& vEnd, int eWeight)//different failed versions include "const Vertex&", "Vertex*", and "Vertex"
{
	start=vStart;
	end=vEnd;
	weight=eWeight;
	vStart.add_adjacent(vEnd);
	vEnd.add_adjacent(vStart);
}

main.cpp:
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
#include <iostream>
#include <vector>
#include <string>

#include "vertex.h"
#include "edge.h"
#include "graph.h"

int main(int argc, char** argv)
{
	std::cout << "Vertices Adjacent To:" << std::endl;
	Graph main;
	Vertex a("A");
	Vertex b("B");
	Vertex c("C");
	Vertex d("D");
	main.add_vertex(a);
	main.add_vertex(b);
	main.add_vertex(c);
	main.add_vertex(d);
	Edge a_to_b(a, b, 10);
	Edge b_to_c(b, c, 10);
	Edge c_to_d(c, d, 10);
	Edge d_to_a(d, a, 11);
//... 


I get a bunch of errors relating to this line in edge.cpp:
Edge::Edge(Vertex& vStart, Vertex& vEnd, int eWeight)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
edge.cpp    In constructor 'Edge::Edge(Vertex&, Vertex&, int)':
//errors for first Vertex& param:
edge.cpp    [Error] no matching function for call to 'Vertex::Vertex()'
edge.cpp    [Note] candidates are:
edge.cpp    In file included from edge.cpp
vertex.h    [Note] Vertex::Vertex(std::string)
vertex.h    [Note] candidate expects 1 argument, 0 provided
vertex.h    [Note] Vertex::Vertex(const Vertex&)
vertex.h    [Note] candidate expects 1 argument, 0 provided
//errors for second Vertex& param:
edge.cpp    [Error] no matching function for call to 'Vertex::Vertex()'
edge.cpp    [Note] candidates are:
edge.cpp    In file included from edge.cpp
vertex.h    [Note] Vertex::Vertex(std::string)
vertex.h    [Note] candidate expects 1 argument, 0 provided
vertex.h    [Note] Vertex::Vertex(const Vertex&)
vertex.h    [Note] candidate expects 1 argument, 0 provided


Any suggestions?

~Hom
Last edited on
You should generally have a default constructor for your classes, which is a constructor that requires no arguments.

Your class has only one constructor, and it requires arguments. Make sure you have both types of constructors:

1
2
3
4
5
6
7
8
9
10
class Edge
{
	private:
		Vertex start;
		Vertex end;
		int weight;
	public:
		Edge();  // 1
		Edge(const Vertex&, const Vertex&, int);  // 2
};

Ctor 1 makes a default edge. (Use default vertices and a weight of zero, else whatever your assignment lists as appropriate.)

Ctor 2 makes an edge according to the arguments.

Note, it is possible to make this one and the same function by providing default arguments, but that is usually not what you want to do. Simply make yourself two constructors.

Hope this helps.
That did it! Thanks!
There is another solution to your particular problem.

Your Edge class isn't using the constructor list to intialize its members, as it should do.

As you don't use the constructor list the two Vertex member variables (the two vertices -- vStart and vEnd) are being initialized by their default constructor (when you provide one, as Duoas suggested) and then assigned to in the constructor body to the values of the passed in parameters.

If you switch to using the constructor initializer list like this:

1
2
3
4
5
6
7
8
9
10
Edge::Edge(Vertex& vStart, Vertex& vEnd, int eWeight)
: start(vStart), end(vEnd), weight(eWeight) // init members in constructor init list
{
    // don't assign here
    //start=vStart;
    //end=vEnd;
    //weight=eWeight;
    vStart.add_adjacent(vEnd);
    vEnd.add_adjacent(vStart);
}


then it's the Vertex(const Vertex&); form off the constructor that's used instead. In this case you can do without a default constructor for your Vertex class, at least in the code you've posted above.

I don't know the details of the rest of your program, but it is a good idea in general to provide default constructors. But there are situations where a particular class is invalid without some kind of initialization information, and the initializer list is part of how you deal with that case.

Andy

PS A pet peeve of mine!

I would rather see

Edge(Vertex& vStart, Vertex& vEnd, int eWeight);

than

Edge(Vertex&, Vertex&, int);

in your header edge.h

You see, I get narked when digging through codebases (not always using a smart IDE) where I have to grovel round to find out what a parameter means. The header to a class should make it absolutely clear what the parameters are, without having to go and find the .cpp file (which might not even be about if I've been given a header and a .LIB file.)

Last edited on
Topic archived. No new replies allowed.