![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 11
| 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();
}
|
| racerday182 is offline | |
| | #2 |
| and the Hat of Guessing 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 | |
| | #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];
Code: Key my_key(key_element key[26]); |
| racerday182 is offline | |
| | #4 |
| and the Hat of Guessing 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]); Code: public:
Key(struct elements[26])
|
| tabstop is offline | |
| | #5 | |
| Registered User Join Date: Nov 2008
Posts: 11
| Quote:
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();
}
error C2228: left of '.print_key' must have class/struct/union type. What can I do to fix this? | |
| racerday182 is offline | |
| | #6 |
| and the Hat of Guessing 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 | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Code: Key my_key(); Code: Key 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:
| |
| Elysia is offline | |
![]() |
| Tags |
| c++, convert, program, structures, translate |
| Thread Tools | |
| Display Modes | |
|
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 |