Hello all!

So basically I start off with one array, then I run a function that takes 3 floats, puts those 3 floats into a temporary array, then it returns this new temporary array and where I call this fucntion, It looks like this:

float array [] = { 0, 0, 0 ];

float ArrayFunction( float num1, float num2, float num3 )
{
    float temparray [] = { 1.0, 2.0, 3.0 };
    return temparray;
}

main...

    array = FunctionName( num1, num2, num3 );

To me, this looks perfect, exactly what I wanted it to do, but to C++, this is code filled with errors:

1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(57) : error C2440: 'return' : cannot convert from 'float [3]' to 'float'
1>        There is no context in which this conversion is possible
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(61) : error C2109: subscript requires array or pointer type
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(62) : error C2109: subscript requires array or pointer type
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(63) : error C2109: subscript requires array or pointer type
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(66) : warning C4244: 'argument' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(68) : warning C4244: 'argument' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(68) : warning C4244: 'argument' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(70) : warning C4244: 'argument' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(70) : warning C4244: 'argument' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(82) : error C2440: '=' : cannot convert from 'float' to 'float [3]'
1>        There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\users\anton\downloads\bug-fixes-2009-07-11\opengl 3.0\chapter_1\simple\src\main.cpp(83) : error C2664: 'CreateTriangle' : cannot convert parameter 1 from 'float [3]' to 'float'
1>        There is no context in which this conversion is possible

Anyone have an idea of what's going on?

Thanks in advance for any replies, Anton

Edited 6 Years Ago by scar164: n/a

Your temparray is exactly that. It's created within the local block of the function and destroyed when the function exits, unfortunately.

Pass in an array by pointer so that your function signature looks like

void arrayfunction(float arr[],float flt1,float flt2,float flt3)
{
   //load up the array here

   //still exists on exit

}

I suspect since you are using opengl that your function is really more complicated than this.

What you have above could work with some modifications, but I think the above way is much more straightforward.

Comments
Thanks for your help!

Yay, I got it too work, and no my function is exactly this. What I am doing is creating two functions for creating and coloring a triangle. ColorTriangle takes the array rgb and 3 numbers represting r, g and b values then CreateTriangle takes the rgb which has been modified in ColorTriangle, but also an x, y and z coordinate. Then it creates the Triangle with those values.
Thanks for your help!

Yay, I got it too work, and no my function is exactly this. What I am doing is creating two functions for creating and coloring a triangle. ColorTriangle takes the array rgb and 3 numbers represting r, g and b values then CreateTriangle takes the rgb which has been modified in ColorTriangle, but also an x, y and z coordinate. Then it creates the Triangle with those values.
Thanks for your help!

Why wouldn't you use structs for this?

struct Color{
 float red, blue, green;
 Color(float r = 0, float g = 0, float b = 0)
   : red(r), green(g), blue(b) {}
};

struct Triangle{
 float vertices[3];
 Color color;
 Triangle() : vertices() {}
 Triangle(const Color& c) : color(c), vertices(){}
};

Triangle redTriangle(){
 Color red = Color(1.0f,0.0f,0.0f);
 Triangle redTriangle(red);
 return redTriangle;
}
This question has already been answered. Start a new discussion instead.