Error "Expected specifier-qualifier-list before" at struct constructor - iphone

I am trying to write some code to optimize some Open GL functions for a program I'm writing, unfortunately, I am not exactly a C or C++ veteran, but that's partially why I'm doing this project!
So I'm creating a struct to handle 3x3 matrices and I am defining the struct as follows:
#ifndef MATRIX3BY3_H
#define MATRIX3BY3_H
struct Matrix3by3
{
float ix, jx, kx;
float iy, jy, ky;
float iz, jz, kz;
Matrix3by3() {}
Matrix3by3(const Matrix3by3 &matrix)
{
ix = matrix.ix;
jx = matrix.jx;
kx = matrix.kx;
iy = matrix.iy;
jy = matrix.jy;
ky = matrix.ky;
iz = matrix.iz;
jz = matrix.jz;
kz = matrix.kz;
}
Matrix3by3 (const float _ix, const float _jx, const float _kx,
const float _iy, const float _jy, const float _ky,
const float _iz, const float _jz, const float _kz) :
ix(_ix), jx(_jx), kx(_kx),
iy(_iy), jy(_jy), ky(_ky),
iy(_iz), jx(_jz), kz(_kz) {}
};
#endif
And I get the error (twice)
Expected specifier-qualifier-list
before 'Matrix3by3'
On the line of the first constructor. I have tried to look around for answers for this, and it seems that it has to do with the compiler not knowing that this is a type. So I have tried the following, I'll remove the innards for brevity:
typedef struct Matrix3by3 { ... };
struct Matrix3by3 { struct Matrix3by3() {} ... };
struct Matrix3by3 { ... } Matrix3by3;
typdef struct Matrix3by3;
struct Matrix3by3 { ... };
Which are all solutions that were suggested on blogs and articles that I saw for this error. I also saw that it may arise because of a circular dependency, but this file has no includes that include anything else, and I've even removed them just to be certain from time to time - no change.
I could write this in a objective-c class, I'm sure, but it will probably take a tiny bit more memory and cycles, and that's exactly what I'm trying to avoid. The only thing I can think of left is some compiler/project setting that I have set by default that precludes my using this type of structure. Entirely possible, as I'm learning the language/environment.
Can any one provide some help?
Thanks!

C does not support constructors or member functions of structs. There is no way you will get this to compile as C or Objective-C. You need to compile this as C++ or Objective-C++, at which point it will almost compile: you have an error in your 3rd constructor, in that you're attempting to initialize the members iy and jx multiple times. Once you fix those typos, it compiles just fine.

typedef struct { ... } Matrix3by3;
should work. It declares the anonymous struct as a type.
And use class instead of struct :)

What language/compiler are you translating your program with? I'd guess that you are trying to compile the code as C, while the language features you are trying to use are strictly C++-specific.

The error "Expected specifier-qualifier-list before 'Matrix3by3'" is a GCC-ism and it means that the token "Matrix3by3" is unknown. This is typically the case when you have a type that the compiler doesn't recognize, either because you mistyped it or because you forgot a header. In your case, it's because the type "Matrix3by3" really doesn't exist. You have two options:
Stop using Matrix3by3 directly and start using struct Matrix3by3 instead, as that's the actual type you defined.
Give your struct a typedef. It will look something like
typedef struct {
// fields here
} Matrix3by3

Related

UStruct: type must be a UCLASS, USTRUCT or UENUM

I’m probably missing something simple but I can’t find it. The struct is marked as USTRUCT() but still it complains about it not being a USTRUCT. It’s not caused by the TArray because removing that still gives the same error. Also I can use the struct FCrusherTrigger in blueprints without a problem at all. But c++ gives me an error.
USTRUCT(BlueprintType)
struct FCrusherTrigger
{
GENERATED_USTRUCT_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CrusherTrigger")
ECollisionShapeEnum CollisionType;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "CrusherTrigger")
FTransform RelativeTransform;
FCrusherTrigger()
{
CollisionType = ECollisionShapeEnum::CollisionShape_Box;
}
};
...
// BaseCrusher.h
public:
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "BaseCrusher")
TArray<FCrusherTrigger> TriggersSetup; // <<<<<<<<<<<<< error here ```
Try to place your constructor above your properties, and call it public. My guess, the issue is around your Construct.

How to add write_raw function in my iio_info structure

