1

in a class i have a public attribute TissueCompartments:

class Diver {
public:
    TissueCompartments tissueCompartments[16];
    Diver();
    Diver(const Diver& orig, TissueCompartments tissueCompartments[16]);
    virtual ~Diver();
};

and i have a constructor

Diver::Diver(const Diver& orig, TissueCompartments tissueCompartments[16]) {
    this->tissueCompartments=tissueCompartments;
}

i get this error: error: incompatible types in assignment of 'TissueCompartments*' to 'TissueCompartments [16]'

4 Answers 4

5

Arrays are not assignable. Do this instead in your constructor body:

#include <algorithm>
//..
Diver::Diver(const Diver& orig, TissueCompartments* tC)
{
    std::copy(tC, tC + 16, tissueCompartments);
    //...
}

Also, please be aware that this declaration:

Diver::Diver(const Diver& orig, TissueCompartments tissueCompartments[16]) 

is no different than this:

Diver::Diver(const Diver& orig, TissueCompartments* tissueCompartments)

Arrays decay to pointers, even when you place [16] in your parameter. So that [16] has absolutely no effect (except to aid in letting you know it is an array of 16 that is being referred to).

Please also note that the TissueCompartment type needs to be an assignable type before doing any of these changes.

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

2 Comments

Let's hope TissueCompartements are assignable.
@Deduplicator - Yes, that too. Those types must be assignable.
1

Arrays can't be assigned. Another thing which trips people up is the fact that functions can't take arrays by value - the compiler implicitly decays array arguments to pointers. If you want an array with value semantics, use std::array.

class Diver {
public:
    std::array<TissueCompartments, 16> tissueCompartments;
    Diver();
    Diver(const Diver& orig, std::array<TissueCompartments, 16> tissueCompartments);
    virtual ~Diver();
};

Diver::Diver(const Diver& orig, std::array<TissueCompartments, 16> tissueCompartments) {
    this->tissueCompartments=tissueCompartments;
}

1 Comment

Your changed code needlessly assumes TissueCompartments are assignable.
1

While arrays aren't assignable, you can get around that limitation in your example easily, using a brace-initializer.
I used constructor-delegation and std::index_sequence due to constructive laziness:

#include <utility>

template<size_t... i> Diver::Diver<i...>(const Diver& orig,
        TissueCompartments tissueCompartments, std::index_sequence<i...>)
    : tissueCompartments{tissueCompartments[i]...}
{}

Diver::Diver(const Diver& orig, TissueCompartments tissueCompartments[16])
    : Diver(orig, tissueCompartments, std::make_index_sequence(16) {}

The helpers are C++14, but only use C++11 features.

Comments

0

As with any question involving arrays, your first thought should be to stop using arrays, and use vector instead.

#include <vector>
class Diver {
public:
    std::vector<TissueCompartments> tissueCompartments;
    Diver();
    Diver(const Diver& orig, std::vector<TissueCompartments> tissueCompartments);
    virtual ~Diver();
};

then you can do:

Diver::Diver(const Diver& orig, std::vector<TissueCompartments> tissueCompartments) {
    this->tissueCompartments=tissueCompartments;
}

and, if you're using C++11, you should be able to construct a Diver with an initializer list, assuming that a Diver is constructed by an int and a string:

Diver d{ {1,"hi"}, {2,"hi"}, {3,"hi"}, {4,"hi"}
       , {5,"hi"}, {6,"hi"}, {7,"hi"}, {8,"hi"}
       , {9,"hi"}, {10,"hi"}, {11,"hi"}, {12,"hi"}
       , {13,"hi"}, {14,"hi"}, {15,"hi"}, {16,"hi"}
       };

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.