how to use a using

Hi

I have a structure defined as an adjacency_list in the private section of a class. I would like to use adjacency_list in a print function, but I'm getting the following error:
1
2
3
4
graph.cpp: In instantiation of ‘std::ostream& operator<<(std::ostream&, const Graph<T>&) [with T = short unsigned int; std::ostream = std::basic_ostream<char>]’:
graph.cpp:21:3: error: binding ‘const adjacency_list {aka const std::map<short unsigned int, std::map<short unsigned int, short unsigned int, std::less<short unsigned int>, std::allocator<std::pair<const short unsigned int, short unsigned int> > >, std::less<short unsigned int>, std::allocator<std::pair<const short unsigned int, std::map<short unsigned int, short unsigned int, std::less<short unsigned int>, std::allocator<std::pair<const short unsigned int, short unsigned int> > > > > >}’ to reference of type ‘Graph<short unsigned int>::adjacency_list& {aka std::map<short unsigned int, std::map<short unsigned int, short unsigned int, std::less<short unsigned int>, std::allocator<std::pair<const short unsigned int, short unsigned int> > >, std::less<short unsigned int>, std::allocator<std::pair<const short unsigned int, std::map<short unsigned int, short unsigned int, std::less<short unsigned int>, std::allocator<std::pair<const short unsigned int, short unsigned int> > > > > >&}’ discards qualifiers

Compilation failed.


Any suggestions on how to use adjacency_list as a type for an argument?
If it is not possible, then how could I go about doing it?

class declaration
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
 #ifndef _GRAPH_H_
#define _GRAPH_H_

#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream
#include <cstdint>
#include <map>
#include <iomanip>
#include <vector>
#include "AVLTree.h"

int digit_count(int number) {
  int digits = 0;
  if (number < 0) digits = 1; // remove this line if '-' counts as a digit
  while (number) {
      number /= 10;
      digits++;
  }
  return digits;
}

template <typename T> class Graph;

// has to be forward declared or else shadow errors appear
template<class T> std::ostream & operator<<(std::ostream & os, const Graph<T> & g);

template <class T> class Graph : public AVLTree<T> {

  using adjacency_list = std::map< T, std::map< T, T> >;
  adjacency_list rcm; // row by col map
  adjacency_list crm; // col by row map
  int v1,v2;

  void print_map (std::ostream & os, std::map< T, std::map< T, T> > & m) const;

  public:
    Graph() = default;
    ~Graph() = default;

    void create_adjacency_list(std::istream & stm) {
      while (stm >> v1 >> v2) {
        rcm[v1]; // only rows are a key
        rcm[v1][v2] = 1; // set Row x Col

        crm[v2];
        crm[v2][v1] = 1; // set Col x Row
      }
    }

    // specialisation
    friend std::ostream & operator<< <>(std::ostream & os, const Graph<T> & g);
};


#include "graph.cpp"
#endif 

class definition
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
#include <iostream>
#include <string>

template<class T>
void Graph<T>::print_map (std::ostream & os, Graph<T>::adjacency_list & m) const {
  std::string ss = "";
  int dc = digit_count(m.rbegin()->first); // equals max number

  for (const auto & p : m) {
    os << p.first << ss.append(" ",  (dc - digit_count(p.first))) << "| ";
    for (const auto & val : p.second)
      os << val.first << "   ";
    ss = "";
    os << "\n";
  }
}

template<class T>
std::ostream & operator<<(std::ostream & os, const Graph<T> & g) {
  os << "Row by columns \n" ;
  g.print_map(os, g.rcm);
  os << "\nColumns by rows \n" ;
  g.print_map(os, g.crm);

	return os;
}

Last edited on
You will need to add a "Getter" function in the class to get whatever the private variable stores.

Or define the print function in the class (Unless you are printing more than just what is stored in the class of course)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class T> class Graph : public AVLTree<T> {

    // ...
    // void print_map (std::ostream & os, std::map< T, std::map< T, T> > & m) const;
    void print_map (std::ostream & os, const std::map< T, std::map< T, T> > & m) const;
    // ...
};

template<class T>
//void Graph<T>::print_map (std::ostream & os, Graph<T>::adjacency_list & m) const {
void Graph<T>::print_map (std::ostream & os, const Graph<T>::adjacency_list & m) const {
    
    // ...
}
Hi JBroges

thanks for the help. Works!
Topic archived. No new replies allowed.