Assignment #1
First Class – extended version
1. Extend the class myTime from the previous assignment so it includes methods of all
types (excluding operators) discussed during the lecture. The constructors and the
destructor should print out a notifying message on the screen.
2. Write a function with the copyMe that accepts as parameter an object of the myTime
type and returns as a value another object of that type.
3. Create objects on the heap, stack and in the global memory area.
4. Create an array of two myTime objects.
5. Analyze the messages written by the constructors and the destructor.

the program is based on three files start.cpp, myTime.cpp, and myTime.h

source code from start.cpp:

#include <stack>
#include <algorithm>
#include <vector>
#include <math>

#include "MyTime.h"

myTime global(17,23,23);  // object in global memory area  (polecenie 3)

int cmp(myTime a, myTime b)        // funkcja porównujaca 2 czasy
{
        if (a.timeToSec()>b.timeToSec()) return 1;
        if (a.timeToSec()<b.timeToSec()) return -1;
        if (a.timeToSec()==b.timeToSec()) return 0;
        return 0;
}

int main(int argc, char* argv[])
{
        //myTime timer;  // obiekt klasy MyTime
        myTime timer( 3600);  // z jednym argumentem (liczba sekund)
        //myTime timer(8,15,34);

        stack<myTime> stack1;   // polecenie 3
        stack1.push(timer);  // 'timer' wrzucony na stack

        myTime array[2]; // tablica 2 zmiennych typu myTime    (polecenie 4)

        //stack<myTime>::iterator ss;

        vector<myTime> wektor(2);
        cout<<"make_heap:\n"    ;
        make_heap(wektor.begin(),wektor.end(),cmp);

        timer.test();



        return 0;
}

the code from myTime.cpp is:

#include <stack>
#include <math>
#include <algorithm>
#include <vector>

#include "myTime.h"

int myTime::timeToSec() {
  return (second + minute*60 + hour*3600);
}

void myTime::secToTime(int nsec) {
  hour = floor(nsec/3600);
  nsec %= 3600;
  minute = floor(nsec/60);
  nsec %= 60;
  second = nsec;
}

myTime::myTime (void) {
  Word Year, Month, Day, Hour, Min, Sec, MSec;
  TDateTime dtPresent = Now();
  DecodeTime(dtPresent, Hour, Min, Sec, MSec);
  second = Sec;
  minute = Min;
  hour = Hour;
  print();
}

myTime::myTime (int nsec) {
  nsec %= 86400;
  secToTime(nsec);
  print();
}

myTime::myTime (int h, int min, int sec) {
  h%=24;
  min%=60;
  sec%=60;
  second = sec;
  minute = min;
  hour = h;
  print();
}

bool myTime::isMorning() {
  if (hour >= 6 && hour <12)
     return true;
  else
     return false;
}

int myTime::addHours(int n) {
  return (addSeconds(n*3600));
}

int myTime::addMinutes(int n) {
  return (addSeconds(n*60));
}

int myTime::addSeconds(int n) {
  int sec;
  int days = 0;
  sec = timeToSec() + n;
  if (sec<0) {
     days = -(sec/86400 - 1);
     sec = (sec % 86400) + 86400;
  }
  else {
     days = sec/86400;
     sec %= 86400;
  }
  hour = sec/3600;
  sec %= 3600;
  minute = sec/60;
  sec %= 60;
  second = sec;
  return (days);
}

void myTime::print() {
  printf("%d:%d:%d", hour, minute, second);
}

void myTime::test() {
  printf("\n It's: ");
  print();
  printf("Time in seconds: %d\n", timeToSec());
  printf("After adding 24h, passes %d day \n", addHours(24));
  printf("After substracting 86400s(24h), passes %d day \n",addSeconds(-86400));
 printf("Now is: ");
  print();
  if (isMorning())
     printf("and it's morning");
  else
     printf("and it's not morning");
}

and code from myTime.h is:

#ifndef myTimeH    
#define myTimeH    


#include <iostream>
#include <sysutils.hpp>
//definicja klasy
class myTime {
   public:  

      myTime (void);  
      myTime (int nsec);  
      myTime (int h, int min, int sec);   
      myTime (const myTime &obj); 
      ~myTime (); // destruktor
      bool isMorning(); 
      int addHours(int n);   
      int addMinutes(int n); 
      int addSeconds(int n);
      void print () const ; 
      void test();       
      inline int getSecond() const;   
      inline int getMinute() const; 
      inline int getHour() const;  
      void setHour(int h);  
      void setMinute(int m); 
      void setSecond(int s); 
      bool isAfternoon();   
      myTime copyMe(myTime obj);    
      int timeToSec();  
   private: 
      int second;
      int minute;
      int hour;

      void secToTime(int nsec);   // plow horse method
};

#endif

after building project all the time i recieve "E2316 'identifier' is not a member of 'struct' Compiler error"

i am complete noob and have no idea how to correct this mistake maybe someone will help me!

thanks in advance

Recommended Answers

All 2 Replies

First try to compile myTime.cpp , which compiler are you using ?

Borland C++ Builder. The process of compiling this files is to get the project with start.cpp and then add myTime.cpp with the myTime.h. whole project is needed to be compiled to exe file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.