2014-04-09 23 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"没有意义。

相关问题