Working with compilers for errors-Adjacency matrix

Errors:
So I have errors that I need help fixing for c++11. This deals with undirected graph in the format of adjacency matrix.
prompt:
Write a C++ 11 function named is_connected() to determine whether an undirected unweighted graph is connected when the graph is stored in the adjacency matrix format.
Write a main program that hard-codes the entries of the adjacency matrix and then passes the matrix to the is_connected function. Every graph has vertices labeled with consecutive unsigned integers starting at 0.
So I have those done already, and I need help with these compiler issues that make no sense. One of them is I already included the packages for them
Errors:
In file included from lin_0912.cpp:7:0:
lin_0912.h: In function ‘bool is_connected(Matrix&)’:
lin_0912.h:129:6: error: ‘vector’ was not declared in this scope
vector< bool >reached(graph.size(),false);
^
lin_0912.h:129:6: note: suggested alternative:
In file included from /usr/include/c++/4.8/vector:64:0,
from lin_0912.cpp:4:
/usr/include/c++/4.8/bits/stl_vector.h:210:11: note: ‘std::vector’
class vector : protected _Vector_base<_Tp, _Alloc>
^
In file included from lin_0912.cpp:7:0:
lin_0912.h:129:14: error: expected primary-expression before ‘bool’
vector< bool >reached(graph.size(),false);
^
lin_0912.h:129:14: error: expected ‘;’ before ‘bool’
lin_0912.h:130:7: error: ‘queue’ was not declared in this scope
queue< uint8_t > to_be_explored;
^
lin_0912.h:130:7: note: suggested alternative:
In file included from /usr/include/c++/4.8/queue:64:0,
from lin_0912.cpp:5:
/usr/include/c++/4.8/bits/stl_queue.h:93:11: note: ‘std::queue’
class queue
^
In file included from lin_0912.cpp:7:0:
lin_0912.h:130:22: error: expected primary-expression before ‘>’ token
queue< uint8_t > to_be_explored;
^
lin_0912.h:130:24: error: ‘to_be_explored’ was not declared in this scope
queue< uint8_t > to_be_explored;
^
lin_0912.h:137:9: error: ‘reached’ was not declared in this scope
reached.at(current_node)=true;
^
lin_0912.h:146:19: error: ‘reached’ was not declared in this scope
return find(reached.begin(), reached.end(), false) == reached.end();
^
lin_0912.h:146:56: error: ‘find’ was not declared in this scope
return find(reached.begin(), reached.end(), false) == reached.end();
^
lin_0912.h:146:56: note: suggested alternative:
In file included from /usr/include/c++/4.8/algorithm:62:0,
from lin_0912.h:9,
from lin_0912.cpp:7:
/usr/include/c++/4.8/bits/stl_algo.h:4432:5: note: ‘std::find’
find(_InputIterator __first, _InputIterator __last,
^
lin_0912.cpp: In function ‘int main()’:
lin_0912.cpp:23:28: error: invalid initialization of reference of type ‘Matrix&’ from expression of type ‘Matrix’
cout< ^
In file included from lin_0912.cpp:7:0:
lin_0912.h:126:9: error: in passing argument 1 of ‘bool is_connected(Matrix&)’
bool is_connected(Matrix & graph)
^
lin_0912.h: In function ‘bool is_connected(Matrix&)’:
lin_0912.h:147:4: warning: control reaches end of non-void function [-Wreturn-type]
}

.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <cstdint>
#include <vector>
#include <queue>
#include <iostream>
#include "lin_0912.h"
using namespace std;

int main()
{
   Matrix <bool> graph(6,6);
   
   
   graph.at(0, 1)=true;
   graph.at(0,2)=true;
   graph.at(2,0)=true;
   graph.at(2,1)=true; 
   graph.at(3,4)=true;
   graph.at(3,5)=true;
   graph.at(4,3)=true;
   graph.at(5,3)=true;
   cout<<is_connected(graph)<<endl;
   return 0;
}

.h:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>

#include <cstdint>
#include <queue>
#include <vector>
#include <algorithm>
/**
 * A generic 2-dimensional array class
 * @author Jon Beck
 * @version 8 September 2017
 */
template <class Object>
class Matrix
{
 public:
  /**
   * Constructor, specifying number of both rows and columns
   * @param rows the number of rows
   * @param cols the number of columns
   */
  Matrix( uint16_t rows, uint16_t cols );

