class composition problem

G

Gary Wessle

I am trying to increment a variable of a class with in another during
a recursive call to the main class. I know there are some problems
with the code below, I hope someone would point them out.

thanks


#include <iostream>
#include <string>
#include <vector>
#include <valarray>
#include <sstream>
#include <fstream>
#include <utility>

using namespace std;

class year
{
int yr;
public:
year(int);
void incur_yr();
int get_yr();
};
year::year(int n):yr(n){}
void year::incur_yr(){ yr++;}
int year::get_yr(){ return yr; }


class make
{
string mak;
year yr; // composition
public:
make(string, int);
void incurm_yr();
string get_make();
int get_yr();
};
make::make(string s, int n) : mak(s) { yr(n); }
void make::incurm_yr() { yr.incur_yr; }
string make::get_make(){ return mak; }
int make::get_yr(){ return yr.get_yr; }




int main(){


vector<make> cars;
cars.push_back( car("GT", 1970) );
cars.push_back( car("chevy", 1957) );

for(unsigned i=0; i<cars.size(); i++)
for(j=0; j<2; j++)
if(cars.get_mak() == GT) {cars.incur_yr();}


cout << cars[0].get_yr();

//expected answer is 1772

}
 
D

David Harmon

On Mon, 28 Aug 2006 14:11:27 +1000 in comp.lang.c++, Gary Wessle
void make::incurm_yr() { yr.incur_yr; }
int make::get_yr(){ return yr.get_yr; }

Those are supposed to be function calls:

void make::incurm_yr() { yr.incur_yr(); }
int make::get_yr(){ return yr.get_yr(); }
^^
 
J

Jens Theisen

Hi David,
Those are supposed to be function calls:

Please don't answer homework questions. You might rather give an
encouraging tip than provide a solution he can just take.

Jens
 
G

Gary Wessle

Jens Theisen said:
Hi David,

Please don't answer homework questions. You might rather give an
encouraging tip than provide a solution he can just take.

Jens

I worked out most of the bugs but still getting the error;

try.cpp: In constructor 'make::make(std::string, int)':
try.cpp:46: error: no matching function for call to 'year::year()'
try.cpp:31: note: candidates are: year::year(int)
try.cpp:22: note: year::year(const year&)
make: *** [try.o] Error 1

****************************************************************
1 #include <iostream>
2 #include <string>
3 #include <vector>
4 #include <valarray>
5 #include <sstream>
6 #include <fstream>
7 #include <utility>
8
9 using namespace std;
10
11
12
13 // void wait ( int seconds )
14 // {
15 // clock_t endwait;
16 // endwait = clock () + seconds * CLOCKS_PER_SEC;
17 // while (clock() < endwait) {}
18 // }
19
20
21 class year
22 {
23 int yr;
24 public:
25 year(int);
26 void incur_yr();
27 int get_yr();
28 void set_yr(int);
29
30 };
31 year::year(int n):yr(n){}
32 void year::incur_yr(){ yr++;}
33 int year::get_yr(){ return yr; }
34 void year::set_yr(int n){ yr = n;}
35
36 class make
37 {
38 string mak;
39 year yr; // composition
40 public:
41 make(string, int);
42 void incurm_yr();
43 string get_make();
44 int get_yr();
45 };
46 make::make(string s, int n) : mak(s) { yr.set_yr(n); }
47 void make::incurm_yr() { yr.incur_yr(); }
48 string make::get_make(){ return mak; }
49 int make::get_yr(){ return yr.get_yr(); }
50
51
52
53
54 int main(){
55
56
57 vector<make> cars;
58 cars.push_back( make("GT", 1970) );
59 cars.push_back( make("chevy", 1957) );
60
61 for(unsigned i=0; i<cars.size(); i++)
62 for(int j=0; j<2; j++)
63 if(cars.get_make() == "GT") {cars.incurm_yr();}
64
65
66 cout << cars[0].get_yr();
67
68
69
70 //expected answer is 1772
71
72 // int n;
73 // cout << "Starting countdown...\n";
74 // for (n=10; n>0; n--)
75 // {
76 // cout << n << endl;
77 // wait (1);
78 // }
79 // cout << "left off" << '\n';
80
81 }
 
D

David Harmon

On Mon, 28 Aug 2006 09:17:43 +0100 in comp.lang.c++, Jens Theisen
Hi David,

Please don't answer homework questions. You might rather give an
encouraging tip than provide a solution he can just take.

No indication this was homework.

Even if it was, he posted his best effort code and asked for some
simple help. This is the kind of problem that you can look at for a
long time, in your own code, without spotting the problem. Nothing
like a second set of eyes.

The kind of "homework" that is ridiculed here is "Here is the
assignment give me the answer i need it today."
 
D

David Harmon

On Mon, 28 Aug 2006 20:10:15 +1000 in comp.lang.c++, Gary Wessle
try.cpp: In constructor 'make::make(std::string, int)':
try.cpp:46: error: no matching function for call to 'year::year()'
try.cpp:31: note: candidates are: year::year(int)
try.cpp:22: note: year::year(const year&)

Since class year has user defined constructors, the compiler will
not supply a default constructor. Call the year constructor in the
initializer list of the constructor of class make.

See "# [10.6] Should my constructors use "initialization lists" or
"assignment"?" in Marshall Cline's C++ FAQ. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
J

Jens Theisen

David said:
Even if it was, he posted his best effort code and asked for some
simple help.

My suspicion was that it was not his code, but the one contained in an
assignment; after all, the code is clearly not from a real world
problem. "I know there are some problems
with the code below" smelled like the wording of an assignment to me, in
the abscense of any error message or anything else.
The kind of "homework" that is ridiculed here is "Here is the
assignment give me the answer i need it today."

I didn't mean to ridicule anyone, sorry if I missed the tone.

Jens
 
G

Gary Wessle

Jens Theisen said:
My suspicion was that it was not his code, but the one contained in an
assignment; after all, the code is clearly not from a real world
problem. "I know there are some problems
with the code below" smelled like the wording of an assignment to me,
in the abscense of any error message or anything else.

the code was not a homework or an assignment, I made it up to the best
I can to understand a point I am having a problem with in a much
larger code, once I understand the point I try applying the correct
principle to my coding style.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top