Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Leanna

Classes w/in Classes- Need Help

This topic is 5166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am having trouble getting the kinks out of my program. I am new to the whole class thing, and I can''t figure out what''s wrong! I have about 17 errors right now. In my main, there are lap1,lap2,lap3,lap4,& lap5 and my compiler says that there is no mathcing function for call. Also, in my compute function it says that there are parse errors, as well as in my showspecs function. Both of these seem to be when I am trying to call the cost & weight from my classes. Any help would be greatly appreciated! Thank you, Leanna #include <cstdlib> #include <iostream> using namespace std; class CPU { private: double weight, size, cost; public: CPU (double w, double s, double c); double GetCost() {return cost;} double GetWeight() {return weight;} double GetSize() {return size;} }; class Screen { private: double weight, size, cost; public: Screen (double w, double s, double c); double GetCost(){return cost;} double GetWeight(){return weight;} double GetSize(){return size;} }; class Modem { private: double weight, speed, cost; public: Modem (double w, double s, double c); double GetCost(){return cost;} double GetWeight(){return weight;} double GetSpeed(){return speed;} }; class Laptop { public: double total_cost; double total_weight; Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem()); void Compute(); void Showspecs(); }; CPU::CPU(double w= 2.266, double s= 300, double c= 1258): weight(w), size(s), cost(c){} Screen::Screen(double w= 3, double s= 13.3, double c= 215): weight(w), size(s), cost(c){} Modem::Modem(double w= .05, double s= 33600, double c= 119): weight(w), speed(s), cost(c){} Laptop::Laptop(CPU::CPU(), Screen::Screen(), Modem::Modem()){} void Laptop::Compute() { total_cost = Modem.cost + CPU.cost + Screen.cost; total_weight = Modem.weight + CPU.weight + Screen.weight; } void Laptop::Showspecs() { cout << "This computer has a " << CPU.size << "Mhz CPU \n"; cout << "with a" << Screen.size << "display and a" << Modem.size << "\n"; cout << "bps modem. It costs" << total_cost << "and weighs" << total_weight << "\n"; cout << "pounds. \n"; system ("pause"); } int main() { CPU cpu1,cpu2(3.4, 233, 945),cpu3(2.9, 350, 1295); Screen scr1,scr2(1.3, 10.3, 306), scr3(1.8, 14.1, 520); Modem mdm1, mdm2(.026, 56000, 160); Laptop lap1, lap2(cpu1, scr3, mdm2), lap3(cpu3, scr2), lap4(cpu2, scr2, mdm2), lap5(cpu3, scr3, mdm2); lap1.Showspecs(); lap2.Showspecs(); lap3.Showspecs(); lap4.Showspecs(); lap5.Showspecs(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
Your problem is the difference between classes and instances of classes.
A class is a type, it has no data storage (well it can with static but you should ignore that stuff for now).

int a; //a is an instance of int.
CPU cpu1; //cpu1 is an instance of CPU.


first couple things
1. The laptop constructor is really messed up.
2. Your laptop class has no instances of cpu, modem or screen in it. Which is not what you want.

Did you get this example from a book or something. I only say this because your using more advanced syntax but are making some basic mistakes.



[edited by - Cocalus on May 29, 2004 4:21:32 PM]

[edited by - Cocalus on May 29, 2004 4:23:49 PM]

Share this post


Link to post
Share on other sites
These lines are a source of many errors:
Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem());
Laptop::Laptop(CPU::CPU(), Screen::Screen(), Modem::Modem()){}
They should be something like this:
Laptop (CPU c, Screen s, Modem m);
Laptop::Laptop(CPU c, Screen s, Modem m){}
Note: Using const references is preferred but I don''t want to confuse the issue.


John Bolton
Page 44 Studios
Current project: NHL Faceoff 2005 PS2

Share this post


Link to post
Share on other sites
Also since cost, weight, size and what not are private, you need to use the accessor functions (GetSize() and the like).

Share this post


Link to post
Share on other sites
Thanks for the help, but I am not sure I am understanding it yet- and no I didn''t get it out of a book- I wrote it As you can see, I don''t have the basics down yet!
Anyway, I am not sure what I should put in the place of these:

Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem());

Laptop::Laptop(CPU::CPU(), Screen::Screen(), Modem::Modem()){}

I understand that cpu1, cpu2, etc.are instances of cpu, so do I need to include them all in the laptop class and in the laptop constructor?

If you could give me a little more clarification I would appreciate it!

Thank you!
Leanna

Share this post


Link to post
Share on other sites
I could rewrite the program for you but then you wouldn't learn. So I'll give you some hints.

Ok let's look at your cpu class which is correct. (well there's const references that JohnBolton mentioned but that's a little advanced.)

class CPU
{
private:
double weight, size, cost;

public:
CPU (double w, double s, double c);
double GetCost() {return cost;}
double GetWeight() {return weight;}
double GetSize() {return size;}

};

So you've made a CPU class, and from this class definition a CPU has weight, size and cost. Notice I said "has", when you think about what a class has you will probally want to make that into a member.

Now a laptop has a CPU, Modem and what not, so your laptop class needs to be changed a bit.

class Laptop
{
prvate:
CPU myCPU;
Screen myScreen;
Modem myModem;

public:
double total_cost;
double total_weight;
Laptop (CPU::CPU(), Screen::Screen(), Modem::Modem());
void Compute();
void Showspecs();
};

Notice the constructor is still messed up try to fix it. Then post again when you get stuck.