  /**
   * Access to an element to allow modification
   * @param row the row index
   * @param col the column index
   * @return reference to the element at that position
   */
  Object & at( uint16_t row, uint16_t col );

  /**
   * Constant access to an element
   * @param row the row index
   * @param col the column index
   * @return constant reference to the element at that position
   */
  const Object & at( uint16_t row, uint16_t col ) const;

  /**
   * Destructor to free allocated memory
   */
  ~Matrix();

  /**
   * Copy constructor to make 1-1 copy of existing matrix
   * @param m the existing matrix to be copied
   */
  Matrix( const Matrix<Object> & m ); // Copy constructor

  /**
   * Disallow the rvalue copy constructor
   */
  Matrix( const Matrix<Object> && m ) = delete;

  /**
   * Assignment operator to make 1-1 copy of existing matrix
   * @param m the existing matrix to be copied
   */
  Matrix & operator= ( const Matrix<Object> & m ); // Assignment operator

  /**
   * Disallow the rvalue assignment operator
   */
  Matrix & operator= ( const Matrix<Object> && m ) = delete;

  /**
   * Accessor to determine how many rows are in the matrix
   * @return the number of rows in the matrix
   */
  uint16_t numrows() const;

  /**
   * Accessor to determine how many columns are in the matrix
   * @return the number of columns in the matrix
   */
  uint16_t numcols() const;

 private:
  uint16_t rows;
  uint16_t cols;
  Object* data;
};

template <class Object>
Matrix<Object>::Matrix( uint16_t rows, uint16_t cols )
: rows( rows ), cols( cols )
{
  data = new Object[ rows * cols ];
}

template <class Object>
Matrix<Object>::~Matrix()
{
  delete[] data;
}

template <class Object>
Object & Matrix<Object>::at( uint16_t row, uint16_t col )
{
  assert( row < rows && col < cols );
  return data[ cols * row + col ];
}

template <class Object>
const Object & Matrix<Object>::at( uint16_t row, uint16_t col ) const
{
  assert( row < rows && col < cols );
  return data[ cols * row + col ];
}

template <class Object>
uint16_t Matrix<Object>::numrows() const
{
  return rows;
}

template <class Object>
uint16_t Matrix<Object>::numcols() const
{
  return cols;
}

   bool is_connected(Matrix<uint8_t> & graph)
   {
    
     vector< bool >reached(graph.size(),false);
      queue< uint8_t > to_be_explored; 
      to_be_explored.push(0);

     while(!to_be_explored.empty())
      {
        uint8_t current_node=to_be_explored.front();
        to_be_explored.pop();
        reached.at(current_node)=true;
        for(uint8_t adjacent=0; adjacent<graph.numcols(); ++adjacent)
        {
           if(!reached.at(adjacent) && graph.at(current_node, adjacent))
           {
              to_be_explored.push(adjacent);
           } 
        }
      }
      return find(reached.begin(), reached.end(), false) == reached.end();
   }
   

#endif 
Last edited on
<Secret name>.h:
line 129-130:
1
2
std::vector< bool >reached(graph.size(), false); // must be std::vector
std::queue< uint8_t > to_be_explored; // must be std::queue 


Again, line 129:
vector< bool >reached(graph.size(), false);
Where do you define Matrix::size()?

Line 62:
Matrix & operator= ( const Matrix<Object> & m ); // Assignment operator
You declare a non defaulted version of overloaded operator=. Where do you define it?
Ok So I needed to just ad using namespace std; in header file.

remaining errors:
In file included from lin_0912.cpp:7:0:
lin_0912.h: In function ‘bool is_connected(Matrix<unsigned char>&)’:
lin_0912.h:130:34: error: ‘class Matrix<unsigned char>’ has no member named ‘size’
vector< bool >reached(graph.size(),false);
^
lin_0912.cpp: In function ‘int main()’:
lin_0912.cpp:23:28: error: invalid initialization of reference of type ‘Matrix<unsigned char>&’ from expression of type ‘Matrix<bool>’
cout<<is_connected(graph)<<endl;
^
In file included from lin_0912.cpp:7:0:
lin_0912.h:127:9: error: in passing argument 1 of ‘bool is_connected(Matrix<unsigned char>&)’
Last edited on
Topic archived. No new replies allowed.