how to pass arrays as parameters in constructor? c++
arrays, c++, constructor, parameters, pointers
Solution
Raw arrays are non-assignable and generally difficult to handle. But you can put an array inside a `struct`, and assign or initialize that. Essentially that's what `std::array` is.
E.g. you can do
typedef std::array<int, 8> num_t;
typedef std::array<char, 14> time_t;
class call_t
{
private:
num_t from_;
num_t dest_;
time_t init_;
time_t end_;
public:
call_t(
num_t const& from,
num_t const& dest,
time_t const& init,
time_t const& end
)
: from_t( from ), dest_( dest ), init_( init ), end_( end )
{}
};
But this still lacks some essential abstraction, so it's merely a technical solution.
To improve things, consider what e.g. `num_t` really is. Is it, perhaps, a telephone number? Then model it as such.
Consider also using standard library containers `std::vector` and, for the arrays of `char`, `std::string`.
Problem
I am trying to make a constructor for class call, in which 4 arrays are passed as parameters. I've tried using `*,&`, and the array itself; however when I assign the values in the parameters to the variables in the class, I get this error : ``` call.cpp: In constructor ‘call::call(int*, int*, char*, char*)’: call.cpp:4:15: error: incompatible types in assignment of ‘int*’ to ‘int [8]’ call.cpp:5:16: error: incompatible types in assignment of ‘int*’ to ‘int [8]’ call.cpp:6:16: error: incompatible types in assignment of ‘char*’ to ‘char [14]’ call.cpp:7:16: error: incompatible types in assignment of ‘char*’ to ‘char [14]’ ``` I would appreciate your help in finding my error and helping me correct it. here is my code: .h file ``` #ifndef call_h #define call_h class call{ private: int FROMNU[8]; int DESTNUM[8]; char INITIME[14]; char ENDTIME[14]; public: call(int *,int *,char *,char *); }; #endif ``` .cpp file ``` call:: call(int FROMNU[8],int DESTNUM[8],char INITIME[14],char ENDTIME[14]){ this->FROMNU=FROMNU; this->DESTNUM=DESTNUM; this->INITIME=INITIME; this->ENDTIME=ENDTIME; } ```