1 Replies - 232 Views - Last Post: 08 December 2012 - 08:03 PM Rate Topic: -----

#1 StormRider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 12

Cannot pass struct to base class' constructor

Posted 08 December 2012 - 07:02 PM

Hello!

I have spent quite a few hours searching for a solution in my problem, but I can't seem to find anything of help. I hope you guys can give me a hint as to what I'm doing wrong.

Well, the main idea is that I'm using a struct to be able to use dates in my program. It (appears to) work fine in main(), but the problems start when I'm trying to construct an object using the struct as a property.

To be exact:

...
struct tm datesend;
struct tm datesend; datesend.tm_mday=d; datesend.tm_mon=m; datesend.tm_year=y;
// I view the datesend's fields, work fine
class base* basepack = new base(sendname,sendadd,recname,recadd,weight,datesend);
basepack->print(); //it fails to print dateofsend from base class



Derived class:

class base : public package {
      public:
             base(string __sname, string __sadd, string __rname, string __radd, float __weight, struct tm __dateofsend) : package(__sname, __sadd, __rname, __radd, __weight,  __dateofsend) {};
             base() : package() {};
             tm calculatedeliverytime() {
                    struct tm deltime = dateofsend;
                    deltime.tm_mday +=  5;
                    return deltime; 
                    };
             };



And the base class:

class package {
      public:
             struct tm dateofsend;
             package(string _sname, string _sadd, string _rname, string _radd,float _weight,struct tm _dateofsend)                {sname=_sname;sadd=_sadd;rname=_rname;radd=_radd;weight=_weight;dateofsend=_dateofsend;};
void print() {cout << dateofsend.tm_mday;};




The thing is, it compiles without an error, but when it's time to print the dateofsend, it says it's not initiated. (I can't use it in any calculations)
My full code can be found here: http://pastebin.com/ErSpkJK7
I tried to reference the struct in multiple ways, didn't work. Also, I'm only working with "base", to try and locate the error.
Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot pass struct to base class' constructor

#2 StormRider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-December 12

Re: Cannot pass struct to base class' constructor

Posted 08 December 2012 - 08:03 PM

Hey, it's me again!
I think I've found a solution to my problem, and I'd like to share it with you, as I've stumbled upon many unanswered questions to come to this point!

Well, I've thought about referencing and pointing between classes, so I've taken the "struct tm datesend;" line and put it before "int main()" as a global struct! Works like a charm.

Even if I helped one person it's worthwhile. Happy Coding everybody!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1