expected unqualified-id

When I try to compile this code..
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
#include <string>
#include <vector>
#include <memory>

struct Time {
   int duration;
   virtual std::string operator()() const = 0;
   Time( int d) : duration{d} {};
   virtual ~Time() {};
};
struct Seconds : Time {
    Seconds( int d ) : Time::Time{d} {}
    std::string operator()() const override {
        if( duration == 0) return "";
        if( duration == 1) return "1 second";
        return std::to_string(duration) + " seconds";
    }
};
struct Minutes : Time {
    Minutes( int d ) : Time::Time{d} {}
    std::string operator()() const override {
        if( duration == 0) return "";
        if( duration == 1) return "1 minute";
        return std::to_string(duration) + " minutes";
    }
};
struct Hours : Time {
   Hours( int d ) : Time::Time{d} {}
   std::string operator()() const override {
       if( duration == 0) return "";
       if( duration == 1) return "1 hour";
       return std::to_string(duration) + " hours";
    }
};
struct Days : Time {
    Days( int d ) : Time::Time{d} {}
    std::string operator()() const override {
        if( duration == 0) return "";
        if( duration == 1) return "1 day";
        return std::to_string(duration) + " days";
    }
};
struct Years : Time {
    Years( int d ) : Time::Time{d} {}
    std::string operator()() const override {
        if( duration == 0) return "";
        if( duration == 1) return "1 year";
        return std::to_string(duration) + " years";
    }
};

std::string format_duration(int sec)
{  
  if( sec = 0) return "now";

  constexpr int s_per_min = 60;
  constexpr int s_per_hour = s_per_min * 60;
  constexpr int s_per_day = s_per_hour * 24;
  constexpr int s_per_year = s_per_day * 356;
  
  
  std::vector<std::unique_ptr<Time>> time;
  time.push_back(std::unique_ptr<Time>(new Years( sec / s_per_year)));
  time.push_back(std::unique_ptr<Time>(new Hours( sec % s_per_year / s_per_day)));
  time.push_back(std::unique_ptr<Time>(new Days( sec % s_per_day / s_per_hour)));
  time.push_back(std::unique_ptr<Time>(new Minutes( sec % s_per_hour / s_per_min)));
  time.push_back(std::unique_ptr<Time>(new Seconds( sec % s_per_min)));
  
  int time_length {0};
  for( auto & unit : time ) if( unit->duration != 0) ++time_length;
  
  std::string result;
  for( int idx = 0;  idx < time.size();  ++idx )
  {
      if( time_length == 1 && time[idx]->duration != 0 ) 
      {
          result += time[idx]->();
          --time_length;
      }
      else if( time_length == 2 && time[idx]->duration != 0) 
      {
          result += time[idx]->() + " and ";
          --time_length;
      }
      else if( time_length >= 3 && time[idx]->duration != 0) 
      {
          result += time[idx]->() + ", ";
          --time_length;
      }
  }
  return result;
}
#include <iostream>
int main()
{
    std::cout << format_duration(0) << std::endl;
}

... I get this compiler errors:
1
2
3
4
5
6
7
8
9
10
duration.cpp: In function ‘std::__cxx11::string format_duration(int)’:
duration.cpp:77:32: error: expected unqualified-id before ‘(’ token
           result += time[idx]->();
                                ^
duration.cpp:82:32: error: expected unqualified-id before ‘(’ token
           result += time[idx]->() + " and ";
                                ^
duration.cpp:87:32: error: expected unqualified-id before ‘(’ token
           result += time[idx]->() + ", ";
                                ^

What's wrong with and what's an unqualified-id?
You get some warnings, too. Line 54 above, you use = when you mean ==.
Also, idx (line 73) is better as a size_t (or at least unsigned).

As for trying to call operator(), you can't say time[idx]->(). Try time[idx]->operator()(). (It also seems to work if you say (*time[idx])(), but I'm not sure if that's correct with virtual functions.)

And you seem to have your Hours and Days mixed up in the push_backs.
Last edited on
Thank you dutch! I've fixed that.

Maybe someone could explain why the -> operator not works, and in generally, what a qualified-id means.
"expected unqualified-id" is just complaining about a missing name between -> and (). Syntactially, you can say p->f() but can't say p->(), even if operator() has been defined for p's class.

A qualified id is one that specifies a scope using ::. Such an id would be a syntax error at that point (e.g., p->::f() is an error). So it mentions an "unqualified" id, but a friendlier error message would just say "missing method name after ->".

BTW, now that I think about it, (*time[idx])() should be okay since p->f() is just "syntactic sugar" for (*p).f().
Topic archived. No new replies allowed.