Thread: C++ Program Classes Question

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    11

    Post C++ Program Classes Question

    Hey guys,
    I was working on a program where I am supposed to convert a c program to a C++ program and I was running into some trouble converting a function to a member function in a class.
    I am trying to make a member function that prints out a line of encoded characters and a line of decoded characters as a key. But, I am having trouble deciding what to make the private members of my class versus the public member functions. Especially with the struct, I am not sure how to pass a struct to a class and do adjustments on it. So far, my code looks like this:

    Code:
    :
    class Key
    {
    private:
        struct key_element 
        {
    	char enc;
    	char dec;
    	int man_flag;
        };
        struct key_element key[26];
    
    public:
         Key(struct elements[26])
         {
          int i,j;
          for(j=0;j<26;j++)
               {
                    key[j]=elements[j];
               }
    
          for(i=0; i<26; i++)
    	   {
    		key[i].enc = 'a'+i;
    		key[i].dec = '-';
    		key[i].man_flag = 0;
    	   }
           }
          ~Key(){}
    
          void print_key()
           {
    	int i;
    	printf("\n");
    	printf("Encoded : ");
    	for(i=0;i<26;i++)
    		printf("%c ",key[i].enc);
    	printf("\n");
    	printf("Decoded : ");
    	for(i=0;i<26;i++)
    	{
    		if(key[i].man_flag == 0)
    			printf("%c ",key[i].dec);
    	}
    	printf("\n\n");
    }
    };
    int main()
    {
    		Key my_key(key[26]);
    		my_key.print_key();
    }
    When I try to do this, I get a compile error dealing with the struct being passed to the class member function. Please help me understand how to pass the struct to the class, if that is the right terminology. Thanks in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think (1) key already exists in main anywhere and (2) key[26] is an array of keys, rather than the 27th element of the array?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    11
    Oh I am sorry, I forgot to copy that in as part of my code but I actually had
    Code:
    struct key_element 
    	{
    		char enc;
    		char dec;
    		int man_flag;
    	};
    struct key_element key[26];
    in my main function in the program. Thus, the line:
    Code:
    Key my_key(key_element key[26]);
    should pass a struct and not the 27th element, or at least I think it should. Am I defining the struct in too many places and is it even possible to pass a struct to a member function?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course it's possible to pass a struct. But your function prototype claims that you want to pass in an array of structs. You'll have to pick one.

    And the line where the function call actually happens is here:
    Code:
    Key my_key(key[26]);
    where you pass a (nonexistent) 27th element of the key array (also nonexistent -- remember things defined elsewhere don't count when you get into main) into the constructor. However, the constructor says ... well, I don't know what it says. This line
    Code:
    public:
         Key(struct elements[26])
    seems to expect an array of 26 ... somethings. struct is a keyword and cannot be used as a type, and elements is the variable name for the parameter, which means you are missing the type completely.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    11
    Quote Originally Posted by tabstop View Post
    Of course it's possible to pass a struct. But your function prototype claims that you want to pass in an array of structs. You'll have to pick one.
    I just need to print out a key for each iteration of a loop so I don't think I need to pass anything. I changed my code to say:
    Code:
    class Key
    {
    private:
    struct key_element 
    {
    	char enc;
    	char dec;
    	int man_flag;
    };
    struct key_element key[26];
    
    public:
    Key()
    {
    int i;
    
    for(i=0; i<26; i++)
    	{
    		key[i].enc = 'a'+i;
    		key[i].dec = '-';
    		key[i].man_flag = 0;
    	}
    }
    ~Key(){}
    void print_key()
    {
    	int i;
    
    	printf("\n");
    	printf("Encoded : ");
    	for(i=0;i<26;i++)
    		printf("%c ",key[i].enc);
    	printf("\n");
    	printf("Decoded : ");
    	for(i=0;i<26;i++)
    	{
    		if(key[i].man_flag == 0)
    			printf("%c ",key[i].dec);
    	}
    	printf("\n\n");
    }
    };
    
    int main()
    {
                    Key my_key();
    		my_key.print_key();
    }
    where I got rid of passing a struct since the key is printed the same every time. BUT....now, when I try to compile the program I get an error saying:
    error C2228: left of '.print_key' must have class/struct/union type.
    What can I do to fix this?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you defined a function called my_key that takes no parameters and returns a Key. You did not actually declare a Key. If you want to declare a Key, you must leave off the parentheses.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    Key my_key();
    Oops, declaration.
    Code:
    Key my_key;
    Defines a new Key called my_key.

    Yes, it's a pitfall, and you are not the only one who have fallen into it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program crashes + fscanf() question
    By happyclown in forum C Programming
    Replies: 27
    Last Post: 01-16-2009, 03:51 PM
  2. Question about K&R program
    By Aerie in forum C Programming
    Replies: 15
    Last Post: 04-24-2005, 07:09 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Program using classes
    By LMurphy in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2003, 11:32 AM
  5. Question about classes
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-19-2002, 01:22 PM

Tags for this Thread