error: use of ‘this’ in a constant expression while using helper funciton

Jan 14, 2019 at 12:07am
Hello everyone. I had a problem when I try to call a helper function from my a::util namespace.
My code can not be compiled: error: use of ‘this’ in a constant expression (refer to the calling of util::count();)
Do you have idea? Where should I place my helper functions. I think they do not belong to a class. Do they?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//--------a.hh----------
#include "util.hh"
namespace a {
  Class DB {
    A a;
    int foo() {
       return util::count();
    }
  }
}


//--------util.hh----------
namespace a {
namespace util {
  template <class E, class C>
  int count();
  
  templace<> int count<int, int>() {
     return 42;
  }
}
}
Last edited on Jan 14, 2019 at 12:24am
Jan 14, 2019 at 12:15am
The code you've posted is nonsense. In particular:
1. What's going on on line 3?
2. The functions in util.hh are not visible from a.hh.
Jan 14, 2019 at 8:26am
Sorry. I have changed the code little bit. Is that more helpful?
Jan 14, 2019 at 9:40am
It looks like it will not be able to deduce the template arguments so you will probably need to specify the types explicitly when calling util::count().

 
return util::count<int, int>();

Not sure if this is related to your error message or not. It's hard to say what the problem is because your code is incomplete and contains many mistakes.
Last edited on Jan 14, 2019 at 9:42am
Topic archived. No new replies allowed.