3

After copying over the code from Classes as parameter of function c++ into my code, I am getting the error: note: because the following virtual functions are pure within 'TEA': and XTEA, but only those two functions. The other functions, AES, BLOWFISH, CAMELLIA, RC4, RC5, RC6, etc. are all working. Its just those two functions that are erroring. I don't get why.

code from link (slightly modified):

class CryptoAlgorithm
{
   public:
      virtual std::string encrypt(std::string DATA) = 0;
      virtual std::string decrypt(std::string DATA) = 0;
      virtual void setkey(std::string KEY) = 0;
};

and TEA setkey()

void setkey(std::string KEY, 
            unsigned int ROUNDS = 64, 
            uint32_t DELTA = 0x9e3779b9, uint32_t TOTAL = 0xc6ef3720)

All of the functions in the other classes are the same. encrypt/decrypt only have std::string DATA as their arguments. void setkey has std::string KEY and other optional arguments. However, functions like RC6, whose setkey also has other optional arguments does not error.

Any reason why?

Also, all of the classes have : public CryptoAlgorithm next to their declarations.

Community
  • 1
  • 1
calccrypto
  • 8,583
  • 21
  • 68
  • 99

3 Answers3

4

The virtual function implementation' definitions must have the same protos as your pure virtual functions in the abstract class definition, else the compiler treats them as different functions(they become overloads), you are also required to implement every pure virtual function for the class not to be abstract(of course it can't define its own pure virtual functions either). The option to fix it is to have your realisation classes implement setKey with only one string argument(to match the virtual proto), then have a separate function for the overloads(this might lead to ambiguos calls from the default parameters however).

Necrolis
  • 25,836
  • 3
  • 63
  • 101
  • `encrypt` does only have one argument. and also, how did `RC6.setkey()` pass if it has multiple optional arguments as well? – calccrypto Jun 16 '11 at 16:57
  • @cal: i'd only be able to tell you that with the definitions of the realised classes, though i'm betting its cause some classes have multiple versions of `setkey`, one of which the compiler can match to the pure virtual function, or the compiler halted on multiples of the same error – Necrolis Jun 16 '11 at 17:12
  • @cal: in that case you definitely aren't implement the virtual function if some implementations have extra args for `setkey`, as I said, full class definitions would help clarify things. also, is that error message the complete error? seems a bit truncated... – Necrolis Jun 17 '11 at 05:00
3

You cannot create objects of abstract classes. You need to ovverride the pure virtual functions in your derived class to be able to create objects of derived class.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

=0; is pure virtual in C++ which means that you have to implement those function in the child classes of CryptoAlgorithm.

And as Als said, you cannot create objects from pure virtual (abstract) classes.

Grammin
  • 11,808
  • 22
  • 80
  • 138