Thread: Class problems

  1. #1

    Angry Class problems

    I have been fiddling around with classes and decided to create an encryption/decryption program. It is a pathetic program but I am just doing it to learn.

    Code-
    Code:
    #include <iostream>
    using namespace std;
    
    class encryption
    {
    public:
    	encryption();
    	void SetCurrentBase();
    	int GetCurrentBase() { return curBase; }
    	void Encrypt();
    	void Decrypt();
    private:
    	int curBase;
    	char c;
    };
    
    encryption::encryption()
    {
    	curBase = 3;
    }
    
    encryption::SetCurrentBase()
    {
    	cout << "\nSet base to: ";
    	cin >> curBase;
    }
    
    encryption::Encrypt()
    {
    	cout << "\nType nothing to exit: ";
    	while(c == getchar())
    	{
    		switch(c)
    		{
    		case 0: break;
    		default: cout << c *= curBase;
    		}
    	}
    }
    
    encryption::Decrypt()
    {
    	cout << "\nType nothing to exit: ";
    	while(c == getchar())
    	{
    		switch(c)
    		{
    		case 0: break;
    		default: cout << c /= curBase;
    		}
    	}
    }
    
    int main()
    {
    	int option=0;
    	while(true)
    	{
    		cout << "\n1: Encrypt\n2: Decrypt\n3: Set base number\n4: View current base number\n5: Exit\nOption: ";
    		cin >> option;
    		switch(option)
    		{
    		case 1: Encrypt(); break;
    		case 2: Decrypt(); break;
    		case 3: SetCurrentBase(); break;
    		case 4: GetCurrentBase(); break;
    		case 5: exit(0);
    		default: cout << "\nInvalid option.\n"; break;
    		}
    	}
    	return 0;
    }
    Compiler errors-
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(23) : error C2556: 'int __thiscall encryption::SetCurrentBase(void)' : overloaded function differs only by return type from 'void __thiscall encryption::SetCurrentBase(void)'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(8) : see declaration of 'SetCurrentBase'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(23) : error C2371: 'SetCurrentBase' : redefinition; different basic types
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(8) : see declaration of 'SetCurrentBase'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(29) : error C2556: 'int __thiscall encryption::Encrypt(void)' : overloaded function differs only by return type from 'void __thiscall encryption::Encrypt(void)'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(10) : see declaration of 'Encrypt'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(29) : error C2371: 'Encrypt' : redefinition; different basic types
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(10) : see declaration of 'Encrypt'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(36) : error C2676: binary '*=' : 'class std::basic_ostream<char,struct std::char_traits<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(42) : error C2556: 'int __thiscall encryption::Decrypt(void)' : overloaded function differs only by return type from 'void __thiscall encryption::Decrypt(void)'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(11) : see declaration of 'Decrypt'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(42) : error C2371: 'Decrypt' : redefinition; different basic types
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(11) : see declaration of 'Decrypt'
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(49) : error C2676: binary '/=' : 'class std::basic_ostream<char,struct std::char_traits<char> >' does not define this operator or a conversion to a type acceptable to the predefined operator
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(63) : error C2065: 'Encrypt' : undeclared identifier
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(64) : error C2065: 'Decrypt' : undeclared identifier
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(65) : error C2065: 'SetCurrentBase' : undeclared identifier
    C:\Documents and Settings\Owner\Desktop\encrytion.cpp(66) : error C2065: 'GetCurrentBase' : undeclared identifier
    Error executing cl.exe.

    encrytion.obj - 12 error(s), 0 warning(s)

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>overloaded function differs only by return type
    The declaration and definition for your function is off, you forgot to add void as the return type in the definition.

    >>binary '*=' : 'class std::basic_ostream<char,struct std::char_traits<char> >' does not define this operator or a conversion
    Change
    Code:
    cout << c *= curBase;
    to
    Code:
    cout << (c *= curBase);
    and you won't confuse the compiler as to what you really meant :-)

    >>'Encrypt' : undeclared identifier
    >>'Decrypt' : undeclared identifier
    >>'SetCurrentBase' : undeclared identifier
    >>'GetCurrentBase' : undeclared identifier
    You forgot to create an object of the class to work with, these functions aren't global, they're only available to objects of the encryption class.
    Code:
    #include <iostream>
    using namespace std;
    
    class encryption
    {
    public:
      encryption();
      void SetCurrentBase();
      int GetCurrentBase() { return curBase; }
      void Encrypt();
      void Decrypt();
    private:
      int curBase;
      char c;
    };
    
    encryption::encryption()
    {
      curBase = 3;
    }
    
    void encryption::SetCurrentBase()
    {
      cout << "\nSet base to: ";
      cin >> curBase;
    }
    
    void encryption::Encrypt()
    {
      cout << "\nType nothing to exit: ";
      while(c == getchar())
      {
        switch(c)
        {
        case 0: break;
        default: cout << (c *= curBase);
        }
      }
    }
    
    void encryption::Decrypt()
    {
      cout << "\nType nothing to exit: ";
      while(c == getchar())
      {
        switch(c)
        {
        case 0: break;
        default: cout << (c /= curBase);
        }
      }
    }
    
    int main()
    {
      int option=0;
      encryption e;
      while(true)
      {
        cout << "\n1: Encrypt\n2: Decrypt\n3: Set base number\n4: View current base number\n5: Exit\nOption: ";
        cin >> option;
        switch(option)
        {
        case 1: e.Encrypt(); break;
        case 2: e.Decrypt(); break;
        case 3: e.SetCurrentBase(); break;
        case 4: e.GetCurrentBase(); break;
        case 5: exit(0);
        default: cout << "\nInvalid option.\n"; break;
        }
      }
      return 0;
    }
    :-)
    *Cela*

  3. #3
    As you can tell OOP isn't one of my strong points but I am working on it. Thanks alot.

  4. #4
    Ok, new problem, after making the adjustments, I ran the program and selected encrypt from the menu but then it just prints "Type nothing to exit" and then goes back to the menu. I have tried using a single letter to exit (x) but it does the same thing. Any ideas? Is it because I used a switch instead of if statements?

  5. #5
    Nevermind figured it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. class Template problems
    By Mr_roboto in forum C++ Programming
    Replies: 8
    Last Post: 02-16-2006, 10:21 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM