classes

Pages: 12
when i compile my program I get the following message:redefinition of 'keyWord' as different kind of symbol
string Vigenere::keyWord;

This is the class I'm using:

class Vigenere{
public:
Vigenere(int shamt); // Specify shift amount
string keyWord(string); // This is the keyword/secret word
string encrypt(string); // Encrypt message
string decrypt(string); // Decrypt message
string simplify(string); // Returns edited copy of string with only UPPERCASE letters
private:
char shift(char, int); // Shifts the order of the letters
int shamt; // Specify shift amount
};

and this where the program catches the error:

string Vigenere::keyWord;

Vigenere(string keyWord){
string key;
for (int i = 0; i < key.size(); i++){
if (key[i] >= 'A' && key[i] <= 'Z')
key += key[i];
else if (key[i] >= 'a' && key[i] <= 'z')
key += key[i] + 'A' - 'a';
}
}

What am I doing wrong?
Why is keyWord a method?
method?
You made keyWord a class method (function). Wouldn't you want keyWord to just be a string variable?
Indeed keyWord should function like a string that covers up the encrypted message
Well you don't have keyWord set to anything. It is just a method that takes a string parameter.
so what do you recommend?
If you want keyWord to actually be a string variable, put it in the private section. Instance variables are usually private and member methods (functions) are usually public.

1
2
3
private:
int shamt; // Specify shift amount
string keyWord;
ok
still getting the same error
And if your methods are set up outside of the class make sure you use the scope resolution operator(::)

1
2
3
4
5
6
7
8
9
10
11
12
//constructor
Vigenere::Vigenere(string keyWord)
{
     string key;
     for (int i = 0; i < key.size(); i++)
     {
          if (key[i] >= 'A' && key[i] <= 'Z')
               key += key[i];
          else if (key[i] >= 'a' && key[i] <= 'z')
               key += key[i] + 'A' - 'a';
     } //end for
}
Last edited on
Here's an example of a class I recently made. It may help you with the logic/syntax of a class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <cmath>
using namespace std;

//declaration/implementation section
class Solve 
{
private:
    double a;
    double b;
    double c;
    
public:
    Solve()
    {
        //initialize private data members
        a = 0.0;
        b = 0.0;
        c = 0.0;
    } //end of default constructor
    
    ~Solve()
    {
        //
    } //end of default destructor
    
    void setParameters(double aNum, double bNum, double cNum)
    {
        //assign values to private data members
        a = aNum;
        b = bNum;
        c = cNum;
    } //end of setParameters method
    
    double solveFirstDegreePlus()
    {
        return (-1 * b) / a;
    } //end of solveFirstDegreePlus method
    
    double solveFirstDegreeMinus()
    {
        return b / a;
    } //end of solveFirstDegreeMinus method
    
    double solveSecondDegreePlus()
    {
        return ((-1 * b) + sqrt(pow(b, 2.0) - (4.0 * a * c))) / (2.0 * a);
    } //end of solveSecondDegreePlus method
    
    double solveSecondDegreeMinus()
    {
        return ((-1 * b) - sqrt(pow(b, 2.0) - (4.0 * a * c))) / (2.0 * a);
    } //end of solveSecondDegreeMinus method
};
Last edited on
ok yeah I do get your point, i have a different constructor, would it make a difference?

// default constructor
Vigenere::Vigenere(int shiftAmount){
shamt = shiftAmount;
}
ok I'm going to try to follow your logic
You can have multiple constructors, but I usually just use one to initialize my instance variables. I use a setParameters(/*parameters*/) method to assign values to them.
ok I didn't know that
And I usually just declare my methods inside of my class so I don't have to put Solve:: before every method since it may be forgotten at times.
Last edited on
now that I have the same sort of syntax as your code above i get this message for my member method: "extra qualification on member Vigenere "
do I have to declare each member method as follows:

class "member method"
{
type "member method"(argument);
};
Usually only the default constructor has the same name as the class. A destructor also does but you don't have to worry about that. Please send all of your code. Also use code braces which makes the code easier to read. Just click the "<>" in the format section and put your code between the blocks
Pages: 12