C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-07-2008, 08:59 PM   #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.
racerday182 is offline   Reply With Quote
Old 12-07-2008, 09:07 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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?
tabstop is offline   Reply With Quote
Old 12-07-2008, 09:20 PM   #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?
racerday182 is offline   Reply With Quote
Old 12-07-2008, 09:26 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 12-07-2008, 09:32 PM   #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?
racerday182 is offline   Reply With Quote
Old 12-07-2008, 09:36 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 12-08-2008, 03:03 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
c++, convert, program, structures, translate

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:10 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22