2017-05-29 29 views
0

我試圖扭轉從FunnyNumber類的字符串引用字符串。問題是,當我在f2的main調用reverse方法時,它不會反轉f2字符串。但是當我從反向方法實現中打印出反轉字符串時,它可以工作。另外,我還重載了運算符<,以便在FunnyNumber類中繼承的類Number中輸出字符串值。任何幫助,將不勝感激。反向功能不反轉類的實例

#ifndef NUMBER_H 
#define NUMBER_H 

#include <iostream> 
#include <string> 
#include <algorithm> 

using namespace std; 

class Number { 
public: 
    // Make a number with value 0 
    Number(); 
    // Make a number with value val 
    Number(string val); 

    // Get the number's value 
    virtual string getValue() const; 

    // Print this number to the stream 
    virtual void print(ostream& stream) const; 

    // Read this number from the stream 
    virtual void read(istream& stream); 

    // Overload the insertion operator 
    friend ostream& operator <<(ostream& outs, const Number& n); 

    // Overload the extraction operator 
    friend istream& operator >> (istream& ins, Number& n); 

protected: 
    string value; 
}; 

#endif 


Number::Number() 
{ 
    value = ""; 
} 
Number::Number(string args) 
{ 
    value = args; 
} 
string Number::getValue()const 
{ 
    return value; 
} 
ostream& operator <<(ostream& outs, const Number& n) 
{ 
    n.print(outs); 
    return outs; 
} 
void Number::print(ostream& stream)const 
{ 
    stream << getValue(); 
} 
void Number::read(istream& stream) 
{ 
    stream >> value; 
} 
istream& operator >> (istream& ins, Number& n) 
{ 
    n.read(ins); 
    return ins; 
} 

#ifndef FUNNYNUMBER_H 
#define FUNNYNUMBER_H 

#include<iostream> 
#include<string> 
#include"Number.h" 
#include<algorithm> 


using namespace std; 

class FunnyNumber : public Number 
{ 
public: 
    FunnyNumber(); 
    FunnyNumber(string val); 
    virtual string operator+(const FunnyNumber &other)const; 
    virtual bool operator==(const FunnyNumber &other)const; 
    void reverse(); 
    int find_first_not_this(char a); 

protected: 
    string value; 

}; 
#endif // !FUNNYNUMBERS_H 

FunnyNumber::FunnyNumber() 
{ 
    value = ""; 
} 

FunnyNumber::FunnyNumber(string val) : Number(val) 
{ 
    value = val; 
} 

string FunnyNumber::operator+ (const FunnyNumber& other)const 
{ 
    return getValue() + other.getValue(); 
} 
int FunnyNumber::find_first_not_this(char a) 
{ 
    int pos = 0; 

    for(int i = 0; i < value.length(); i++) 
    { 
     if(value[i] != a) 
     { 
      pos = i; 
      return pos; 
     } 
    } 
    return pos; 
} 
bool FunnyNumber::operator==(const FunnyNumber& other)const 
{ 
    bool isEqual = true; 
    for (int i = 0; i < other.getValue().length(); i++) 
    { 
     bool found = false; 
     for (int j = 0; j < getValue().length(); j++) 
     { 
      if(getValue()[j] == other.getValue()[i]) 
      { 
       found = true; 
       break; 
      } 
     } 
     if(!found) 
     { 
      isEqual = found; 
      return isEqual; 
     } 
    } 
    return isEqual; 
} 

void FunnyNumber::reverse() 
{ 
    std::reverse(value.begin(), value.end()); 
    value.erase(0, find_first_not_this('0')); 
} 


#include <iostream> 
#include<string.h> 
#include "FunnyNumber.h" 

using namespace std; 

int main() 
{ 
    FunnyNumber f2; 
    f2 = FunnyNumber("223"); 
    f2.reverse(); 
    cout<<"Reversed value "<<f2<<endl; 

    system("pause"); 
    return 0; 
} 

輸出是223而不是322

+0

您的操作符<< <<'是如何實現的? – Ari0nhh

+0

類號內的值永遠不會更改爲223的相反值,因爲重載算子<<正在從「類號」中獲取其「值」 – Tyger

+0

我該如何解決這個問題? – tildawn28

回答

2

FunnyNumber存儲值兩次,一次在Number類型的子對象,而一旦在string FunnyNumber::value

您的reverse功能修改了第二個,但對Number基礎子對象沒有任何影響。然後你調用的唯一輸出函數是在Number基本子對象上工作,並且對string FunnyNumber::value一無所知。這就是爲什麼印刷不是逆轉的結果。

+0

所以要解決它我應該重載運算符<<在FunnyNumber類呢? – tildawn28

+0

@ tildawn28:這將是* A *的解決方案,可能不是最好的一個,因爲這意味着任何其他情況即'FunnyNumber'代替了'Number'的使用,反向字符串將不會被使用。但是由於您沒有顯示數字源代碼,因此很難談論更好的解決方案。 –

+0

好的,我剛剛添加了數字源代碼。 – tildawn28

0

重載操作< <是Number類和友元函數友元函數不是inherited

class Number { 
public: 
    // Make a number with value 0 
    Number(); 
    // Make a number with value val 
    Number(string &val); 

    // Get the number's value 
    virtual string getValue() const; 

    // Print this number to the stream 
    virtual void print(ostream& stream) const; 

    // Read this number from the stream 
    virtual void read(istream& stream); 

    // Overload the insertion operator 
    friend ostream& operator <<(ostream& outs, const Number& n); 

    // Overload the extraction operator 
    friend istream& operator >> (istream& ins, Number& n); 

protected: 
    string *value; 
}; 
Number::Number() 
{ 
    value = NULL; 
} 
Number::Number(string &args) 
{ 
    value = &args; 
} 
string Number::getValue()const 
{ 
    return *value; 
} 
ostream& operator <<(ostream& outs, const Number& n) 
{ 
    n.print(outs); 
    return outs; 
} 
void Number::print(ostream& stream)const 
{ 
    stream << getValue(); 
} 
void Number::read(istream& stream) 
{ 
    stream >> *value; 
} 
istream& operator >> (istream& ins, Number& n) 
{ 
    n.read(ins); 
    return ins; 
} 
+0

哦確定我看到某種原因,我認爲朋友職能繼承太 – tildawn28

+0

[鏈接](https://stackoverflow.com/a/24304490/7868736) – Tyger

+0

友元函數不能被繼承,但它們將被用於實例派生類。該錯誤是由別的東西引起的。 –