![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Creating an instance of another class? (i think that was java talk)Hi all, this is my first post so please be nice
I'm not a great programmer, but I seem to be able to get through my uni work alright so far, until now. I've hit a snag. What we did was create this program that would tell you what day of the week any given day from the start of time was. We did this in java, and then were required to convert it to c++. I think I've done everything in that conversion, except one part. One of the classes calls the other class and creates and instance of it. In java I know how to do this, it's: Code:
But how do I do this in C++? Leaving that line gives me an error: 'conversion from Date* to non-scalar type 'Data' requested'. I'm pretty sure to complete this section of the practical I just need to change this bit of code, but nothing I've tried has worked, so any help is greatly appreciated. Thanks Denno |
|||
|
#2
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)Quote:
The "new" operator returns a pointer to an object, so: CPP / C++ / C Code:
Quote:
Are you sure it didn't say something like Code:
It's better to paste the code and the message directly into your post. Regards, Dave |
|
#3
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)Quote:
grrr, the amount of times I hit the a key instead of the e key or vice versa. What's wrong with me, they're not even next to each other! Anyway, thank you very much for your help. I will give it a quick go now and see how it works. |
|
#4
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)OK so now that error is finished, I've got a new one.
I'm trying to do this: Code:
I want this to check if the current date precedes the set date, (precedes is a function inside Date), then the date is increased somehow, don't think that's particularly important here. The error I get is: Code:
|
|
#5
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)Quote:
Well, the message is telling you that date is not a class object, but it is a pointer to a Date object. Since date is a pointer, you must dereference it in order to access the object. Assuming there is a Date class member function named precedes()The "brute force" notation to dereference the pointer (and, therefore, to invoke the function) would be CPP / C++ / C Code:
Since this a very commonly used construct, the original author of the C language thoughtfully defined an exactly equivalent notation that he thought is much more elegant, and I think most programmers agree, since the following is almost universally used: CPP / C++ / C Code:
Regards, Dave Footnote: Are you in a course with no C or C++ prerequisite requirements and no C++ course reference material and you are given an assignment to write a C++ program? Maybe you can get a book or find an on-line tutorial. For example: http://www.cplusplus.com/doc/tutorial/ |
|
#6
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)Ok, I've now tried that but I get an error saying there is no matching function for call to 'Date::precedes(Date*&)'
extract from weekdays.cpp CPP / C++ / C Code:
Date.h CPP / C++ / C Code:
Error: Code:
Well it doesn't have a pre-requisite of c++ exactly, just programming in general I think. This is the first time that this course is teaching c++, it used to be java, hence the reason why this prac started out with doing the program in java and converting to c++. There is a text book but I haven't got around to buying it yet seeing as though it's well over $100. Last edited by admin : 21-Mar-2009 at 04:50.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#7
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)Quote:
Quote:
|
|
#8
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)Thanks, I'll have a look for that book.
I tried the dereferencing strategy that Dave posted, but this gives me another error: no match for 'operator*' in '*date'. I replaced the if condition with Code:
error: base operand of '->' has non-pointer type 'Date' I'm sorry to be such a nuisance, but the more help I can get here then the less time I'll have to spend with a demonstrator at uni. Thanks |
|
#9
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)CPP / C++ / C Code:
CPP / C++ / C Code:
To make the function receive a pointer instead, you write CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#10
|
|||
|
|||
Re: Creating an instance of another class? (i think that was java talk)I would not bother doing it like Java. Use regular objects instead of pointers, it makes life easier and you aren't creating memory leaks left and right.
CPP / C++ / C Code:
Here's the thing: In C++, any time you use new, you have to call delete on that variable when you are done with it. If you don't, that's memory that you lose (although most modern operating systems are smart enough to grab it only when the application closes). Java on the other hand, does that for you automatically by using garbage collection. If your professor has not mentioned anything about delete in C++, don't use new. If he demands you use new in C++, ask him about delete. |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Error compiling Apache 2.2.10 | tiagofgarcia | Apache Web Server Forum | 2 | 10-Nov-2009 14:31 |
| Hard drive/CPU Diagnoses Issues | binarybug | Computer Hardware Forum | 1 | 22-Jan-2007 20:23 |
| Message Class | TransformedBG | C++ Forum | 5 | 29-Nov-2006 22:28 |
| Box Class, need help again :( | TransformedBG | C++ Forum | 7 | 13-Nov-2006 16:11 |
| a tester class and then some. | postage | Java Forum | 1 | 06-May-2006 16:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The