2012-09-03 15 views
6

मैं worm_sim simulater पर काम कर रहा हूँ, ubuntu, जीसीसी, codeblocks आईडीईत्रुटि में केवल पढ़ने के लिए आपत्ति

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 const नहीं है, लेकिन जब मैं इसे सीधे संशोधित करने या सेट फ़ंक्शन का उपयोग करने का प्रयास करता हूं तो यह मुझे त्रुटियां देता है

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

उत्तर

3
bool Traffic_source::can_send(void) const 

इस घोषणाओं एक const के लिए सूचक में this बदल जाता है। const के रूप में एक विधि को चिह्नित करना उदाहरण को अपरिवर्तनीय बनाता है, ताकि आप इसके सदस्यों को संशोधित नहीं कर सकें।

यदि आप सदस्यों को संशोधित करने जा रहे हैं, तो आप इसे पहले स्थान पर const क्यों चिह्नित करते हैं?

इसके अलावा, मेरे लिए यह लगता है कि can_send गेटर अर्थ विज्ञान है, तो तार्किक रूप से यह सदस्यों को संशोधित नहीं किया जाना चाहिए (मुझे लगता है कि यहाँ त्रुटि है कि आप packet_generating_rate, विधि const नहीं कर संशोधित करने का प्रयास है।

0

एक स्थिरांक सदस्य समारोह इस तरह के रूप में

bool Traffic_source::can_send(void) const 

उस वर्ग के किसी भी सदस्य चर को संशोधित करने से अनुमति नहीं है। आप इस समारोह के अंदर एक सदस्य चर को संशोधित रूप में, यह तुम क्यों त्रुटि मिलती है। समारोह गैर स्थिरांक और अपनी इच्छा बनाओ यह त्रुटि नहीं मिली।

2
packet_generating_rate = 0; 

यह निरंतर कार्य के अंदर उपयोग किया जाता है। निरंतर कार्य में, आप ऑब्जेक्ट के किसी भी डेटा सदस्य के मान को नहीं बदल सकते हैं, जिस पर फ़ंक्शन को कॉल किया गया था।

17
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_ratemutable

पहले दो विकल्प आम हैं: या तो फ़ंक्शन const है और ऑब्जेक्ट को संशोधित नहीं करता है, या यहनहीं हैऔर ऑब्जेक्ट को संशोधित कर सकते हैं। हालांकि, सदस्य सूचक के भीतर सदस्य को संशोधित करने के लिए, जहां आप चाहते हैं। उस स्थिति में आप const सदस्य फ़ंक्शंस के अंदर संशोधन को सक्षम करने के लिए सदस्य घोषणा को mutable के रूप में चिह्नित कर सकते हैं।

नोट हालांकि सामान्य रूप से ऐसा किया जाता है जब सदस्य चर पर दृश्यमान स्थिति पर भाग नहीं लेता है। उदाहरण के लिए mutex वैरिएबल बाद में गेटटर या ऑब्जेक्ट की स्थिति से लौटाए गए मान को नहीं बदलता है, लेकिन मल्टीथ्रेड किए गए वातावरण में ऑब्जेक्ट का निरंतर दृश्य प्राप्त करने के लिए प्राप्तकर्ताओं को ऑब्जेक्ट को लॉक (संशोधित) करने की आवश्यकता होती है। दूसरा सामान्य उदाहरण कैश है, जहां कोई ऑब्जेक्ट एक ऑपरेशन की पेशकश कर सकता है जो गणना करने के लिए महंगा है, इसलिए उस ऑपरेशन को निष्पादित करने वाला फ़ंक्शन कैश बाद में परिणाम हो सकता है। दोबारा, क्या मान को कैश से पुन: गणना या पुनर्प्राप्त किया गया है, यह वही होगा, इसलिए ऑब्जेक्ट की दृश्य स्थिति परिवर्तित नहीं होती है। अंत में, कभी-कभी आपको किसी मौजूदा इंटरफ़ेस के अनुरूप निर्माण का दुरुपयोग करने की आवश्यकता हो सकती है।

अब यह निर्धारित करने के लिए आप अपने डिजाइन पर लागू होने वाले तीन विकल्पों में से कौन सा विकल्प निर्धारित करते हैं। यदि आपको सदस्य विशेषता को संशोधित करने की आवश्यकता है, तो या तो सदस्य दृश्य स्थिति का हिस्सा है और फ़ंक्शन const नहीं होना चाहिए, अन्यथा यह ऑब्जेक्ट की स्थिति का हिस्सा नहीं है और इसे mutable चिह्नित किया जा सकता है।