我想我想要做的事情是不可能的,但我有这个代码:
22 class Q
21 {
22 int integer;
23 int fractional;
24 int word;
25 public:
26 Q(int i,int f) { this->integer = i; this->fractional = f; this->word = i+f;}
27 const int get_i() {return (const int)this->integer;}
28 const int get_f() {return (const int)this->fractional;}
29 const int get_w() {return (const int)this->word;}
30
31 friend ostream& operator<<(ostream& os, const Q& q){ os << "Q" << q.integer << "." << q.fractional << " (w:" << q.word << ")"; return os; }
32 };
33
34
35 const Q Q1_i = Q(1,10);
36 const Q Q1_o = Q(0,11);
37
38 const std::array<Q,1> input_queue_q = {Q1_i};
39 const std::array<Q,1> output_queue_q = {Q1_o};
40
41
42 int sc_main(int argc, char *argv[])
43 {
44 ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
45
46 for(const Q &input_q : input_queue_q)
47 {
48 for(const Q &output_q : output_queue_q)
49 {
50 ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
51 }
52 }
53 }
当我尝试编译时出现以下错误:
check_ac_one_over.cpp: In function ‘int sc_main(int, char**)’:
check_ac_one_over.cpp:44:23: error: passing ‘const Q’ as ‘this’ argument of ‘const int Q::get_w()’ discards qualifiers [-fpermissive]
ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
^
check_ac_one_over.cpp:44:23: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:44:23: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:44:46: note: in template argument for type ‘int’
ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
^
check_ac_one_over.cpp:44:51: error: invalid type in declaration before ‘;’ token
ac_fixed<Q1_i.get_w(),1,false,AC_TRN,AC_SAT> mem;
^
check_ac_one_over.cpp:50:30: error: passing ‘const Q’ as ‘this’ argument of ‘const int Q::get_w()’ discards qualifiers [-fpermissive]
ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
^
check_ac_one_over.cpp:50:30: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:50:30: error: call to non-constexpr function ‘const int Q::get_w()’
check_ac_one_over.cpp:50:53: note: in template argument for type ‘int’
ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
^
check_ac_one_over.cpp:50:58: error: invalid type in declaration before ‘;’ token
ac_fixed<input_q.get_w(),1,false,AC_TRN,AC_SAT> mem;
^
make: *** [check_ac_one_over.o] Error 1
我想问题是gcc认为Q1_i.get_w()可以改变。有没有办法编译这段代码?我想使用我在上面定义的类来迭代并使用该模板化类型。
干杯, 斯特凡诺。
答案 0 :(得分:4)
您需要使代码“const correct”。目前它是:
const int get_w() {return (const int)this->word;}
将值const
返回是没用的,因为对值的修改不会反映在其他任何地方。但是你没有制作方法const
,这意味着它不会改变this
。它应该是:
int get_w() const {return word;}
括号后面的const
表示该方法不会修改任何成员变量(标记为mutable
的变量除外,通常都没有)。