4

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;
}
3
  • Replace the arrays by std::array or std::tr1::array if you don't have C++11 support (or altenratively, boost::array) Commented Feb 18, 2013 at 0:52
  • Arrays are not the same thing as pointers, although in many cases they are demoted to pointers. For your use case, consider std::array instead of [] arrays. Commented Feb 18, 2013 at 0:52
  • 8
    You know variables can be lowercase, right? Commented Feb 18, 2013 at 0:56

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Passing a raw array as an argument is possible in C++.

Consider the following code:

template<size_t array_size>
void f(char (&a)[array_size])
{
    size_t size_of_a = sizeof(a); // size_of_a is 8
}

int main()
{
    char a[8];
    f(a);
}

2 Comments

You're using C++11's auto in your code sample. Who in their right mind would have access to C++11 and use that instead of std::array?
'auto' is not essential here. I replaced it with explicit 'size_t'
0

In C/C++ you cannot assign arrays by doing this->FROMNU=FROMNU; thus your method wont work, and is one half of your error.

The other half is that you try to assign a pointer to the array. Even if you pass arrays to a function, they decay to pointers to the first element, despite what you say in the definition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.