-
Class pointers
Okay, I am making a program for my brother, but I can seem to handle the objects like I want. So, I was wondering if any of you guys/girls could assist me. This is what I have so far, I have highlighted the problem in red, and the errors are at the bottom of post thank you for any assistance.
Code:
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#include <string>
#include <deque>
#include <map>
using namespace std;
class file_i {
public:
file_i();
~file_i();
virtual void setupF() const = 0;
private:
string files[20];
};
file_i::file_i() {
//
};
file_i::~file_i() {
//
};
class player : private file_i
{
public:
player();
~player();
virtual void setupF() const {
//
}
private:
deque<string> names;
deque<int> s_num;
map<int, string> manage;
};
player::player() {
file_i* p_one = new player; //this works but is the cause of error, with //delete
};
player::~player() {
delete p_one; this is line 48
};
int main()
{
}
this is the error
Code:
error: 'p_one' undeclared ( first use of this function ) Line 48
yet again thank you for assistance
-
You are deriving privately from file_i, so you don't need to allocate space for a file_i object. There is already space in the player object for file_i data.
-
okay, thanks but yet again how do I fix my error? I will change the code,but I still have no idea how to handle the object for player. managing inside of main would be okay, but I am going to use multiple functions so it wouldnt work right? to say this is a music player, and I had 5 functions ( play, stop, pause, restart, and exit ). How would I create, and delete an object that can be used by all of these. Thank you
-
>> how do I fix my error?
Remove the code in the constructor and destructor for player.
>> I still have no idea how to handle the object for player.
What do you mean? A player is a class like any other. You use it like you would use any other.
What specifically is confusing you? Do you understand private inheritance and why you are using it here?
-
Actually, no I don't know why i'm using private inheritance, it is just what I start learning with so I stuck with it. Usually with my classes I create an instance of an object inside of main, but in the situation I can't because, if I tried to use it in any other function I would get one of 2 errors one is multiple definitions, or first use of function. I just do not understand how to fix this. Thanks for any assistance
EDIT: would this work?
Code:
int main() {
static player* p_one = new player;
}
-
Hm. Why exactly are you creating a new player object inside the constructor of the player object? If that worked, you'd end up with an infinitely repeating constructor; every newly created player would again create a new player.
The only thing you have to do in a constructor is initialise the member variables, in this case names, s_num and manage. AFAIK the fact that you're privately inheriting doesn't change anything about that.
-
Okay, here is updated code. This is correct right?
Code:
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
#include <string>
#include <deque>
#include <map>
using namespace std;
class file_i {
public:
file_i();
~file_i();
virtual void setupF() const = 0;
private:
string files[20];
};
file_i::file_i() {
//
};
file_i::~file_i() {
//
};
class player : private file_i
{
public:
player();
~player();
virtual void setupF() const {
//
}
private:
deque<string> names;
deque<int> s_num;
map<int, string> manage;
};
player::player() {
};
player::~player() {
names.clear();
s_num.clear();
manage.clear();
};
int main()
{
static player* p_one = new player;
}
-
Code:
void my_func(const player& p1) { }
int main()
{
player p_one;
my_func(p_one);
}
It seems you are using advanced language features when basic ones will do.
>> This is correct right?
No. There is no need to make names, s_num and manage pointers. Leave them as objects.
You don't need to use new or delete very often in C++, so if you are using them, question whether you can use a simple object instead.
-
Ok, sorry for questions, one more problem, that code you gave I get the following error.
Code:
'p1' undeclared (first use of this function
I probably missed something
-
That was a typo. I should have written my_func(p_one).
-
Ah yes I should have seen that sorry, I am eternally grateful for the fast replies and helpful answers. One last thing tho what is the difference in private, protected,and public inheritance. Is the just a access specifier for the child class, or something else?Thanks
EDIT: ..im not doing this right
Code:
//classes are up top no difference
void my_func(const player& p1) { }
int main() {
player p_one;
my_func(p_one);
}
int myfunction() {
p_one->setupF();
}
EDIT 2: I tried sumthing is this right?
Code:
void my_func(const player& p1) { }
int myfunction();
int main()
{
player p_one;
my_func(p_one);
}
int myfunction(const player p_one) {
}
-
They are very different things. Private inheritance models "implemented-in-terms-of". In other words, when you are using private inheritance, you are saying that the current class uses the parent class for part of its implementation. Another way to model "implemented-in-terms-of" is to use simple member variables (composition).
For example, your player class is implemented in terms of the file_i class, as well as two deques and a map. You would choose private inheritance over composition only for a few specific reasons. One reason would be if you wanted to override some virtual functions in the parent class. There are other reasons, but it is somewhat rare to use private inheritance. You might not even need it in this case.
Protected inheritance is extremely rare. There might be some uses for it, but generally you can just assume it doesn't exist.
Public inheritance models "is-a" or "works-like-a". In other words, the derived object can be used as a parent class object in code that uses the parent class pointer or reference. The parent class has an interface, and the derived class implements that interface in its own way so that it can be used wherever the interface is used.
The point is that even though these seem similar, they model very different things and should generally be used in very different situations if you use them at all.
-
Code:
void my_func(player& any_name);
int myfunction(player& some_name);
int main()
{
player p_one;
//use these functions to mess with player one
my_func(p_one);
myfunction(p_one);
return 0;
}
int myfunction(player & any_name) {
//operate on any_name directly to modify p_one
}
void my_func(player& some_name) {
//operate on some_name directly to modify p_one
}
-
I just cannot get this to work, I have been trying and trying for over 3 hours now, and it isnt working. I have searched google, asked here, and still.... I am losing faith on this project. Sad thing is ive done harder things than this, but this I cant seem to grasp at all
-
Look at CodeMonkey's example. Just fill in the code in the function.
This is actually just basic function calling and variable passing that you should have done many times before.