GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Mar-2009, 08:07
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

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:
Date date = new Date (20, 3, 2009);

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  
Old 20-Mar-2009, 08:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Creating an instance of another class? (i think that was java talk)


Quote:
Originally Posted by Denno

Code:
Date date = new Date (20, 3, 2009);

The "new" operator returns a pointer to an object, so:

CPP / C++ / C Code:
    Date *date = new Date(20, 3, 2009);
Quote:
Originally Posted by Denno
error:
'conversion from Date* to non-scalar type 'Data' requested'.

Are you sure it didn't say something like
Code:
error: conversion from ‘Date*’ to non-scalar type ‘Date’ requested

It's better to paste the code and the message directly into your post.

Regards,

Dave
  #3  
Old 20-Mar-2009, 08:36
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

Re: Creating an instance of another class? (i think that was java talk)


Quote:
Originally Posted by davekw7x
‘Date’ requested[/code]

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  
Old 20-Mar-2009, 08:43
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

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:
if(!date.precedes(date2)) {....}

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:
error: request for member 'precedes' in date, which is of non-class type 'Date*'
  #5  
Old 20-Mar-2009, 10:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Creating an instance of another class? (i think that was java talk)


Quote:
Originally Posted by Denno
Code:
error: request for member 'precedes' in date, which is of non-class type 'Date*'

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:
(*date).predeces(whatever)

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:
date->predeces(whatever)


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  
Old 20-Mar-2009, 19:51
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

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:
#include "Date.h"
string weekday (Date date){
  Date *trial = new Date(1, 1, 1);
  if (date.precedes(trial)) {..}
}

Date.h
CPP / C++ / C Code:
public:  
  bool precedes (Date date);

Error:
Code:
no matching function for call to 'Date::precedes(Date*&)'

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  
Old 21-Mar-2009, 11:17
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Creating an instance of another class? (i think that was java talk)


Quote:
Originally Posted by Denno
Ok, I've now tried that but I get an error saying there is no matching function for call to 'Date:recedes(Date*&)'
That's because the method expects an object, but you're trying to pass a pointer. See Dave's previous post on how to dereference the pointer to send the actual object. (Alternatively, change your method to receive a pointer instead of an object... But it's probably simpler typing-wise to do the other way.)

Quote:
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.
Sounds like a bad idea teaching C++ and Java side by side. As for the book, if you're after learning instead of just bying the book you're supposed to, you could try the C++ Primer Plus from Prata. It costs way less than over $100. Not sure how up to date the 5th edition is, but it probably doesn't cover anything planned for the new standard.
  #8  
Old 21-Mar-2009, 19:10
Denno Denno is offline
New Member
 
Join Date: Mar 2009
Posts: 16
Denno is on a distinguished road

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:
(*date).precedes(trial)
If I try the more elegant way that dave suggested I get this
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  
Old 22-Mar-2009, 00:45
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Creating an instance of another class? (i think that was java talk)


CPP / C++ / C Code:
#include "Date.h"
string weekday (Date date){
  Date *trial = new Date(1, 1, 1);
  if (date.precedes(trial)) {..}
}
Here in your function signature it says date is an instance of Date. When you pass by value, you simply write as you have written,
CPP / C++ / C Code:
returnType function(class object);
string weekday(Date date);

To make the function receive a pointer instead, you write
CPP / C++ / C Code:
returnType function(class* object); // or (class *object) or (class * object)
string weekday(Date* date);
So, your date variable in the weekday function is already an object itself, not a pointer. You don't need to change anything in the calling object. But...
CPP / C++ / C Code:
bool precedes (Date date);
This function also expects an object itself. You have this line in your weekday function:
CPP / C++ / C Code:
Date *trial = new Date(1, 1, 1);
As Dave explained, the new operator returns a pointer and trial here is a pointer to Date now. To send the actual object instead of the pointer you need to use the dereferencing operator (*). When used with a pointer type, this operator gives you the pointed-to value. So, you should instead write
CPP / C++ / C Code:
date.precedes(*trial);
  #10  
Old 22-Mar-2009, 07:12
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 157
dlp has a spectacular aura about

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:
Date date = Date(20, 3, 2009);
...
string weekday(Date date) {
  Date trial = Date(1, 1, 1);
  // equivalent to the above: Date trial(1, 1, 1);
  if(date.precedes(trial)) {
    ...

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 GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.