0

I am working in a graph class , and i have just started to build a class for vertex , and another class for edge , my question is general not related to graph.

first i build a class its name Vertex , i didn't face any problem in implementing it so far , then i started another class its name is Edge , Edge has three main members two of them has the type Vertex , and the third member has the type unsigned int.

here is the code :

#include<iostream>
using namespace std;



class Vertex
{
 private:
     unsigned int id;                                 
 public:   
    unsigned int get_id(){return id;};   
    void set_id(unsigned int value) {id = value;};
    Vertex(unsigned int init_val) {id = init_val;};   
    ~Vertex() {};                                     
};


class Edge
{
 private:
      Vertex first_vertex;                 // a vertex on one side of the edge
      Vertex second_vertex;                // a vertex on the other side of the edge
      unsigned int weight;                 // the value of the edge ( or its weight )     
 public:   
    Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight)  //constructor
    {
         first_vertex(vertex_1.get_id());
         second_vertex(vertex_2.get_id());
         weight = init_weight;
      }

    ~ Edge();   // destructor
}; 

///////////////////////////////// this part is to test the result

Vertex ver_list[2] = {7, 9};
Vertex test = 101;

int main()
{
    cout<< "Hello, This is a graph"<< endl;
    for (unsigned int i = 0; i < 2; i++) cout<< ver_list[i].get_id() << endl;      
    cout<< test.get_id() << endl;

return 0;
}

before adding the constructor Edge the code was running without errors , after adding the constructor Edge i received errors when trying to run the code , i am not able to determine my mistake above .

Thanks for your suggestions.

here is the errors message i am receiving:

hw2.cpp: In constructor 'Edge::Edge(Vertex, Vertex, unsigned int)':
hw2.cpp:31:6: error: no matching function for call to 'Vertex::Vertex()'
      {
      ^
hw2.cpp:31:6: note: candidates are:
hw2.cpp:13:2: note: Vertex::Vertex(unsigned int)
  Vertex(unsigned int init_val) {id = init_val;};   // constructor
  ^
hw2.cpp:13:2: note:   candidate expects 1 argument, 0 provided
hw2.cpp:6:7: note: Vertex::Vertex(const Vertex&)
 class Vertex
       ^
hw2.cpp:6:7: note:   candidate expects 1 argument, 0 provided
hw2.cpp:31:6: error: no matching function for call to 'Vertex::Vertex()'
      {
      ^
hw2.cpp:31:6: note: candidates are:
hw2.cpp:13:2: note: Vertex::Vertex(unsigned int)
  Vertex(unsigned int init_val) {id = init_val;};   // constructor
  ^
hw2.cpp:13:2: note:   candidate expects 1 argument, 0 provided
hw2.cpp:6:7: note: Vertex::Vertex(const Vertex&)
 class Vertex
       ^
hw2.cpp:6:7: note:   candidate expects 1 argument, 0 provided
hw2.cpp:32:41: error: no match for call to '(Vertex) (unsigned int)'
           first_vertex(vertex_1.get_id());
                                         ^
hw2.cpp:33:42: error: no match for call to '(Vertex) (unsigned int)'
           second_vertex(vertex_2.get_id());
mazlor
  • 1,795
  • 4
  • 19
  • 20
  • which errors, specifically? – Ashalynd Oct 25 '13 at 17:25
  • 1
    Post the error message so people can actually help you better. That being said, it looks like you're trying to initialize the first_vertex and second_vertex in the body of the constructor instead of the initializer list. – Chris Cooper Oct 25 '13 at 17:26

4 Answers4

3

Maybe the problem is the syntax in your constructor:

Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight)  //constructor
{
         first_vertex(vertex_1.get_id());
         second_vertex(vertex_2.get_id());
         weight = init_weight;
}

You should initialize the members in an initializer list.

Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight) : first_vertex(vertex_1.get_id()), second_vertex(vertex_2.get_id()), weight(init_weight)
{

}

and you should pass const references to Vertex:

Edge(const Vertex& vertex_1, const Vertex& vertex_2, unsigned int init_weight)
xorguy
  • 2,594
  • 1
  • 16
  • 14
  • why do we have to pass const references to vertex..? thanks in advance. – mazlor Oct 25 '13 at 18:33
  • 1
    In this case it is not neccesary, but in cases where class instances are very big it is better to pass references, so that a temporary copy isn't created. See [this](http://stackoverflow.com/questions/2582797/why-pass-by-const-reference-instead-of-by-value) for more information – xorguy Oct 25 '13 at 18:40
1

hw2.cpp:32:41: error: no match for call to '(Vertex) (unsigned int)' first_vertex(vertex_1.get_id());

That error message is addressed below:

Edge(const Vertex& vertex_1, const Vertex& vertex_2, unsigned int init_weight)  //constructor
    : first_vertex(vertex_1.get_id()), second_vertex(vertex_2.get_id()), weight(init_weight)
  {
  }

You were trying to initialize your vertexes using their constructors inside the Edge constructor's body (instead of in it's initializer list). That is not valid syntax. You would either need to use the initializer list (show, and preferred), or use their assignment operator in the body of the constructor (syntactically correct, but not preferred as the Vertex would be constructed with incorrect data, and then initialized, instead of simply constructed with the correct data).

An even better approach would be to utilize the copy-constructor of Vertex (instead of your conversion constructor):

// notice the lack of calling the get_id function
Edge(const Vertex& vertex_1, const Vertex& vertex_2, unsigned int init_weight)
    : first_vertex(vertex_1), second_vertex(vertex_2), weight(init_weight)
  {
  }

Additionally, with the following error message:

hw2.cpp:31:6: error: no matching function for call to 'Vertex::Vertex()

You have declared a non-default, non-copy-constructor ( Vertex(unsigned int init_val)), so the compiler will not generate a default constructor for you. Since it will try to initialize a Vertex with the default constructor when you attempt to initialize first_vertex and second_vertex in the body of the Edge constructor, and it does not exist, you get an error. You can fix that by declaring a Vertex() {} constructor.

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • why do we have to pass const references to vertex..? thanks in advance. – mazlor Oct 25 '13 at 18:34
  • 1
    You do not *have* to, but it will be more efficient and will not force the usage of copy-constructors multiple times. You had two separate, but slightly related, problems. – Zac Howland Oct 25 '13 at 18:51
0

Probably this is the fix:

Edge(Vertex vertex_1, Vertex vertex_2, unsigned int init_weight)  //constructor
{
     first_vertex = Vertex(vertex_1.get_id());
     second_vertex = Vertex(vertex_2.get_id());
     weight = init_weight;
}
Ashalynd
  • 12,363
  • 2
  • 34
  • 37
0

You are trying to set your vertex values in edge the wrong way. Use something like

first_vertex.set_id(vertex_1.get_id());

Also, there are standard representations for this type of problem. See more here: http://en.wikipedia.org/wiki/Graph_%28abstract_data_type%29#Representations

Andrew Ring
  • 3,185
  • 1
  • 23
  • 28