2014-04-09 29 views
0
#include <iomanip> 
#include <string> 
#include <cstdlib> 
#include <iostream> 

using namespace std; 

class STLstring 
{ 
    private: 
     string word; 
    public: 
    STLstring() 
    { 
     word = ""; 
    } 
    void setWord(string w); 
    string getWord(); 

}; 

class EncryptString:public STLstring 
{ 
    private: 
     void encrypt(); 
     void decrypt(); 
}; 


/*****************IMPLEMENTATION*******************/ 

void STLstring::setWord(string w) 
{ 
    void encrypt(); 
    word = w; 
    cout << word; 
} 

string STLstring::getWord() 
{ 
    void decrypt(); 
    return word; 
} 

void EncryptString::encrypt() 
{ 
    string temp = getWord(); 

    temp = (temp - 5) %26; 



    setWord(temp); 
} 

void EncryptString::decrypt() 
{ 
    string temp = getWord(); 



    setWord(temp); 
} 

int main() 
{ 
    string word = ""; 
    EncryptString EncrptStr; 

    cout << "Enter a word and I will encrypt it so that you cannot read it any longer." << endl; 
    getline(cin, word); 

    cout << "\nHere is the encrypted word..." << endl; 
    EncrptStr.setWord(word); 

    cout << "\nHere is the decrypted word..." << endl; 
    cout << EncrptStr.getWord() << endl; 
} 

1中錯誤: '溫度 - 5' 敵不過 '操作符 - '

temp = (temp - 5) %26; 

錯誤錯誤說:在沒有比賽的「操作符 - '溫度 - 5' 我我試圖做的是一個ceasar密碼,我知道我還沒有完成密碼,但我認爲即使完成它,錯誤仍然會出現,我應該在課堂上做一個重載操作符?如果是這樣如何?我認爲重載只在兩個類之間。

回答

1

tempstring類型,您指定的是減法。將類型更改爲支持減法的類型(如int)並相應地更改邏輯,或將operator-更改爲stringint

+0

不要爲字符串實現'operator-'。 –

+0

@LightnessRacesinOrbit:我澄清了我的答案的一部分。謹慎解釋你的評論? – wallyk

+1

這樣的操作符會令人驚訝,它的語義不清。避免創造一個誘惑;這將是一個運營商濫用的完美例子。看到urzeit的答案。 –

1

您的變量temp是一個字符串,字符串沒有減法。像"hello" - "world"這樣的聲明沒有意義,所以定義一個通常不是一個好主意。在你的情況下,你甚至會嘗試從一個字符串中減去一個數字(「hello」 - 5),這也是沒有意義的。

如果你要計算的東西,使用號碼類型(如intfloatdoublelong)。

看着你的代碼我很確定你想用你的字符串中的單個字符的數值來計算某些東西來「加密」它們。爲此,您必須對字符串char char的字符進行操作。類型char是一個數字類型,因此計算'T'-'A'非常好,而"T" - "A"沒有意義。

相關問題