Alguien me podría indicar como asignar objetos durante el código, a un array de este tipo.
Time * pTArray=new Time[2];
No quiero inicializar, el array en la creación, es decir no quiero esto:
Time * pTArray=new Time[2]{Time(1,2,3),Time(12,13,14)};
sino que quiero hacer algo del tipo:
pTArray[0]=new Time(1,2,3);
Pero esto, no me compila.Alguna idea de como puedo asignar objetos Time a mi array.
Error mostrado:
**** 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
definición de métodos:
/* 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;
}
Saludos, Julio.