我正在研究worm_sim simulater,ubuntu,gcc,codeblocks IDE
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::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;
}
值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]|
虽然它在其他文件上使用没有问题,但任何建议都是plz
答案 0 :(得分:20)
bool Traffic_source::can_send(void) const
正如其他人已经指出的那样,问题是在const
函数内(行中的最后const
),您无法修改对象的成员。实际上,成员函数被转换为类似于bool Traffic_source__can_send( const Traffic_source* this, void )
的内容,this
参数是指向const
的指针。这反过来意味着在函数的上下文中packet_generating_rate
const
。
您可以在此处遵循三种选择:
const
packet_generating_rate
mutable
前两个选项是常见选项:函数是const
并且不修改对象,或者它不是const
并且可以修改对象。但是,在某些情况下,您希望修改const
成员指针中的成员。在这种情况下,您可以将成员声明标记为mutable
,以便在const
成员函数中进行修改。
但是请注意,通常这是在成员变量不参与对象的可见状态时完成的。例如,mutex
变量不会更改从getter返回的值或之后的对象状态,但getter需要锁定(修改)对象以获得多线程环境中对象的一致视图。第二个典型示例是缓存,其中对象可能提供计算成本高昂的操作,因此执行该操作的函数可能缓存结果以供日后使用。同样,无论是重新计算值还是从缓存中检索它,它都是相同的,因此对象的可见状态不会改变。最后,有时您可能需要滥用构造以符合现有接口。
现在由您决定应用于您的设计的三个选项中的哪一个。如果您需要修改成员属性,那么该成员是可见状态的一部分而该函数不应该是const
,否则它不是该对象状态的一部分并且可以标记为{{1 }}
答案 1 :(得分:3)
bool Traffic_source::can_send(void) const
此声明将this
转换为指向const
的指针。将方法标记为const
会使实例不可变,因此您无法修改其成员。
如果您要修改成员,为什么首先将其标记为const
?
另外,对我来说,似乎can_send
具有getter语义,所以逻辑上它不应该修改成员(我认为这里的错误是你试图修改packet_generating_rate
,而不是方法{ {1}}。
答案 2 :(得分:2)
packet_generating_rate = 0;
在常量函数中使用。在常量函数中,您不能更改调用该函数的对象的任何数据成员的值。
答案 3 :(得分:0)
const成员函数,例如
bool Traffic_source::can_send(void) const
不允许修改该类的任何成员变量。当您在此函数中修改成员变量时,这就是您收到错误的原因。使函数非常量,你不会得到这个错误。