-
November 6th, 2004, 11:34 PM
#1
Member
C++ Classes
Ok, I am a little confused as to why this is not working. The code I am going to post is all I have right now, but it is part of a larger program I plan to build. I cant get started on my larger project until I finish my class header file. The only problem is, I cant get my class to work correctly. It worked almost perfect...until I made made the class have private variables and public functions. I know you are supposed to do this, but I cant seem to get it to work.
Code:
//Class Music
#include<iostream>
class m
{
public:
void SetGroup(char[]);
char Get();
private:
char group[50];
struct cds
{
char title[];
struct songs
{
char songName[];
char lyrics[];
};
songs song[25];
};
cds cd[50];
};
void m::SetGroup(char igroup[])
{
group[50] = igroup[50];
std::cout<<igroup<<'\n'<<group;
}
char m::Get()
{
return group[50];
}
int main()
{
m music[3];
char hey[] = "Senses Fail";
music[0].SetGroup(hey);
// std::cout<<music[0].Get();
system("PAUSE");
return 0;
}
And the output looks like:
Senses Fail
╨ⁿ"Press any key to continue . . .
I am confused as to why the value is passed, but not assigned to the class variable. Because I havent gotten this part to work, I havent worried about the Get function. I already see a problem. Can I return a char array? Would char m::Get() return a char array, because I tryed it with char[] m::Get() and that didnt work, so I am a little confused as to what to to about that. But I am not really there yet. I cant even assign the class variable the right value.
Thanks for any help provided.
-Ep
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
-
November 7th, 2004, 12:44 AM
#2
Member
Well, no one has replied yet. Someone else has helped me...kinda. He send me this code, but has yet to explain what it all does. He made changes and I was wondering if anyone could explain why he changed what he did. Or, tell me how you would change the code to make it work for you and why.
Code:
//Class Music
#include <iostream>
#include <string>
struct songs
{
std::string songName;
std::string lyrics;
};
struct cds
{
std::string title;
songs song[25];
};
class m
{
public:
void SetGroup(char*);
char* Get();
private:
std::string group;
cds cd[50];
};
void m::SetGroup(char* igroup)
{
group = igroup;
std::cout << igroup << '\n' << group.c_str();
}
char* m::Get()
{
return group.c_str();
}
int main()
{
m music[3];
char* hey = "Senses Fail";
music[0].SetGroup(hey);
std::cout << music[0].Get();
std::cin.get();
return 0;
}
Waiting for any response...
-Ep
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
-
November 7th, 2004, 07:18 AM
#3
Member
Well, No one has yet to reply. Im not sure if that is because everyone that has looked at my post sofar has no idea what I am talking about, or dont know how to fix it, but yeah. Anywayz, So far so good. I have fixed all of the problems sofar.
Code:
//Lyrics
//Class Music
#include <iostream>
#include <string>
using namespace std;
struct songs
{
string songName;
string lyrics;
};
struct cds
{
string title;
songs song[25];
};
class m
{
public:
void SetGroup(string);
string GetGroup();
void SetTitle(string, int);
string GetTitle(int);
private:
string group;
cds cd[5];
};
void m::SetGroup(string xgroup)
{
group = xgroup;
}
string m::GetGroup()
{
return group;
}
void m::SetTitle(string xtitle, int cnum)
{
cd[cnum].title = xtitle;
}
string m::GetTitle(int cnum)
{
return cd[cnum].title;
}
int main()
{
m music[3];
char hey[256];
cin.get(hey,256);
music[0].SetGroup(hey);
cin.ignore(1,'\n');
cin.get(hey,256);
music[0].SetTitle(hey, 0);
cout<<music[0].GetGroup()<<'\n'<<music[0].GetTitle(0)<<endl;
system("PAUSE");
return 0;
}
Just incase anyone was following the code and wanted to know why it wasnt working, I will explain it to the best of my knowledge. My "friend" had the GetGroup() functions returning a char pointer. Well, I re-read a chapter on pointers, which didnt help at all, and then I did some research on teh internet.
First, when you declare a char pointer, it is a little different then normal pointers.
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word = "Hey";
char* pChar = "Rah";
cout<<&word<<'\n'<<pChar<<'\n'<<word<<'\n'<<*pChar<<endl;
int num = 0;
int* pInt;
*pInt = 1;
cout<<&num<<'\n'<<pInt<<'\n'<<num<<'\n'<<*pInt<<endl;
system("PAUSE");
return 0;
}
Ouput:
0x22ff58
Rah
Hey
R
0x22ff40
0x22ffe0
0
1
Press any key to continue . . .
With the int pointer, it contains the address which points to a value. With the char pointer, (to my knowledge) it also contains an address which points to a value. But like it says, it is a char, which only holds one character. So when I assigned char* pChar = "Rah"; it gave the first address of the pointer R. If you want to access the a, you would have to do something like *(pChar+1). I know this is confusing, because I am still a little confused myself. If all char pointers are const, and you cant access the full string which the char pointer points to, what good are they. I dont know and hope someone answers this question.
Anywayz, I hope I helped someone out with my code, and I hope someone helps me out with my above question. Thanks for following along.
-Ep
01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001
-
November 7th, 2004, 12:45 PM
#4
Hi
Pointers are confusing, no question about that. However, I try to shed some light on that
issue for you, but I will be rather technical
Code:
char* pChar = "abcdef";
First notion:
What is pChar? pChar is not a "char", which holds only one character, but it is a
"char[4]" - buffer, that contains the address of "abcdef".
Test:
Code:
printf("The size of the pChar itself: %d\n", sizeof(pChar));
// the output is 4, not 6 or 1.
Second notion:
The address of pChar itself is more or less irrelevant (here):
Code:
printf("The address of the pChar itself (on the stack!): %x\n",&pChar);
Third notion:
At the address of pChar itself however, there are 4 bytes, which are the address
of "abcdef".
Code:
printf("The content of the pChar itself: %x\n",pChar);
Note, that this address actually is not on the stack but in the .data-section
of your executable, that is also in the memory.
Fourth notion:
At that memory address, there is "abcdef".
Code:
printf("The content at the memory address %x stored in pChar: %s\n",pChar,pChar);
Final notion:
Often, you want to access one particular char in your array. You can do this
in two ways, which are completely equivalent!
Code:
printf("%c",pChar[1]); // the second element of "abcdef" = 'b'
printf("%c",*pChar+1);
printf("%c",*(pChar+1));
// also the second element: Take the address stored in pChar, which is the address
// of "abcdef" and increase that one by one. The address of "abcdef" actually
// is the beginning of it, ie 'a'. The following char in the memory is thus 'b'
Cheers
If the only tool you have is a hammer, you tend to see every problem as a nail.
(Abraham Maslow, Psychologist, 1908-70)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|