2017-05-29 2 views
0

FunnyNumberクラスから文字列を取り消そうとしています。問題は、私がf2のメインで逆メソッドを呼び出すと、f2文字列を逆にしないということです。しかし、逆のメソッドの実装から逆の文字列を出力すると、うまく動作します。また、< <演算子をオーバーロードし、FunnyNumberクラスで継承されたNumberクラスの文字列値を出力しました。どんな助けもありがとう。reverse関数は、クラスのインスタンス化の参照される文字列を反転しません。

#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機能は、2番目の機能を変更しますが、Numberベースサブオブジェクトには影響しません。そして、あなたが呼び出す唯一の出力関数は、Numberベースサブオブジェクトで作業しており、string FunnyNumber::valueについては何も知りません。だからこそ、印刷されたのは逆転の結果ではない。

+0

これを修正するにはFunnyNumberクラスのoperator <<をオーバーロードする必要がありますか? – tildawn28

+0

@ tildawn28:これは、おそらく最高のものではないでしょう。これは、 'FunnyNumber'が' Number'の代わりに使用されていることを意味するので、逆の文字列は使用されません。しかし、あなたが 'Number'ソースコードを表示していないので、より良い解決策について話すのは難しいです。 –

+0

私はNumberソースコードを追加しました。 – 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

友人の機能は継承されませんが、派生クラス。バグは他の何かによって引き起こされます。 –

関連する問題