2013-04-02 20 views
0

這裏是給我找麻煩代碼:我想不通爲什麼會這樣對我的生活爲什麼在嘗試將重複對象添加到std :: set時會出現這個奇怪的錯誤?

Relation* Relation::relation_union(Relation* r) { 
    std::set<Tuple*>::iterator it1; 
    for (it1 = tuples.begin(); it1 != tuples.end(); it1++) {           
     r->tuples.insert(*it1);   //this is where i insert into the set    
    } 
    return r; 
} 

。我用這段代碼得到了一個核心轉儲。

以下代碼可以很好地按字母順序排列我的集合中的元組(這是字符串的向量),但我認爲這是我的錯誤的來源,因爲它不知道每個元素相同時該做什麼:

編輯對代碼進行了更改。

struct comp { 
    bool operator()(const Tuple * lt, const Tuple * rt) { 
     for (unsigned i = 0; i < lt->values.size(); i++) { 
      std::string strl = lt->values[i]; 
      std::string strr = rt->values[i]; 
      if (strl != strr) { 
       return (strl < strr); // compares with the length 
      } 
     } 
      return false; 
    } 

}; 

'元組' 來自下面的代碼:

Relation(){ 
     name = ""; 
     schema = new Schema(); 
     tuples = std::set<Tuple*, comp>(); 
     domain = std::set<std::string>(); 
    } 

    std::string name; 
    Schema* schema; 
    std::set<Tuple*, comp> tuples; 
    std::set<std::string> domain; 
} 

這裏是我的堆棧跟蹤 - 這是不是非常有幫助,我:

Program received signal SIGABRT, Aborted. 
0x00007ffff753b425 in raise() from /lib/x86_64-linux-gnu/libc.so.6 
(gdb) bt 
#0 0x00007ffff753b425 in raise() from /lib/x86_64-linux-gnu/libc.so.6 
#1 0x00007ffff753eb8b in abort() from /lib/x86_64-linux-gnu/libc.so.6 
#2 0x00007ffff7b9169d in __gnu_cxx::__verbose_terminate_handler()() from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 
#3 0x00007ffff7b8f846 in ??() from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 
#4 0x00007ffff7b8f873 in std::terminate()() from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 
#5 0x00007ffff7b8f96e in __cxa_throw() from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 
#6 0x00007ffff7b3c987 in std::__throw_out_of_range(char const*)() from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 
#7 0x00007ffff7b7a453 in std::string::substr(unsigned long, unsigned long) const() from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 
#8 0x000000000040d889 in Input::getTokensValue (this=0x630460) at Input.cpp:91 
#9 0x000000000040e812 in Lex::emit (this=0x7fffffffe130, tokenType=UNDEFINED) at Lex.cpp:268 
#10 0x000000000040e12d in Lex::nextState (this=0x7fffffffe130) at Lex.cpp:106 
#11 0x000000000040e026 in Lex::generateTokens (this=0x7fffffffe130, input=0x630460) at Lex.cpp:85 
#12 0x000000000040da20 in Lex::Lex (this=0x7fffffffe130, filename=0x0) at Lex.cpp:17 
#13 0x000000000040ea3e in main (argc=1, argv=0x7fffffffe248) at main.cpp:7 

任何幫助將是非常讚賞。謝謝。

+0

'std :: set'的一半是它不存儲重複項。有一套'Tuple'而不是那些指針有問題嗎? – chris

+2

如果所有值相等,'comp :: operator()'返回什麼? –

+0

除了@AlexChamberlain,請調高警戒級別。 – chris

回答

3
bool operator()(const Tuple * lt, const Tuple * rt) { 
    for (unsigned i = 0; i < lt->values.size(); i++) { 
     std::string strl = lt->values[i]; 
     std::string strr = rt->values[i]; 
     if (strl != strr) { 
      return (strl < strr); // compares with the length 
     } 

    } 
    return false;//EDIT 
} 
+0

我將這段代碼添加到我的代碼中,但是當我嘗試添加重複的對象時,仍然收到了核心轉儲......可能會導致此問題? – amorimluc

+0

我編輯了代碼,很抱歉給您帶來不便。 –

+0

這不是我的錯誤的來源,而是我的代碼正常工作的必要部分。謝謝 – amorimluc

2

comp::operator()如果所有值都相等,應返回false

struct comp { 
    bool operator()(const Tuple * lt, const Tuple * rt) { 
     for (unsigned i = 0; i < lt->values.size(); i++) { 
      std::string strl = lt->values[i]; 
      std::string strr = rt->values[i]; 
      if (strl != strr) { 
       return (strl < strr); // compares with the length 
      } 
     } 
     return false; 
    } 
}; 
0

您的比較運算符期望兩個操作數的「值」成員具有相同的大小。如果rt-> values小於lt-> values,你會得到一個out-of-bounds錯誤(但有問題的不是來自這裏)。

相關問題