2016-11-21 63 views
0

我做了一個C++代碼。一個持有物品的MList。我超載了運算符以便以特定的格式打印MList中的值。這裏是代碼:運算符超載解決方案

friend ostream& operator<<(ostream &out, const MSet<V> &m) 
{ 
    string s = ""; 
    s += "Size " + to_string(m.size_) + "\n";//out << m.size() << endl; 
    s += "Cap " + to_string(m.capacity_) + "\n"; //out << m.capacity() << endl; 
    for (int i = 0; i < m.size_; i++) 
    { 
     if (i < m.size_ - 1) 
      s += m.ary[i].element + ",";//out << m.ary[i].element << ","; 
     else 
      s += m.ary[i].element; 
    } 
    //cout << "String : " << s; 
    return out << s; 
} 

但它不打印正確的值。它打印的大小和容量正確,但不是值。相反的價值觀,它打印一些跡象,如心臟:

Here is the output.

你可以看到它打印的規模和能力的權利,但不是值。這是相關的代碼。我只是現在正在執行的情況下2:

#include<iostream> 
using std::cout; using std::endl; 
using std::ostream; using std::cin; using std::boolalpha; 

#include<string> 
using std::string; 

using namespace std; 

template <class V> 
struct SetElement 
{ 
    V element; 
    int cnt; 

    SetElement() = default; 
    SetElement(V v) : element(v){} 
}; 

template <class V> 
ostream &operator<<(ostream & o,const SetElement<V> &p) 
{ 
    return o << p.element; 
} 

template <class V> 
class MSet 
{ 
private: 
    SetElement<V> *ary; 
    size_t capacity_; 
    size_t size_; 

public: 
    MSet(V val) 
    { 
     capacity_ = 2; 
     ary = new SetElement<V>[capacity_]; 
     ary[0].element = val; 
     ary[0].cnt = 1; 
     size_ = 1; 
    } 

    SetElement<V>* find(V val) 
    { 
     SetElement<V> *found = nullptr; 
     bool yes = false; 
     for (int i = 0; i < size_ && !yes; i++) 
     { 
      if (ary[i].element == val) 
      { 

       found = &ary[i]; 
       yes = true; 
      } 
     } 
     return found; 
    } 

    friend ostream& operator<<(ostream &out, const MSet<V> &m) 
    { 
     string s = ""; 
     s += "Size " + to_string(m.size_) + "\n";//out << m.size() << endl; 
     s += "Cap " + to_string(m.capacity_) + "\n"; //out << m.capacity() << endl; 
     for (int i = 0; i < m.size_; i++) 
     { 
      if (i < m.size_ - 1) 
       s += m.ary[i].element + ",";//out << m.ary[i].element << ","; 
      else 
       s += m.ary[i].element; 
     } 
     //cout << "String : " << s; 
     return out << s; 
    } 

}; 

int main(){ 
    int test; 
    long l1, l2, l3; 

    cin >> test; 
    cout << boolalpha; 

    switch (test){ 

     // ... 
    case 2: { 
     cin >> l1 >> l2; 
     MSet<long> m_l(l1); 
     auto p = m_l.find(l1); 
     if (p != nullptr) 
      cout << *p << endl; 
     else 
      cout << "Val:" << l1 << " not found " << endl; 

     p = m_l.find(l2); 
     if (p != nullptr) 
      cout << *p << endl; 
     else 
      cout << "Val:" << l2 << " not found " << endl; 
     //cout << "MList \n"; 
     cout << m_l; 
     break; 
    } 
     // ... 
    } 
} 
+2

我刪除了不相關的Ç標籤。請不要添加不相關的標籤。還請學習如何創建一個[***最小***,完整和可驗證示例](http://stackoverflow.com/help/mcve)。最後,您應該學習如何使用調試器,以及如何逐行執行代碼。 –

+0

我已經使用了調試器,並逐行執行並檢查了代碼。它在調試器中顯示的字符串是s =「Size 1 \ nCap 2 \ n \ x3」。但是當它打印到控制檯而不是3時,它會打印一些符號。我不知道。我可以在調試器中看到值3,但是當它與字符串連接時,會插入\ x3。 –

回答

4

要添加的值轉換爲臨時字符串,這可能涉及不同的模板類型(在這裏你數值被轉換成字符)的隱式轉換。

剛剛打印值,而臨時字符串:

friend ostream& operator<<(ostream &out, const MSet<V> &m) 
{ 
    out << "Size " << m.size_ << endl; 
    out << "Cap " << m.capacity_ << endl; 
    for (int i = 0; i < m.size_; i++) 
    { 
     if (i < m.size_ - 1) 
      out << m.ary[i].element << ","; 
     else 
      out << m.ary[i].element; 
    } 
    return out; 
} 
+0

非常感謝你的朋友。這已經解決了我的問題。我非常感謝這一點。你能檢查一下我的代碼,並告訴我我是否做了一個好的編碼練習?那太好了。 –