meaning of (&)() in the compilation error message

Hi all, I'm receiving this error during compilation with g++ on my Ubuntu box:


test_utils.cc:13:15: error: no matching function for call to ‘midurad::pair_list<int>::append(midurad::pair<int> (&)())’
utils.hh:91:8: note: candidate is: void midurad::pair_list<T>::append(midurad::pair<T>&) [with T = int]


I'm quite new to C++ so this might be something very trivial I have missed. What does (&)() stand for on the first line in the above error message? The offending portion of the test_utils.cc is as follows:

1
2
3
pair_list<int> pl;
pair<int> p = pair(2, 3);
pl.append(p);


and here is the utils.hh file:

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
#include <vector>

#ifndef UTILS_HH
#define UTILS_HH

namespace midurad {
  template<class T>
  class pair {
  private:
    std::vector<T> __pair;
  public:
    pair();
    pair(const T&, const T&);
    bool operator==(const pair<T>&) const;
    T const& operator[](unsigned) const;
    T& operator[](unsigned);
  };

  template<class T>
  pair<T>::pair() {
    __pair = std::vector<T>(2);
  }
  
  template<class T>
  pair<T>::pair(const T& t1, const T& t2) {
    __pair = std::vector<T>(2);
    __pair[0] = t1; __pair[1] = t2;
  }

  template<class T>
  T const& pair<T>::operator[](unsigned index) const {
    return __pair[index];
  }

  template<class T>
  T& pair<T>::operator[](unsigned index) {
    return __pair[index];
  }

  template<class T>
  bool pair<T>::operator==(const pair<T>& other) const {
    return (__pair[0] == other[0] && __pair[1] == other[1]) ? true : false;
  }

  template<class T>
  class pair_list {
    std::vector<pair<T> > __list;
  public:
    pair_list();
    bool contains(const pair<T>&) const;
    void append(const pair<T>&);
    class iterator;
    friend class iterator;
    class iterator {
      pair_list<T> __pl;
      typename std::vector<pair<T> >::iterator __it;
    public:
      iterator(pair_list& pl, bool end) {
	__pl = pl;
	if(end == true)
	  __it = __pl.__list.end();
	else
	  __it = __pl.__list.begin();
      }
      pair<T> operator++(int) {
	return __it++;
      }
      bool operator==(const iterator& other) const {
	return __it == other.__it;
      }
      bool operator!=(const iterator& other) const {
	return __it != other.__it;
      }
    };

    iterator begin() {
      return iterator(*this, false);
    }

    iterator end() {
      return iterator(*this, true);
    } 
  };
  
  template<class T>
  pair_list<T>::pair_list() {
    __list = std::vector<pair<T> >();
  }

  template<class T>
  void pair_list<T>::append(const pair<T>& p) {
    __list.push_back(p);
  }

  template<class T>
  bool pair_list<T>::contains(const pair<T>& p) const {
    for(pair_list<T>::iterator it = (*this).begin(); it != (*this).end(); it++) {
      if(*it == p) return true;
    }
    return false;
  }
}
#endif 


I'm going to look into this further, but if any of you guys spot the error I would really appreciate your help. Thanks in advance.
1
2
3
4
5
6
7
8
// this is wrong:
pair<int> p = pair(2, 3);

// it should be either this:
pair<int> p(2, 3);

// or this:
pair<int> p = pair<int>(2,3);


After making that change it compiles fine in VS.
Thank you Disch, that worked perfectly for me as well. I thought the C++ compiler would infer the template parameter from the type declaration, but I guess this is not the case, I will have to read about this more. Thanks again!
Topic archived. No new replies allowed.