this isn't quite working out. Let's see if we can collectively expand our knowledge on this one. Okay:
vector<vector<Point>> aVectorOfPoints
int main(){
someConstructor(&aVectorOfPoints)
}
someConstructor(vector<vector<Point>>* aVectorOfPoints){
functionOne(aVectorOfPOints);
}
functionOne(vector<vector<Point>>* aVectorOfPOints){
aVectorOfPoints[i][j] = getPointFromClass();
}
//functionX(...){...}
I'm getting some error underneath the assignment in functionOne. How can I better do this? Thanks.
The specific error is "No operator '=' matches these operands".
Why is this wrong?
aVectorOfPoints[i][j] = getPointFromClass();
type of aVectorOfPoints is vector<vector<Point>>*.
type of aVectorOfPoints[i] is vector<vector<Point>>.
type of aVectorOfPoints[i][j] is vector<Point>.
A Point cannot be assigned to a vector<Point>. Hence the compiler error.
Perhaps you meant to use:
(*aVectorOfPoints)[i][j] = getPointFromClass();
You can simplify the code by passing references.
int main(){
someConstructor(aVectorOfPoints)
}
someConstructor(vector<vector<Point>>& aVectorOfPoints){
functionOne(aVectorOfPOints);
}
functionOne(vector<vector<Point>>& aVectorOfPOints){
aVectorOfPoints[i][j] = getPointFromClass();
}
Use references instead of pointers:
someConstructor( vector<vector<Point>> &aVectorOfPoints) {
and the same for functionOne.
Your mistake is that aVectorOfPoints[i] indexes the pointer by i. If using pointers you'd need to dereference the pointer first before doing that, by writing (*aVectorOfPoints)[i][j].
Related
i was reversing a source code and i've found a function it wich looks like:
consider this:
int examplefn(int x) { return x * 4; }
int (*rtx())(int)
{
return examplefn;
}
well, Then i needed make a pointer function to rtx() to do a hook, then
i've done something like this:
int (*fncptr())(int) = (int(*())(int))0xC0FFEE;
/* 0xC0FFEE it's a sample of the memory address of the function...*/
But my compiler did not compile it, then i've tried do:
typedef int(*fnc_t())(int);
// Clearer example pointing to rtx
fnc_t* TRY_2 = (fnc_t*)&rtx;
// then has successfully compiled, ex test...
int main()
{
std::cout << TRY_2()(4) << std::endl; // output: 16 ok.
}
well, i'm going to the point, ¿How i can do the correct casting without use a typedef?
I searched all over the internet and I have not found anything...
Why do you want to avoid using a typedef? It makes code so much easier to understand:
using F = int(*)(int); // pointer to function taking int and returning int
using G = F(*)(); // pointer to function taking nothing and returning
// a pointer to function taking int and returning int
This took me no time to write and everybody else no time to read and understand. I'd call that a win.
(int(*())(int)) is a function type (the same type as the function rtx has). Your code attempts to declare a function, and cast an integer to function. However you actually want to deal with a pointer to such a function.
After: typedef int(*fnc_t())(int);, the equivalent of fnc_t *x; can be found by replacing fnc_t with (*x) in the typedef: int (*(*x)())(int). So your code could be:
int (*(*fncptr)())(int) = (int(*(*)())(int))0xC0FFEE;
Using a series of typedefs (or equivalent usings) is certainly preferable in real code.
I know that the following, if possible, would be an absolutely bad practice, but I want to know if this is possible.
The question is the following: is it possible in C++ (and in a way the compiler does not throw any warning), to perform a useless arithmetic operation with a function returning a void.
std::vector<int> v;
int i = 42 + v.resize(42);
/* How to transform the last line to execute resize and to have i = 42 */
I know that this is stupid, but that is not the question...
I'm not sure it makes much sense, but you could use the comma operator here:
int i = (v.resize(42), 42);
You could use the comma operator:
int i = (v.resize(42), 42);
and with GCC you could use its statement expression extension:
int i = ({v.resize(42); 42;})
and in standard C++11 you could use and call an anonymous closure:
int i = ([&v]() {v.resize(42); return 42;}());
Type void has no values so it may not be used in arithmetic expressions.
In my opinion the design of member function resize is bad. Instead of voidit should return the object itself. In this case you could write for example
int i = v.resize(42).size();
I pointed out about this in the forum where the C++ Standard is discussed.
As for your question then you can write
int i = ( v.resize(42), v.size() );
using the comma operator.
Or maybe it would be better to separate these two calls
v.resize(42);
int i = v.size();
Don't see the point, but here's another way
std::tie(i, std::ignore) = std::make_tuple(42, (v.resize(42),1) );
Also you can do:
if ((i=42)) v.resize(42);
And don't forget
do { v.resize(42); } while (!(i=42));
And the favorite
(i=42) ? v.resize(42) : i;
Or (the only serious c++ in the post)
int i(0);
std::vector<int> v(i=42);
Come on, this has no end
.....
I have the following program in two files
main.cpp
float POW10[300];
main(0
{
Fill_POW10();
}
Fill.cpp
extern float *POW10;
Fill_POW10()
{
for(int i=0;i<300;i++)
{
POW10[i]=i;
}
}
This crashed with a segmentation fault. When I inspect, POW10 is NULL. However if I change Fill.cpp to
extern float POW10[];
Fill_POW10()
{
for(int i=0;i<300;i++)
{
POW10[i]=i;
}
}
the code works fine. I was thinking that POW10 is actually implemented as a pointer to floats and so the codes should be identical. Can you please explain why this is not so.
Arrays and pointers are completely different types. When you define a pointer variable, all you get is a single pointer that may or may not actually point anywhere. When you define an array, you get a contiguous sequence of objects.
You may be thinking of function argument types, where array types are transformed to pointer types. That is, void foo(int arg[]) is equivalent to void foo(int* arg). This is only true for function arguments.
First read this entry which explains your issue:
http://c-faq.com/aryptr/aryptr1.html
Then read this follow up which explains the differences between array and pointer.
http://c-faq.com/aryptr/aryptr2.html
The type of POW10 is array of 300 float. It is not pointer to float. When you change your extern declaration to match the definition the problem goes away.
Because the linker is not resolving your float * POW10 declaration to the float POW10[] definition, but actually creating a separate definition altogether, which ends up being uninitialized (NULL, as you experienced).
I am a bit confused. There are two ways to return an array from a method. The first suggests the following:
typedef int arrT[10];
arrT *func(int i);
However, how do I capture the return which is an int (*)[]?
Another way is through a reference or pointer:
int (*func(int i)[10];
or
int (&func(int i)[10];
The return types are either int (*)[] or int (&)[].
The trouble I am having is how I can assign a variable to accept the point and I continue to get errors such as:
can't convert int* to int (*)[]
Any idea what I am doing wrong or what is lacking in my knowledge?
If you want to return an array by value, put it in a structure.
The Standard committee already did that, and thus you can use std::array<int,10>.
std::array<int,10> func(int i);
std::array<int,10> x = func(77);
This makes it very straightforward to return by reference also:
std::array<int,10>& func2(int i);
std::array<int,10>& y = func2(5);
First, the information you give is incorrect.
You write,
“There are two ways to return an array from a method”
and then you give as examples of the ways
typedef int arrT[10];
arrT *func(int i);
and
int (*func(int i))[10];
(I’ve added the missing right parenthesis), where you say that this latter way, in contrast to the first, is an example of
“through a reference or pointer”
Well, these two declarations mean exactly the same, to wit:
typedef int A[10];
A* fp1( int i ) { return 0; }
int (*fp2( int i ))[10] { return 0; }
int main()
{
int (*p1)[10] = fp1( 100 );
int (*p2)[10] = fp2( 200 );
}
In both cases a pointer to the array is returned, and this pointer is typed as "pointer to array". Dereferencing that pointer yields the array itself, which decays to a pointer to itself again, but now typed as "pointer to item". It’s a pointer to the first item of the array. At the machine code level these two pointers are, in practice, exactly the same. Coming from a Pascal background that confused me for a long time, but the upshot is, since it’s generally impractical to carry the array size along in the type (which precludes dealing with arrays of different runtime sizes), most array handling code deals with the pointer-to-first-item instead of the pointer-to-the-whole-array.
I.e., normally such a low level C language like function would be declared as just
int* func()
return a pointer to the first item of an array of size established at run time.
Now, if you want to return an array by value then you have two choices:
Returning a fixed size array by value: put it in a struct.
The standard already provides a templated class that does this, std::array.
Returning a variable size array by value: use a class that deals with copying.
The standard already provides a templated class that does this, std::vector.
For example,
#include <vector>
using namespace std;
vector<int> foo() { return vector<int>( 10 ); }
int main()
{
vector<int> const v = foo();
// ...
}
This is the most general. Using std::array is more of an optimization for special cases. As a beginner, keep in mind Donald Knuth’s advice: “Premature optimization is the root of all evil.” I.e., just use std::vector unless there is a really really good reason to use std::array.
using arrT10 = int[10]; // Or you can use typedef if you want
arrT10 * func(int i)
{
arrT10 a10;
return &a10;
// int a[10];
// return a; // ERROR: can't convert int* to int (*)[]
}
This will give you a warning because func returns an address of a local variable so we should NEVER code like this but I'm sure this code can help you.
I am interested in Judy Arrays and try to use it. But i had unable to do any useful thing using it. Every time it gives me casting errors.. Sample c++ code and the error given below.
#include "Judy.h"
#include <iostream>
using namespace std;
int main()
{
int Rc_int; // return code - integer
Word_t Rc_word; // return code - unsigned word
Word_t Index = 12, Index1 = 34, Index2 = 55, Nth;
Word_t PValue; // pointer to return value
//Pvoid_t PJLArray = NULL; // initialize JudyL array
Pvoid_t JudyArray = NULL;
char String[100];
PWord_t _PValue;
JSLI( JudyArray, _PValue, (uint8_t *) String);
return(0);
} // main()
This gives me the error
m.cpp: In function ‘int main()’:
m.cpp:19: error: invalid conversion from ‘long unsigned int**’ to ‘void**’
m.cpp:19: error: initializing argument 1 of ‘void** JudySLIns(void**, const uint8_t*, J_UDY_ERROR_STRUCT*)’
Please anyone help me to figure out what is the error what i'm doing..
Thanks
According to the documentation, you have the _PValue and JudyArray parameters reversed. Make your call look like this:
JSLI( _PValue, JudyArray, (uint8_t *) String);
Also, try not compiling it as C++ code. So far, your test uses no C++ features. I bet it will compile as C code. It looks like JudyArray relies on the fact that C will do certain kinds of implicit conversions between void * and other pointer types.
If this is the case, I'm not sure what to do about it. The error messages you're getting tell me that JSLI is a macro. In order to fix the error message you have in the comments on this answer, you'd have to reach inside the macro and add a typecast.
These kinds of implicit conversions are allowed in C because otherwise using malloc would always require ugly casts. C++ purposely disallows them because the semantics of new make the requirement that the result of malloc be cast to the correct type unimportant.
I don't think this library can be used effectively in C++ for this reason.
It seems that, you pass JudySLIns(void**, const uint8_t*, J_UDY_ERROR_STRUCT*) a wrong parameter, the first one, you'b better check it!
For integer keys there is a C++ wrapper at http://judyhash.sourceforge.net/