You doubt how to assign objects to an array of pointers, created with new

2

Someone could tell me how to assign objects during the code, to an array of this type.

Time  * pTArray=new Time[2];

I do not want to initialize, the array in the creation, that is, I do not want this:

Time  * pTArray=new Time[2]{Time(1,2,3),Time(12,13,14)};

but I want to do something of the type:

pTArray[0]=new Time(1,2,3);

But this does not compile me. Any idea of how I can assign Time objects to my array.

Error displayed:

**** Build of configuration Debug for project Time ****

**** Internal Builder is used for build               ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o Time_test.o ..\Time_test.cpp
..\Time_test.cpp: In function 'int main()':
..\Time_test.cpp:8:30: error: ambiguous overload for 'operator=' (operand  types are 'Time' and 'Time*')
pTArray[0]=new Time(1,2,3);
                          ^
In file included from ..\Time_test.cpp:3:0:
..\Time.h:5:7: note: candidate: constexpr Time& Time::operator=(const Time&) <near match>
   class Time {
       ^~~~
 ..\Time.h:5:7: note:   conversion of argument 1 would be ill-formed:
 ..\Time_test.cpp:8:16: error: invalid user-defined conversion from 'Time*'    to 'const Time&' [-fpermissive]
  pTArray[0]=new Time(1,2,3);
            ^~~~~~~~~~~~~~~
In file included from ..\Time_test.cpp:3:0:
..\Time.h:12:4: note: candidate is: Time::Time(int, int, int) <near match>
 Time(int h = 0, int m = 0, int s = 0);
 ^~~~
..\Time.h:12:4: note:   conversion of argument 1 would be ill-formed:
..\Time_test.cpp:8:16: error: invalid conversion from 'Time*' to 'int' [-   fpermissive]
     pTArray[0]=new Time(1,2,3);
            ^~~~~~~~~~~~~~~
..\Time_test.cpp:8:16: error: invalid conversion from 'Time*' to 'int' [-fpermissive]
In file included from ..\Time_test.cpp:3:0:
..\Time.h:12:4: note:   initializing argument 1 of 'Time::Time(int, int, int)'
Time(int h = 0, int m = 0, int s = 0);
^~~~
 ..\Time.h:5:7: note: candidate: constexpr Time& Time::operator=(Time&&)   <near match>
 class Time {
   ^~~~
..\Time.h:5:7: note:   conversion of argument 1 would be ill-formed:
..\Time_test.cpp:8:16: error: invalid user-defined conversion from   'Time*' to 'Time&&' [-fpermissive]
  pTArray[0]=new Time(1,2,3);
            ^~~~~~~~~~~~~~~
In file included from ..\Time_test.cpp:3:0:
..\Time.h:12:4: note: candidate is: Time::Time(int, int, int) <near match>
Time(int h = 0, int m = 0, int s = 0);
^~~~
 ..\Time.h:12:4: note:   conversion of argument 1 would be ill-formed:
 ..\Time_test.cpp:8:16: error: invalid conversion from 'Time*' to 'int' [-fpermissive]
     pTArray[0]=new Time(1,2,3);
            ^~~~~~~~~~~~~~~
  ..\Time_test.cpp:8:16: error: invalid conversion from 'Time*' to 'int' [-fpermissive]
  In file included from ..\Time_test.cpp:3:0:
  ..\Time.h:12:4: note:   initializing argument 1 of 'Time::Time(int, int, int)'
Time(int h = 0, int m = 0, int s = 0);
^~~~
  ..\Time_test.cpp:8:30: error: conversion to non-const reference type     
 class Time&&' from rvalue of type 'Time' [-fpermissive]
 pTArray[0]=new Time(1,2,3);
                          ^
 Build error occurred, build is stopped
 Time consumed: 1065  ms.  

Time.h:

#ifndef TIME_H
#define TIME_H

class Time {
private:
   int hour;     // 0 - 23
   int minute;   // 0 - 59
   int second;   // 0 - 59

 public:
   Time(int h = 0, int m = 0, int s = 0);
   int getHour() const;
   void setHour(int h);
   int getMinute() const;
   void setMinute(int m);
   int getSecond() const;
   void setSecond(int s);
   void setTime(int h, int m, int s);
   void print() const;
   Time nextSecond() ;
};

#endif

method definition:

  /* Implementation for the Time Class (Time.cpp) */
  #include <iostream>
  #include <iomanip>
  #include <stdexcept>    // Needed for exception handling
  #include "Time.h"
  using namespace std;

  Time::Time(int h, int m, int s) {
  // Call setters to perform input validation
  setHour(h);
  setMinute(m);
  setSecond(s);
  }

 Time Time::nextSecond() {
    if (++second == 60) {
       second = 0;
       if (++minute == 60) {
           minute = 0;
           if (++hour == 24) {
              hour = 0;
          }
      }
   }
   return *this;  // Return this instance by reference
              // "this" is a pointer to this instance. *this refers to     
                 this instance.
}

int Time::getHour() const {
     return hour;
}

void Time::setHour(int h) {  // with input validation
     if (h >= 0 && h <= 23) {
        hour = h;
     } else {
        throw invalid_argument("Invalid hour! Hour shall be 0-23.");
            // need <stdexcept>
     }
 }

  int Time::getMinute() const {
      minute;
}

void Time::setMinute(int m) {
     if (m >= 0 && m <= 59) {
         minute = m;
     } else {
         throw invalid_argument("Invalid minute! Minute shall be 0-59.");
           // need <stdexcept>
    }
}

int Time::getSecond() const {
      return second;
}

 void Time::setSecond(int s) {
     if (s >= 0 && s <= 59) {
        second = s;
     } else {
        throw invalid_argument("Invalid second! Second shall be 0-59.");
        // need <stdexcept>
     }
 }

 void Time::setTime(int h, int m, int s) {
    // Call setters to validate inputs
        setHour(h);
        setMinute(m);
        setSecond(s);
 }

 void Time::print() const {
 cout << setfill('0');
 cout << setw(2) << hour << ":" << setw(2) << minute << ":"
       << setw(2) << second << endl;
 }

Greetings, Julio.

    
asked by Jcpardo 28.04.2018 в 02:12
source

1 answer

0

Simple, as a standard arrangement:

Example:

class Test
{
    public:
        int a;
        Test(int x) : a(x) {};

};

int main()
{
    Test* p[2] = { new Test(1), new Test(4) };  

    std::cout << p[0]->a << " | " << p[1]->a << std::endl;
}
    
answered by 28.04.2018 в 20:27