a problem with the operator < in a set of class

hello,

i need help!!!

i have defined an opetarator in class TS
bool operator < (TS const & t) const { return this->getcoutinuity_counter() < t.getcoutinuity_counter() ;}

this operator < will be use by a set (set <TS>) to create the order thet i need!
exp:
1
2
3
TS* t =TS(w);// w is a parameter
set <TS> sec_ts;
sec_ts.insert(*t);//good the problem is here 

so the compiler say and say and say:

1
2
3
/home/sst/tmp/ccZQ4V0x.o: In function `Section::Section(std::set<TS, std::less<TS>, std::allocator<TS> >)':
ff.cpp:(.text._ZN7SectionC1ESt3setI2TSSt4lessIS1_ESaIS1_EE[Section::Section(std::set<TS, std::less<TS>, std::allocator<TS> >)]+0x8): undefined reference to `vtable for Section'
collect2: ld returned 1 exit status


that is this can you explain to me??
that is the TS class if needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class TS{

    public:
        TS(){}
        TS(u_char* data){
                                    ts_h=new TS_header;
                                    memcpy(ts_h,data,4);
                                   // payload=new u_char[184];
                                    //memcpy(payload,data,7);
                                    }
        TS(u_char* Payload,int size){}
        virtual ~TS(){};


  
         // le pid du paquet TS
        u_int16 getpid();
        void setpid(u_int16 pid);
         //début d'une section
        bool getpayload_start();
        void setpayloadstart(bool start);
        // la valeur du continuity counter
        u_int8 getcoutinuity_counter() const;
        void setcountinuity_counter(u_int8 counter);
       //le vrai payload dans le paquet
        const u_int8* getpayload() const;
        void setpayload(u_int8* data);
        void setpayload(u_int8* data, int size);
       //l'existence d'un champ d'adaptation
        bool getadaptation_field();
        void setadaptation_field(bool adaptation);
        //cette methode retourne le table_id de la section si c le debut de la section sinon elle retourne une valeur qui ne correspond à une table
        u_int8 get_table_id();

          bool operator < (TS const & t) const { return this->getcoutinuity_counter() < t.getcoutinuity_counter() ;}
     private:
           //les attributs
         TS_header* ts_h;
         u_char* payload;
        
};

I think your problem is actually in line 1:

TS* t =TS(w);

This makes t a pointer to a TS object. It then attempts to assign that pointer the actual value of the TS object. Unless a TS object can be dynamically cast into a long integer of some sort (and I don't see any way that's gonna happen) this will never compile.

You need to use
1
2
3
TS t (w);
set <TS> sec_ts;
sec_ts.insert (t);

or
1
2
3
4
5
6
7
TS *t = new TS (w);
set<TS> sec_ts;
sec_ts.insert (*t);

...

delete (t);    // whenever you are done with it 


Hope that'll work for ya, anyway. I haven't actually tried it out myself or anything.
Last edited on
Also assuming you ever write the destructor to free the memory allocated by the constructor, you will need to write a copy constructor and assignment operator.
(The code would compile without writing them, but it would crash).
Topic archived. No new replies allowed.