When you start getting a hang for C++ I recommend you read
[url]http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html [/url]
It'll explain the reasoning behind a lot of C++ (at least the first will). It's also a free download.

[edit]Bad Grammar[/edit]

[edited by - Cocalus on May 29, 2004 5:28:23 PM]

Share this post


Link to post
Share on other sites
Okay, I''ve been working on the Laptop constructor, and the compiler isn''t flagging it as an error now. I have an example program I have been trying to follow, but it works differently than mine and so I am having trouble understanding where the cpu1, cpu2, etc, and scr 1, scr2, etc fit in.
I''ll paste it all below again.
Thanks for all your help!
Leanna

#include <cstdlib>
#include <iostream>

using namespace std;

class CPU
{
private:
double weight, size, cost;

public:
CPU (double w, double s, double c);
double GetCost() {return cost;}
double GetWeight() {return weight;}
double GetSize() {return size;}

};

class Screen
{
private:
double weight, size, cost;

public:
Screen (double w, double s, double c);
double GetCost(){return cost;}
double GetWeight(){return weight;}
double GetSize(){return size;}
};

class Modem
{
private:
double weight, speed, cost;

public:
Modem (double w, double s, double c);
double GetCost(){return cost;}
double GetWeight(){return weight;}
double GetSpeed(){return speed;}
};

class Laptop
{
private:
CPU myCPU;
Screen myScreen;
Modem myModem;
public:
double total_cost;
double total_weight;
Laptop(CPU CPU, Screen Screen, Modem Modem);
void Compute();
void Showspecs();
};

CPU::CPU(double w= 2.266, double s= 300, double c= 1258): weight(w), size(s), cost(c){}
Screen::Screen(double w= 3, double s= 13.3, double c= 215): weight(w), size(s), cost(c){}
Modem::Modem(double w= .05, double s= 33600, double c= 119): weight(w), speed(s), cost(c){}

Laptop::Laptop (CPU CPU, Screen Screen, Modem Modem)
{
myCPU = CPU;
myScreen = Screen;
myModem = Modem;
}

void Laptop::Compute()
{
total_cost = Modem.cost + CPU.cost + Screen.cost;
total_weight = Modem.weight + CPU.weight + Screen.weight;
}

void Laptop::Showspecs()
{
cout << "This computer has a " << CPU.size << "Mhz CPU \n";
cout << "with a" << Screen.size << "display and a" << Modem.size << "\n";
cout << "bps modem It costs" << total_cost << "and weighs" << total_weight << "\n";
cout << "pounds. \n";
system ("pause");
}

int main()
{
CPU cpu1, cpu2(3.4, 233, 945), cpu3(2.9, 350, 1295);
Screen scr1, scr2(1.3, 10.3, 306), scr3(1.8, 14.1, 520);
Modem mdm1, mdm2(.026, 56000, 160);

Laptop lap1,
lap2(cpu1, scr3, mdm2),
lap3(cpu3, scr2),
lap4(cpu2, scr2, mdm2),
lap5(cpu3, scr3, mdm2);



lap1.Showspecs();
lap2.Showspecs();
lap3.Showspecs();
lap4.Showspecs();
lap5.Showspecs();

return 0;
}

Share this post


Link to post
Share on other sites
Ok the next compiler problem (on mine at least) is

void Laptop::Compute()
{
total_cost = Modem.cost + CPU.cost + Screen.cost;
total_weight = Modem.weight + CPU.weight + Screen.weight;
}

There are two different problems repeating a couple times here.

First Modem is a class, not an instance of class, thus it doesn''t have a cost. What your asking is what''s the cost of computer not the cost of "A" computer.

Second cost and weight are private (From the whole private: thing above where you define a computer). private means that only member functions (functions defined in the class) of that class can access the private parts of that class. Luckily you''ve already written that part, you just need to use it. Remeber member functions can access private parts, and anything can access public.

Share this post


Link to post
Share on other sites
review (very basic though):

in a class, if you have any variables which are private, they can only be directly accessed from that particular class, and nowhere else.

example:

class Example {
private:
int data_blocks;
float average_speed;
string title_name;

public:
Example ( ) {
data_blocks = 0;
average_speed = 0;
title_name = "Name not set.";
}
void setData (int numOfBlocks ) {
data_blocks = numOfBlocks;
}
void printSpeed ( ) {
average_speed = data_blocks / 4;
cout << "It is:" << average_speed << endl;
}
void setName ( ) {
title_name = "Example Class";
}
void printName ( ) {
cout << title_name << endl;
}
}

In this class the private members are accessed/called directly by the class that holds them. The class itself can access those private members but not an instance of the class, for example:

class Example_2 {
private:
float pipeline_misses;
Example mistake; //an instance of class Example


public:
Example_2 () {
pipeline_misses = 2; //this is fine.

mistake.data_blocks = 16; //VERY WRONG, CAN''T DO THAT!

}

//...blah blah

}

Granted when you get more experience with C++ you''ll see there are workarounds and features that don''t make the above always true. But for now, pay attention to the guidelines above, and you''ll be fine.

Share this post


Link to post
Share on other sites
I''ve been trying a ton of different ways to fix the problem, and I can''t seem to get it. I''m not sure if I need to change the actual functions GetCost, GetWeight, & GetSize, or if I just need to call them differently.
I thought that my GetCost, GetWeight, & GetSize functions returned cost, weight, and size respectively. So, when I called them from my Compute function, I could call these, as it would now be public.
I also am not sure I understand how to call the individual costs, weights, and sizes without using modem, CPU, and screen.
Could you give me a few more hints? Please???

Thank you!
Leanna

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!