I am writing a driver using iio framework. So far it goes well. All the inputs and sysfs entries are working perfectly and the values measured are fine. (It is very well documented and it is easy). But I need a small extension to be able to write on one of the channels. When I add my function in iio_info the compiler issues me an error:
drivers/iio/adc/iio-ccuss.c:197:15: error: initialization from incompatible pointer type [-Werror=incompatible-pointer-types]
.write_raw = ccuss_iio_write_raw,
^~~~~~~~~~~~~~~~~~~
It is very weird for me. I even can't believe I am asking shamelessly this here but I am very frustrated. I lost almost half day with that.
My structure is:
static const struct iio_info ccuss_iio_info = {
.driver_module = THIS_MODULE,
.attrs = &ccuss_iio_attribute_group,
.read_raw = ccuss_iio_read_raw,
.write_raw = ccuss_iio_write_raw,
};
my channel types are IIO_VOLTAGE, IIO_TEMP and IIO_HUMIDITYRELATIVE.
I am start thinking to make it as an device attribute :-( if I do not receive an answer in the next 12 hours.
Update:
just to be more visible, according Murphy's comment.
static int ccuss_iio_write_raw(struct iio_dev *iio,
struct iio_chan_spec const *channel, int *val1,
int *val2, long mask);
P.S. I do not want to remove this error by the most known way. The QA (and me) will be unhappy.
Thanks
According to the reference the write_raw() function is declared as follows:
int (*write_raw)(
struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val,
int val2,
long mask);
Your implementation is declared like this:
static int ccuss_iio_write_raw(
struct iio_dev *iio,
struct iio_chan_spec const *channel,
int *val1,
int *val2,
long mask);
So you declare the two integer parameters as pointers, but they are expected to be passed by value. I think that's the mismatch that causes the "incompatible pointer type" error.

How to use macros to replace/inject function calls in OpenCL

I am developing an algorithm using PyOpenCL. To avoid code duplication I am trying to use templating along with C macros to replace function calls, since OpenCL 1.2 does not support function pointers.
I currently have the following macro section in my OpenCL kernel code:
#define LINEAR_FIT_SEARCH_METHOD ${linear_fit_search_method}
#if LINEAR_FIT_SEARCH_METHOD == MIN_MAX_INTENSITY_SEARCH
#define LINEAR_FIT_SEARCH_METHOD_CALL() determineFitUsingMinMaxIntensitySearch(lineIntensities,imgSizeY,linFitParameter,linFitSearchRangeXvalues)
#elif LINEAR_FIT_SEARCH_METHOD == MAX_INCLINE_SEARCH
#define LINEAR_FIT_SEARCH_METHOD_CALL() determineFitUsingInclineSearch(lineIntensities,imgSizeY,linFitParameter,linFitSearchRangeXvalues,inclineRefinementRange)
#endif
In the kernel code I also define the corresponding functions determineFitUsingMinMaxIntensitySearch and determineFitUsingInclineSearch. I am now attempting to use the macro to exchange the function call like this:
__private struct linearFitResultStruct fitResult = LINEAR_FIT_SEARCH_METHOD_CALL();
so that I select the desired call (note: I always only need either one or the other and configuration is done before the program runs (no need for dynamically switching the two)).
Using PyOpenCL templating I now do something like this:
def applyTemplating(self):
tpl = Template(self.kernelString)
if self.positioningMethod == "maximumIntensityIncline":
linear_fit_search_method="MAX_INCLINE_SEARCH"
if self.positioningMethod == "meanIntensityIntercept":
linear_fit_search_method="MIN_MAX_INTENSITY_SEARCH"
rendered_tpl = tpl.render(linear_fit_search_method=linear_fit_search_method)
self.kernelString=str(rendered_tpl)
Where self.kernelString contains the macro above along with the code.
Unfortunately I am getting this error, which I do not understand:
1:455:53: error: implicit declaration of function 'determineFitUsingInclineSearch' is invalid in OpenCL
1:9:41: note: expanded from macro 'LINEAR_FIT_SEARCH_METHOD_CALL'
1:455:41: error: initializing 'struct linearFitResultStruct' with an expression of incompatible type 'int'
1:536:30: error: conflicting types for 'determineFitUsingInclineSearch'
1:455:53: note: previous implicit declaration is here
1:9:41: note: expanded from macro 'LINEAR_FIT_SEARCH_METHOD_CALL'
1:616:41: error: initializing 'struct linearFitResultStruct' with an expression of incompatible type 'int'
I have very little experience with macros so:
Is what I am attempting even possible in this way or do I need to go a different route?
UPDATE 1:
This code runs fine when I set self.positioningMethod = "meanIntensityIntercept" in my unit test, but fails when setting self.positioningMethod = "maximumIntensityIncline" with the error message above. I cannot spot the error at the yet.
UPDATE 2:
I was also inspired by this post, if that helps:
how to compare string in C conditional preprocessor-directives
As you say you have very little experience with macros then I would go for something simple. determineFitUsingMinMaxIntensitySearch and determineFitUsingInclineSearch accept different number of arguments, so this could done this way:
kernel_code = """
#ifdef USE_FUNCTION_A
void function_a(
int x,
int y,
int extra_param,
__global const int* restrict in,
__global int* restrict out
)
{
//...
}
#else
void function_b(
int x,
int y,
__global const int* restrict in,
__global int* restrict out
)
{
//...
}
#endif
__kernel void my_kernel(
int x,
int y,
__global const int* restrict in,
__global int* restrict out
)
{
// ...
#ifdef USE_FUNCTION_A
function_a(x,y,5,in,out);
#else
function_b(x,y,in,out);
#endif
// ...
}
"""
if use_function_a:
prg = cl.Program(ctx, kernel_code).build("-DUSE_FUNCTION_A")
else:
prg = cl.Program(ctx, kernel_code).build("")

cannot sort a vector of objects that has array in it

Hi I am using a class that has an integer and array and creating a vector of the classes objects but I cannot sort it also don't know to store in it.
I am a BEGINNER on c++ so i just wanted to know if I am wrong and how to
do that thing
here n = no of times the program has to execute
num = to store the no. of elements in vector a
but problem loop for(j=0;j<arr[i].a.end();j++)
and also pushback is not working
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class subcont
{
public:
int num;
vector<int> a;
};
int main()
{
vector<subcont> arr(100);
int i,j,k,l,n,num1,num2;
cin>>n;
for(i=0;i<n;i++)
{
cin>>arr[i].num;
for(j=0;j<arr[i].num;j++)
{
cin>>num2;
cin>>arr[i].a.pushback(num2);
}
}
for(i=0;i<n;i++)
{
sort(arr[i].a.begin(),arr[i].a.end());
}
for(i=0;i<n;i++)
{
cout<<arr[i].num;
for(j=0;j<arr[i].a.end();j++)
cout<<arr[i].a[j];
}
return 0;
}
The problems you describe sound as if you could at least compile your code, which I can't. In fact, the compiler error messages (if one first ignores the large amount of error noise generated by unhappy templates) should hint to most important problems.
Logical problems on first sight: In for(j=0;j<k;j++) the value of k is undefined. In for(j=0;j<arr[i].a.end();a++) the a++ does not make sense.
In cin>>arr[i].a.pushback[num]; the num should probably be num2. Please check your code for more such typoes.
Your sort fails because a is a C array and not a C++ container, so a.begin() and a.end() are not defined.
Stylistic problem: While it makes life a lot easier, mayn people strictly recommend to not use using namespace std;
Additional remark: Why not use std::vector<int> in place of subcont?

What's the difference between typedef const struct *CGPath and typedef struct *CGPath?

I've found this in the CGPath.h header file. I'm curious what this const thing does?
typedef struct CGPath *CGMutablePathRef;
typedef const struct CGPath *CGPathRef;
My guess: If I typedef something as const, it's constant so immutable and can't be changed in any way. Does that make sense?
Yes the const means you cannot change the path externally.
For CoreFoundation-based libraries though, the const is more a hack to allow mutable objects to use immutable methods without casting, and not vice-versa. That means
CGMutablePathRef mpath;
...
CGPathContainsPoint(mpath, ...);
compiles fine because a Foo* can be implicitly converted to a const Foo*, but
CGPathRef path;
...
CGPathAddRect(path, ...);
will throw an error because a const Foo* cannot be safely converted to a Foo*.
const T* means that the type is a pointer and that the pointee cannot be changed via that pointer. (That doesn't necessarily mean that the pointee can never change; it might be modified by a different, non-const reference to it, for example.)
"In any way" might be an overstatement, but you're correct. You'll notice that one is called CGPathRef and the other is CGMutablePathRef.
const is from standard C. It means that the dereferenced pointer cannot be assigned to. So I cannot do:
GCPathRef p = &my_path;
*p = new_path; // generates compiler error
But note that p itself can be changed:
p = &another_path; // this is OK.
It is not a hack at all as another poster has suggested. It has been part of C for generations.