0

The error confronting me is the compiler saying cannot convert ‘double*’ to ‘double’ in assignment. My code is below.

double* initArray(int data[], int dimensionCount){
    //data: element 0= number of dimensions
    //element 1 through inf define the size of each dimension
    //dimensionCount keeps track of the current dimension being manipulated by the function


    //Allocate dynamic memory for the current dimension
    double* output;
    output=new double[data[dimensionCount]];

    int i=dimensionCount;

    while(i<data[dimensionCount]){
        if( !(output[i]= initArray(data, dimensionCount++))){
            std::cout<< "Error! Out of Memory!";
            break;
        }

    i++;
    }
//returning the generated array tacks on the generated dimension to the lower dimension preceding it
return output;
}

As output is of type double* and arrayInit returns variables of type double*, I don't know where it is trying to convert from a double to a double*. I found this, but it doesn't seem to apply, as data is being passed correctly, and intArray is returning a pointer to the array being generated, not the array itself, so there shouldn't be any type mismatch with output.

Community
  • 1
  • 1
Graf
  • 11
  • 7
  • `data[]` is most likely not what you think it is, you probably just mean `data` in that context. Note that `initArray(int data[]...` is equivalent to `initArray(int *data...` However, this is rather C-ish C++. You should definitely get better learning material. Remark: If you are doing this for any reason but academic curiosity, what you need is most likely already implemented and ready to use. – Baum mit Augen Apr 28 '16 at 00:36
  • To emphasize, `new[]` is *really* rarely useful in the real world, `std::vector` does everything it does and more and better. – Baum mit Augen Apr 28 '16 at 00:42
  • I'm just doing it to try and figure it out. :D I am also trying to make a library without using anything other than `iostream` and `string`. You are right on data I think- on closer look, wouldn't that be an array element I am trying to reference, without a key telling the array what cell I am referencing? I'll try changing that to `data` and see how that works. Thanks! – Graf Apr 28 '16 at 00:45
  • I'll look into `std::vector`- although I am interested in memory management, I probably don't need to get too deep in that for this project if there is a better option. – Graf Apr 28 '16 at 00:46
  • I changed `data[]` to `data`, but now my compiler is saying `cannot convert ‘double*’ to ‘double’ in assignment`. `initArray` is returning a `double*`, and `output` is also of type `double*`, so I am not sure what needs to be changed to the correct type. – Graf Apr 28 '16 at 00:51
  • 1
    First of all, by "C-ish C++" I mean that pointer business with manual memory management. RAII is one one of the key principles of "Why C++ instead of C?". Now back to your real problem: If your question is "How to resolve that compilation error?", please post a [mcve] and I'm certain someone will help you fairly fast. If you care how to create a N-dim matrix in general: Stroustrup (the creator of C++) presents that in his book "The C++ programming language". (A useful book to learn C++ in my opinion(!).) – Baum mit Augen Apr 28 '16 at 01:00
  • (Just for you: `output[i]` is a `double`, but `initArray` returns a `double*`, which causes the error, but don't tell the Meta Police. ;) ) – Baum mit Augen Apr 28 '16 at 01:04
  • Alright! Thank you! I'm guessing that appending the next part of the issue to the bottom of the question is a no go? Do I just post the code, say what the error is, and then ask what the problem is? I went through the explanation because I thought that the problem was stemming from something wrong with my algorithm, but as that doesn't seem to be the case, do I edit that back? Thanks fot the book recommendation, I'll try and pick that up! :D – Graf Apr 28 '16 at 01:15
  • Wait.. you're making this into a library? It is extremely rare that you'll ever need MORE than 4 dimensions.. You should look up the 3 star programmer.. I mean.. it's an interesting topic, but I can't see "usage" for it. – Brandon Apr 28 '16 at 01:18
  • It's a calculus final project, and I really wanted to impress my teacher with N-Dimensional vector math... :P Also it is cool. – Graf Apr 28 '16 at 01:19
  • @Brandon Well, tensor math is a thing, so this is not unreasonable a-priori. – Baum mit Augen Apr 28 '16 at 01:20
  • After figuring out that pointers point to a location and don't directly hold the value, the type issues went away. Do I edit my question to make it more concise and then answer it myself? Is that bad form? – Graf Apr 28 '16 at 01:29
  • @Graf *"I'm guessing that appending the next part of the issue to the bottom of the question is a no go?"* Lengthy discussions in the comments queue fundamental edits to the OP are not a good fit to this Q/A format, yes. It would be good if you could narrow down your issue to something that has something like a complete (not necessary unique) answer and either edit this to the aforementioned or post it as a new question. (I don't know the implications of those variants, but unless you screw up big time big time multiple times, this community tends to be rather nice.) – Baum mit Augen Apr 28 '16 at 01:30
  • Self answers are not a bad form at all, but you probably should not expect a myriad of upvotes for solving a basic issue. That being said: Keep at it, you seem like a very good and motivated member of this community. :) – Baum mit Augen Apr 28 '16 at 01:32
  • Thanks! I'm happy to be joining! :D – Graf Apr 28 '16 at 01:34

2 Answers2

1

I played with the function more and realised that although output is a double* and does not conflict with what is returned by initArray, output[i] is just a regular double, as it is what output is pointing to. The type mismatch was resulting from trying to set output[i] to a double*.

To solve the problem, I modified the function to return a double instead of a double*, and then made sure that output was dereferenced into a new variable array. This allowed a normal array to be returned by the function, preventing the type mismatch, and also making the actual result of the function that much more usable.

Here is the code:

double initArray(int data[], int dimensionCount){
//data: element 0= number of dimensions
//element 1 through inf define the size of each dimension
//dimensionCount keeps track of the current dimension being manipulated by the function


//Allocate dynamic memory for the current dimension
double* output;
output=new double[data[dimensionCount]];

int i=dimensionCount;

while(i<data[dimensionCount]){
    if( !(output[i]= initArray(data, dimensionCount++))){
        std::cout<< "Error! Out of Memory!";
        break;
    }

    i++;
}
//returning the generated array tacks on the generated dimension to the lower dimension preceding it
double array=*output;
return array;
}
Graf
  • 11
  • 7
  • This function is not returning an array, it’s returning a `double` – and moreover, a double which hasn’t actually been initialised to any particular value. As far as I can see, this function accomplishes nothing except to leak memory and return garbage. And that’s assuming it doesn’t get stuck in infinite recursion and, well, overflow your stack! – Entropy Apr 28 '16 at 03:14
  • I thought arrays weren't a type. A `double` can store an array with values of type `double`. I have moved to working on other parts of my library and plan on debugging this when I haven't messed with it for a while (in the hopes I get some new insight on the problem or whatever). In theory, this function generates an array of N-dimension. The `output` pointer points to an array, and each loop adds a dimension to it... or that is what I intended. It doesn't do that, as it isn't done yet. :P – Graf May 01 '16 at 16:48
  • It also isn't intended to produce a value. I am aiming to get a skeleton of an array for which I can use to store N-Dimensional coordinates or vectors so I can do !!MATH!!. – Graf May 01 '16 at 16:49
  • I never said arrays were a type. You were the one who used the word, when you said “…This allowed a normal array to be returned by the function…”. Unless you’re using some newfangled feature of C++ that I’m not aware of, you are mistaken: A `double` CANNOT store an array, it can only store a single value. – Entropy May 09 '16 at 10:22
  • When you dereference your array pointer, you don’t get the entire array, you only get the value contained in the first element. In other words, writing `double array = *output;` is equivalent to writing `double array = output[0];`. Last time I checked, there was no array type in C++, and NO WAY to deal with arrays, except by pointer. I’ve been doing vector “!!MATH!!”, as you call it, in C++ for years, and I can’t see any sense in the function you’ve written. – Entropy May 09 '16 at 10:22
0

Simply

return *output;

would do the trick. Any specific reason for using another variable?.

Ali Kazmi
  • 1,460
  • 9
  • 22
  • I thought it was more clear. I am still learning, and `*output` doesn't immediatly click in my head, whereas `array=*output` reminds me that `*output` is dereferencing `output`. – Graf May 01 '16 at 16:42