I am working on worm_sim simulater , ubuntu, gcc, codeblocks IDE
我正在使用worm_sim仿真器,ubuntu, gcc, codeblocks IDE。
traffic_source.h file
traffic_source。h文件
class Traffic_source : public Buffer_owner, public Connector, public Addressee{
private:
static unsigned int id_base;
unsigned int id;
unsigned int packet_size;
unsigned int flit_size;
double packet_generating_rate;
int pkt_id;
traffic_source_state ts_state;
double* packet_to_destination_rate;
Traffic_mode traffic_mode;
int period; // period for packet generation using trace_file
ifstream trace_file;
int trace_file_loop_cnt; // how many times we have gone over the trace file so far
bool trace_file_empty;
ofstream trace_dump; // trace file to dump out
typedef struct Message {
int timestamp;
unsigned int destination;
unsigned int size;
} Message, *pMessage;
Message pre_fetched_message;
bool get_next_message(Message & msg);
unsigned int get_destination_uniform(void) const;
unsigned int get_destination_transpose1(void) const;
unsigned int get_destination_transpose2(void) const;
unsigned int get_destination_hotspot(void) const;
unsigned int get_destination_customized(void) const;
void generate_a_packet(unsigned int dst_id);
void generate_packets(const Message & rec);
public:
Traffic_source(Position p, int buf_sz);
~Traffic_source();
bool can_send(void) const;
bool can_receive(void) const { return false; }
bool send(void);
bool receive(class Flit * a_flit) { return false; }
class Connector * get_receiver(void) const;
static void reset_id_base(void) { id_base = 0; }
void tick(void);
/* traffic control routines */
void set_packet_generating_rate(double r);
void set_packet_to_destination_rate(unsigned int dst_id, double rate);
double get_packet_to_destination_rate(unsigned int dst_id) const;
double get_total_packet_injection_rate(void) const;
int set_trace_file(char * file_name);
bool has_trace_file(void) { return (trace_file.is_open()); }
int get_id(void) const { return id; }
};
traffic_source.cpp
traffic_source.cpp
Traffic_source::Traffic_source(Position p, int buf_sz) : Buffer_owner(buf_sz), Addressee(p) {
id = id_base ++;
packet_generating_rate = param.packet_generating_rate;
packet_size = param.flits_per_packet;
flit_size = param.flit_size;
traffic_mode = param.traffic_mode;
period = 0;
packet_to_destination_rate = 0;
pkt_id = 0;
ts_state = OFF_
if (param.dump_traffic_source_trace) {
char file_name[20];
sprintf(file_name, "%d.trace", id);
trace_dump.open(file_name);
if (!trace_dump.is_open() || !trace_dump.good()) {
cerr << "Error in opening file " << file_name << " for trace dumping" << endl;
exit(-1);
}
trace_dump << "PERIOD\t" << param.simulation_length << endl;
trace_dump << "#Trace file dumped by worm_sim from node " << id << endl;
trace_dump << "#Folloing lines are with format as:" << endl
<< "#timestamp\t" << "destination\t" << "message_size(bits):" << endl;
}
}
bool Traffic_source::can_send(void) const
{
int router_id=get_id();
unsigned int local_availability;
pRouter a_router= param.network->get_router(router_id);
local_availability=a_router->get_port_availability(0);
//cout<<local_availability<<endl;
if (buffer.is_empty())
return false;
if(local_availability <= 0)
{
packet_generating_rate = 0; //error: assignment of member ‘Traffic_source::packet_generating_rate’ in read-only object|
set_packet_generating_rate(0); // error: passing ‘const Traffic_source’ as ‘this’ argument of ‘void Traffic_source::set_packet_generating_rate(double)’ discards qualifiers [-fpermissive]|
return false;
}
// This is somehow trick, we need to verify whether the first flit in the fifo
// is received right in this clock cycle. If so, we can not send it
const Flit * first_flit = buffer.peek_flit();
if (first_flit->arrived_in_this_cycle())
return false;
pConnector receiver = get_receiver();
if (receiver)
return receiver->can_receive();
else
return false;
}
the value packet_generating_rate is not const but when i try to modify it either directly or using the set function it give me errors
packet_generating_rate的值不是const,但是当我试图直接或使用set函数修改它时,它会给我带来错误
packet_generating_rate = 0; //error: assignment of member
‘Traffic_source::packet_generating_rate’ in read-only object|
set_packet_generating_rate(0); // error: passing ‘const Traffic_source’ as ‘this’ argument of ‘void Traffic_source::set_packet_generating_rate(double)’ discards qualifiers [-fpermissive]|
although it is used on other files with no problem, any suggestion plz
虽然它在其他文件上使用没有问题,但是有任何建议
4 个解决方案
#1
18
bool Traffic_source::can_send(void) const
As other's have already pointed out, the problem is that inside a const
function (last const
in the line) you cannot modify the members of the object. Effectively the member function is translated into something similar to: bool Traffic_source__can_send( const Traffic_source* this, void )
, there the this
argument is a pointer to const
. Which in turn means that packet_generating_rate
is const
in the context of the function.
正如其他人已经指出的,问题是在const函数(行中的最后一个const)中,您不能修改对象的成员。有效地,成员函数被转换成类似于:bool Traffic_source__can_send(const Traffic_source* this, void),这里的参数是指向const的指针。这又意味着packet_generating_rate在函数的上下文中是常量。
There are three alternatives that you can follow here:
这里有三种选择:
- don't modify the member
- 不要修改成员
- don't mark the function
const
- 不要把函数标记为const
- make
packet_generating_rate
mutable
- 使packet_generating_rate可变
The first two options are the common ones: either the function is const
and does not modify the object, or it is not const
and can modify the object. There are cases however, where you want to modify a member within a const
member pointer. In that case you can mark the member declaration as mutable
to enable modification inside const
member functions.
前两个选项是常见的选项:要么函数是const,不修改对象,要么它不是const,可以修改对象。但是,在某些情况下,您希望在const成员指针中修改成员。在这种情况下,您可以将成员声明标记为可变的,以便在const成员函数中启用修改。
Note however that in general this is done when the member variable does not take part on the visible state of the object. For example a mutex
variable does not change the value returned from a getter or the state of the object afterwards, but getters need to lock (modify) the object to obtain a consistent view of the object in multithreaded environments. The second typical example is a cache, where an object may offer an operation that is expensive to calculate, so the function performing that operation might cache the result for later. Again, whether the value is recalculated or retrieved from the cache it will be the same, so the visible state of the object does not change. Finally, sometimes you might need to abuse the construct to conform to an existing interface.
但是,请注意,通常是在成员变量不参与对象的可见状态时完成的。例如,互斥量变量不会改变从getter返回的值或之后对象的状态,但是getter需要锁定(修改)对象以获得多线程环境中对象的一致视图。第二个典型的例子是缓存,在缓存中,对象可能提供计算代价高昂的操作,因此执行该操作的函数可能会缓存稍后的结果。同样,无论该值是重新计算还是从缓存中检索,它都是相同的,因此对象的可见状态不会改变。最后,有时可能需要滥用构造以符合现有接口。
Now it is up to you to determine which of the three options to apply to your design. If you need to modify the member attribute, then either the member is part of the visible state and the function should not be const
, or else it is not part of the state of the object and can be marked mutable
.
现在由您来决定应用于您的设计的三个选项中的哪一个。如果需要修改成员属性,那么要么成员是可见状态的一部分,函数不应该是const,要么它不是对象状态的一部分,可以标记为mutableck。
#2
3
bool Traffic_source::can_send(void) const
this declarations turns this
into a pointer to a const
. Marking a method as const
makes the instance immutable, so you can't modify its members.
此声明将其转换为指向const的指针。将方法标记为const会使实例不可变,因此不能修改其成员。
Why did you mark it as const
in the first place, if you're going to modify members?
如果你要修改成员,为什么要把它标记为const呢?
Also, to me it seems that can_send
has getter semantics, so logically it shouldn't modify members (I think the error here is that you attempt to modify packet_generating_rate
, not making the method const
.
而且,对我来说,can_send似乎有getter语义,所以逻辑上它不应该修改成员(我认为这里的错误是您试图修改packet_generating_rate,而不是使方法const。
#3
2
packet_generating_rate = 0;
It is used inside a constant function. In constant function, you cannot change the value of any data member of the object, on which the function was called.
它用在常数函数中。在constant函数中,不能更改对象中调用该函数的任何数据成员的值。
#4
0
A const member function such as this
一个像这样的const成员函数
bool Traffic_source::can_send(void) const
is disallowed from modifying any member variables of that class. As you modify a member variable inside this function, this is why you get the error. Make the function non-const and your will not get this error.
不允许修改该类的任何成员变量。当您在这个函数中修改一个成员变量时,这就是您得到错误的原因。使函数不const,您将不会得到这个错误。
#1
18
bool Traffic_source::can_send(void) const
As other's have already pointed out, the problem is that inside a const
function (last const
in the line) you cannot modify the members of the object. Effectively the member function is translated into something similar to: bool Traffic_source__can_send( const Traffic_source* this, void )
, there the this
argument is a pointer to const
. Which in turn means that packet_generating_rate
is const
in the context of the function.
正如其他人已经指出的,问题是在const函数(行中的最后一个const)中,您不能修改对象的成员。有效地,成员函数被转换成类似于:bool Traffic_source__can_send(const Traffic_source* this, void),这里的参数是指向const的指针。这又意味着packet_generating_rate在函数的上下文中是常量。
There are three alternatives that you can follow here:
这里有三种选择:
- don't modify the member
- 不要修改成员
- don't mark the function
const
- 不要把函数标记为const
- make
packet_generating_rate
mutable
- 使packet_generating_rate可变
The first two options are the common ones: either the function is const
and does not modify the object, or it is not const
and can modify the object. There are cases however, where you want to modify a member within a const
member pointer. In that case you can mark the member declaration as mutable
to enable modification inside const
member functions.
前两个选项是常见的选项:要么函数是const,不修改对象,要么它不是const,可以修改对象。但是,在某些情况下,您希望在const成员指针中修改成员。在这种情况下,您可以将成员声明标记为可变的,以便在const成员函数中启用修改。
Note however that in general this is done when the member variable does not take part on the visible state of the object. For example a mutex
variable does not change the value returned from a getter or the state of the object afterwards, but getters need to lock (modify) the object to obtain a consistent view of the object in multithreaded environments. The second typical example is a cache, where an object may offer an operation that is expensive to calculate, so the function performing that operation might cache the result for later. Again, whether the value is recalculated or retrieved from the cache it will be the same, so the visible state of the object does not change. Finally, sometimes you might need to abuse the construct to conform to an existing interface.
但是,请注意,通常是在成员变量不参与对象的可见状态时完成的。例如,互斥量变量不会改变从getter返回的值或之后对象的状态,但是getter需要锁定(修改)对象以获得多线程环境中对象的一致视图。第二个典型的例子是缓存,在缓存中,对象可能提供计算代价高昂的操作,因此执行该操作的函数可能会缓存稍后的结果。同样,无论该值是重新计算还是从缓存中检索,它都是相同的,因此对象的可见状态不会改变。最后,有时可能需要滥用构造以符合现有接口。
Now it is up to you to determine which of the three options to apply to your design. If you need to modify the member attribute, then either the member is part of the visible state and the function should not be const
, or else it is not part of the state of the object and can be marked mutable
.
现在由您来决定应用于您的设计的三个选项中的哪一个。如果需要修改成员属性,那么要么成员是可见状态的一部分,函数不应该是const,要么它不是对象状态的一部分,可以标记为mutableck。
#2
3
bool Traffic_source::can_send(void) const
this declarations turns this
into a pointer to a const
. Marking a method as const
makes the instance immutable, so you can't modify its members.
此声明将其转换为指向const的指针。将方法标记为const会使实例不可变,因此不能修改其成员。
Why did you mark it as const
in the first place, if you're going to modify members?
如果你要修改成员,为什么要把它标记为const呢?
Also, to me it seems that can_send
has getter semantics, so logically it shouldn't modify members (I think the error here is that you attempt to modify packet_generating_rate
, not making the method const
.
而且,对我来说,can_send似乎有getter语义,所以逻辑上它不应该修改成员(我认为这里的错误是您试图修改packet_generating_rate,而不是使方法const。
#3
2
packet_generating_rate = 0;
It is used inside a constant function. In constant function, you cannot change the value of any data member of the object, on which the function was called.
它用在常数函数中。在constant函数中,不能更改对象中调用该函数的任何数据成员的值。
#4
0
A const member function such as this
一个像这样的const成员函数
bool Traffic_source::can_send(void) const
is disallowed from modifying any member variables of that class. As you modify a member variable inside this function, this is why you get the error. Make the function non-const and your will not get this error.
不允许修改该类的任何成员变量。当您在这个函数中修改一个成员变量时,这就是您得到错误的原因。使函数不const,您将不会得到这个错误。