Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
class Array {                                           

public:

    Array(unsigned h, unsigned l, std::initializer_list<double>);
    ....
private:
    unsigned length;
    double * array;
...
};

Array::Array(unsigned l, std::initializer_list<double> il) :
length(l)
{
    array(new (std::nothrow) array[l]);
    if (!array)
        throw OutOfMem();
    for (auto el = il.begin(), int i = 0; i < length; i++)
    {
            if (el === il.end())
                throw WrongDim();
            array[i] = *el;
            el++;
        }
    }
}

It's justthe draft of the original class which i have to do for my assignment. Compiling results in error:

invalid use of non-static data member 'length' unsigned length;

Anyone got any clue how to fix this?

share|improve this question
    
That's pretty confusing code you have there. You're missing h from the ctor definition, so probably the compiler doesn't parse it as a ctor, but something weird. Second, the initialisation of array should probably be in the mem-initializer-list as well, and you probably meant new (std::nothrow) double[l]. –  Angew yesterday
    
When you post questions about error, please always include the complete error log in the question, and also mark out where in the source the errors are. –  Joachim Pileborg yesterday
    
the full error log contains more than 6 pages, most of which as i can observe is the result of bad allocation. That's why i was asking about it. The problem is i don't know why even tough i always have to initialize those values i cannot use them to allocate dynamic array in a constructed object –  user3119781 yesterday

1 Answer 1

what about:

int i=0;
for (auto &el : il)
    array[i++] = el;

if (i!=l) 
    throw WrongDim();
share|improve this answer
    
Please explain how this code helps the OP and further readers of this thread to solve the proble. You can find more about answering guidelines in the help center: How to Answer –  Pred yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.