What's wrong with this array declaration? - c++

I created this array this array inside a function, with the variable MODEL_VERTEX_NUM initialized # runtime, which, I guess, is the sticking point here.
loat model_vertices [][]= new float [MODEL_VERTEX_NUM][3];
I got the following errors:
1>.\GLUI_TEMPLATE.cpp(119) : error C2087: 'model_vertices' : missing subscript
1>.\GLUI_TEMPLATE.cpp(119) : error C2440: 'initializing' : cannot convert from 'float (*)[3]' to 'float [][1]'
I realize that when I do:
float model_vertices *[]= new float [MODEL_VERTEX_NUM][3];
The compiler lets this pass, but I wanna understand what's wrong with the previous declaration.
So, what's wrong with the [][] declaration?

For a two-dimensional array a[X][Y] the compiler needs to know Y to generate code to access the array, so you need to change your code to
float (*model_vertices) [3] = new float [2][3];
If you have an array of type T a[X][Y] and want to access a[x][y] that is equivalent to accessing *(((T*)(&a[0][0])) + x*Y + y). As you can see the compiler needs to know Y but not X to generate code for accessing the array.

Related

C++: Type of multidimensional array with `variable` size

I can run this
int a = 5;
auto foo = new int [a][4][4];
But when I try this:
int a = 5;
int * foo[4][4];
foo = new int [a][4][4];
I get the error
error: incompatible types in assignment of ‘int (*)[4][4]’ to ‘int* [4][4]’
Question
What type do I have to specify for foo?
Edit:
The goal is to have one single chunk of memory, not an array of pointers.
The error message is a little confusing because it does not state the variable name.
This works:
int a = 5;
int (*foo)[4][4];
foo = new int [a][4][4];
As #john correctly identified:
You're confused between a 2D array of pointers (that's what you wrote) and a pointer to a 2D array (that's what you want).
So what's the difference between pointer to an array and array of pointers. The correct syntax to define a pointer to an array (what you tried to do):
data_type (*var_name)[array_size];
But this defines an array of pointers (what you actually did):
data_type *var_name[array_size];
#OP in your own answer you already found out what the correct type should be – a pointer to an array int (*foo)[4][4], but I thought a little more explanation is also helpful.

Please solve these errors. I'm using dev++ compiler [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Kindly tell me what's wrong with the code. It is showing many errors. The error statements and problem statement is given below:
First error is in the overloaded dereference operator. It says pmemory was not declared in this scope.
2nd error is in the main function where I use
ptrFloat ptr1 = &var1;
it shows error "conversion from 'Float' to non-scalar type 'ptrFloat' requested".
And in the next line of above mention statement save error..
in next line it show that (no match for operator <<) .
and in the very next line it show the same error as above.(no match for operator).
And at last it show again the same error that (no match for operator)
The question statement is given below:
Remember fmem_top is an index to fmemory which points to the next available place where a float value can be stored. And pmem_top is the similer index to pmemory.
Create a class called Float . We’ll use it to model numbers of type float that are stored in fmemory instead of real memory. The only instance data in Float is its own “address”; that is, the index where its float value is stored in fmemory. Call this instance variable addr. Class Float also needs two member functions. The first is a one-argument constructor to initialize the Float with a float value. This constructor stores the float value in the element of fmemory pointed to by fmem_top , and stores the value of fmem_top in addr . This is similar to how the compiler and linker arrange to store an ordinary variable in real memory. The second member function is the overloaded & operator. It simply returns the pointer (really the index, type int ) value in addr .
Create a second class called ptrFloat . The instance data in this class holds the address(index) in pmemory where some other address (index) is stored. A member function initializes this “pointer” with an int index value. The second member function is the overloaded * (dereference, or “contents of”) operator. Its operation is a tad more complicated.
It obtains the address from pmemory , where its data, which is also an address, is stored. It then uses this new address as an index into fmemory to obtain the float value pointed to by its address data.
float& ptrFloat::operator*()
{
return fmemory[pmemory[addr]];
}
#include<iostream>
#include<conio.h>
using namespace std;
class Float
{
protected:
float *fmem_top;
float **addr;
float fmemory[10];
public:
Float(float a)
{
fmem_top=fmemory;
addr=&fmem_top;
*fmem_top=a;
fmem_top++;
}
Float operator & ()
{
return *fmem_top;
}
};
class ptrFloat: public Float
{
private:
int *pmem_top;
int pmemory[10];
public:
ptrFloat(int abc) : Float(abc)
{
pmem_top=pmemory;
*pmem_top=abc;
pmem_top++;
}
ptrFloat operator * ()
{
return fmemory[pememory[addr]];
}
};
int main ()
{
Float var1 = 1.234;
Float var2 = 5.678;
ptrFloat ptr1 = &var1;
ptrFloat ptr2 = &var2;
cout<<"*ptr1 = "<< *ptr1;
cout<<"*ptr2 = "<< *ptr2;
*ptr1 = 7.123;
*ptr2 = 8.456;
cout<<"*ptr1 = "<< *ptr1;
cout<<"*ptr2 = "<< *ptr2;
getch();
return 0;
}
I'm not sure what's going on here, but this is what Clang has to say:
prog.cc:33:48: error: array subscript is not an integer
ptrFloat operator*() { return fmemory[pmemory[addr]]; }
^~~~~
There's a typo in the line here - you'd written pememory. But addr is a float**.
prog.cc:40:12: error: no viable conversion from 'Float' to 'ptrFloat'
ptrFloat ptr1 = &var1;
^ ~~~~~
ptrFloat is a class derived from Float, not a Float*.
What's your intent with this code?
As an aside, the clang compiler has great and helpful error messages. If you don't/can't install clang on your computer - I think it's a pfaff to set it up on Windows, I don't know - you can quickly test your code on an online compiler such as Wandbox and see if clang's output gives better clues. Here's your code running there - I had to strip out conio.h and getch as they're Windows-only: http://melpon.org/wandbox/permlink/4iCa61Rqysqc7Lpl
The first error is a typo you wrote pememory.
The second error appears because you didn't code any transformation between Float and ptrFloat.
You should add a copy constructor to Float:
Float(const Float& a) : Float(*a.fmem_top) {}
And then a conversion constructor to ptrFloat:
ptrFloat(Float a) : Float(a) {
pmem_top=pmemory;
*pmem_top=abc;
pmem_top++;
}
PS: your code has a lot of problems. I don't really get what you are trying to do, but, for instance, you can't have float typed variables as array subscripts.

Pointer on a float matrix is considered as *uchar

I'm trying to iterare trough a cv::Mat with a pointer.
So I did the following in my function:
template<typename Tin=uchar,typename Tout=float>
inline cv::Mat_<Tout> dct(const cv::Mat_<Tin>& oBlock) {
cv::Mat_<Tout> oOutput(oBlock.size());
Tout *pointeurOut= oOutput.data;
//...
}
However, I'm getting an error at the last line (Tout *pointeurOut= oOutput.data;).
'initializing': cannot convert from 'uchar *' to 'float *'
Where does that come from?
data is always a uchar*.
You should use
oOutput.ptr<Tout>();
to get the Tout pointer to the i-th row (by default is the 0-th row, i.e. the beginning of the image)

Typecasting DXVA2_Fixed32 to Float in C++

DXVA2_Fixed32 a = DXVA2_Fixed32OpaqueAlpha();
float f = (float)a;
This is throwing a compilation error
"error C2440: Cannot convert from "DXVA2_Fixed32 " to Float.
My purpose is to assign "a"'s value to "f".
Can anyone kindly let me know How to assign DXVA2_Fixed32 type variable "a" to "a float variable "f".
Thanks in advance.
You can't do it with a typecast like that. The DXVA2_Fixed32 type is a struct containing two fields with fractional and integer parts of the number.
You need to call DXVA2FixedToFloat to perform the conversion.
float f = DXVA2FixedToFloat(a);
If ever you need to go in the opposite direction you can use the predictably named DXVA2FloatToFixed.

C++ help with error : cannot convert parameter 1 from 'float' to 'float [][2]

and I was doing a program, that isn't so important. Apparently, I cant send parameters to a float function.
the code look something like this
float myfunction(float array[1][2])
{
// ...
return 0;
}
int main()
{
float array[1][2];
int foo = 0;
// assigning values to the array
foo = myfunction(array[1][2]);
return 0;
}
when I try to compile, I get the error "cannot convert parameter 1 from 'float' to 'float [][2]"
What is wrong? And how can I solve it?
Just pass array, without the indexes:
foo = myfunction(array);
A couple of things.
First, a function prototyped float myfunction(float array[1][2]) is confusing (you), since what it actually mean is: float myfunction(float array[][2]) or float myfunction(float (*array)[2]). The function accepts a pointer to (one or more) array(s) of two floats.
Second, the error you get is because the function accepts a pointer to an array, while you're tryinmg to pass it single float - element [1][2] of the two-dimensional array float array[1][2]. Perhaps you meant to pass the entire array to the function?
You've defined the variable: float array[1][2];
Then, you call the function in this way: foo = myfunction(array);
As a parameter, only you have to set the variable name. You shouldn't do this: foo = myfunction(array[1][2]);
When you make a function, do it in this way: type myfunction(float Array[][w]) , being type the type of function (void, float...) and "w" a constant integer.
You are passing a certain cell and not the array