Error: expected initializer before 'bool' - boolean

I have this header file and GNU GGC compiler gives me error in row with declaration of bool function. I cannot find anything wrong.
#ifndef GAMECREATOR_H
#define GAMECREATOR_H
bool isPerfectSquare (int x);
#endif // GAME_CREATOR_H

Related

friend declaration invokes "C++ requires a type specifier for all declarations"

When I only include my "Int.h", friend declaration invokes "C++ requires a type specifier for all declarations". And it runs well when I just write forward declaration "class Int;" and include "Int.h" in cpp file.
#ifndef _MY_ARRAY_H_
#define _MY_ARRAY_H_
// class Int;
#include "Int.h"
class MyArray {
friend Int; //error: C++ requires a type specifier for all declarations
public:
MyArray(int dim, int *size);
Int operator[](int n);
private:
int mDim;
int *mSize;
int *mData;
};
#endif
In my opinion, it doesn't need "forward declaration", because there is a "Int" class declaration in the "Int.h" file. But it does. Why?

Issues running mex commands on Octave

I am using Ubuntu and am trying to use Octave to run a Matlab script that runs the mex compiler on some cpp files https://github.com/yuxng/MDP_Tracking/blob/master/compile.m I have the OpenCV requirement already installed but am receiving some errors.
Basically the commands in octave are
compile
MOT_test
The errors I got from the compile command are shown below. Based on my web search plus this thread it seems like this issue of compiling the MDP Tracker remains unsolved. I have minimal experience running mex and octave so there is not much more I can do.
octave:4> compile
In file included from imResampleMex.cpp:7:0:
wrappers.hpp:22:24: error: ‘wrCalloc’ declared as an ‘inline’ variable
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
^~~~~~
wrappers.hpp:22:24: error: ‘size_t’ was not declared in this scope
wrappers.hpp:22:36: error: ‘size_t’ was not declared in this scope
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
^~~~~~
wrappers.hpp:22:48: error: expression list treated as compound expression in initializer [-fpermissive]
inline void* wrCalloc( size_t num, size_t size ) { return calloc(num,size); }
^
wrappers.hpp:23:24: error: ‘wrMalloc’ declared as an ‘inline’ variable
inline void* wrMalloc( size_t size ) { return malloc(size); }
^~~~~~
wrappers.hpp:23:24: error: ‘size_t’ was not declared in this scope
wrappers.hpp: In function ‘void wrFree(void*)’:
wrappers.hpp:24:44: error: ‘free’ was not declared in this scope
inline void wrFree( void * ptr ) { free(ptr); }
^
wrappers.hpp: At global scope:
wrappers.hpp:29:17: error: ‘size_t’ was not declared in this scope
void* alMalloc( size_t size, int alignment ) {
^~~~~~
wrappers.hpp:29:30: error: expected primary-expression before ‘int’
void* alMalloc( size_t size, int alignment ) {
^~~
wrappers.hpp:29:44: error: expression list treated as compound expression in initializer [-fpermissive]
void* alMalloc( size_t size, int alignment ) {
^
imResampleMex.cpp: In function ‘void resampleCoef(int, int, int&, int*&, int*&, T*&, int*, int)’:
imResampleMex.cpp:22:39: error: ‘alMalloc’ cannot be used as a function
wts = (T*)alMalloc(nMax*sizeof(T),16);
^
imResampleMex.cpp:23:43: error: ‘alMalloc’ cannot be used as a function
yas = (int*)alMalloc(nMax*sizeof(int),16);
^
imResampleMex.cpp:24:43: error: ‘alMalloc’ cannot be used as a function
ybs = (int*)alMalloc(nMax*sizeof(int),16);
^
imResampleMex.cpp: In function ‘void resample(T*, T*, int, int, int, int, int, T)’:
imResampleMex.cpp:49:43: error: ‘alMalloc’ cannot be used as a function
T *C = (T*) alMalloc((ha+4)*sizeof(T),16); for(y=ha; y<ha+4; y++) C[y]=0;
^
warning: mkoctfile exited with failure status
warning: called from
mkoctfile at line 171 column 5
mex at line 29 column 18
compile at line 17 column 2
Compilation finished.
octave:5>

Freeglut doesn't initialize when using it from Swift

I've tried to use the Freeglut library in a Swift 4 Project. When the
void glutInit(int *argcp, char **argv);
function is shifted to Swift, its declaration is
func glutInit(_ pargc: UnsafeMutablePointer<Int32>!, _ argv: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!)
Since I don't need the real arguments from the command line I want to make up the two arguments. I tried to define **argv in the Bridging-Header.h file
#include <OpenGL/gl.h>
#include <GL/glut.h>
char ** argv[1] = {"t"};
and use them in main.swift
func main() {
var argcp: Int32 = 1
glutInit(&argcp, argv!) // EXC_BAD_ACCESS
glutInitDisplayMode(UInt32(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH));
glutCreateWindow("my project")
glutDisplayFunc(display)
initOpenGL()
glutMainLoop()
but with that I get Thread 1: EXC_BAD_ACCESS (code=1, address=0x74) at the line with glutInit().
How can I initialize glut properly? How can I get an UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>! so that it works?
The reason the right code in C char * argv[1] = {"t"}; does not work is because Swift imports fixed size C-array as a tuple, not a pointer to the first element.
But your char ** argv[1] = {"t"}; is completely wrong. Each Element of argv needs to be char **, but you assign char * ("t"). Xcode must have shown you a warning at first build:
warning: incompatible pointer types initializing 'char **' with an expression of type 'char [2]'
You should better take incompatible pointer types warning as error, unless you know what you are doing completely.
Generally, you should better not write some codes generating actual code/data like char * argv[1] = {"t"}; in a header file.
You can try it with Swift code.
As you know, when you want to pass a pointer to single element T, you declare a var of type T and pass &varName to the function you call.
As argcp in your code.
As well, when you want to pass a pointer to multiple element T, you declare a var of type [T] (Array<T>) and pass &arrName to the function you call.
(Ignoring immutable case to simplify.)
The parameter argv matches this case, where T == UnsafeMutablePointer<Int8>?.
So declare a var of type [UnsafeMutablePointer<Int8>?].
func main() {
var argc: Int32 = 1
var argv: [UnsafeMutablePointer<Int8>?] = [
strdup("t")
]
defer { argv.forEach{free($0)} }
glutInit(&argc, &argv)
//...
}
But I wonder if you really want to pass something to glutInit().
You can try something like this:
func main() {
var argc: Int32 = 0 //<- 0
glutInit(&argc, nil)
//...
}
I'm not sure if freeglut accept this, but you can find some articles on the web saying that this works in some implementation of Glut.

Assigning UnsafeMutablePointer value to UnsafePointer without unsafeBitCast

I am working with a C API that defines a structure with const char* and a function that returns a char* and am trying to find the best way to do the assignment.
Is there a way to do this without using unsafeBitCast? If I don't put the cast then I get this error:
Cannot assign value of type 'UnsafeMutablePointer<pchar>'
(aka 'UnsafeMutablePointer<UInt8>')
to type 'UnsafePointer<pchar>!'
(aka 'ImplicitlyUnwrappedOptional<UnsafePointer<UInt8>>')
Also, will the initialization of pairPtr below using pair() allocate a pair struct on the stack to initialize the allocated pair on the heap because this seems inefficient for the case where the structure just has to be zero'd out.
Here is sample code:
C library header (minimized to demonstrate the problem):
#ifndef __PAIR_INCLUDE__
#define __PAIR_INCLUDE__
typedef unsigned char pchar;
pchar*
pstrdup(const pchar* str);
typedef struct _pair {
const pchar* left;
const pchar* right;
} pair;
#endif // __PAIR_INCLUDE__
My Swift code:
import pair
let leftVal = pstrdup("left")
let rightVal = pstrdup("right")
let pairPtr = UnsafeMutablePointer<pair>.allocate(capacity: 1)
pairPtr.initialize(to: pair())
// Seems like there should be a better way to handle this:
pairPtr.pointee.left = unsafeBitCast(leftVal, to: UnsafePointer<pchar>.self)
pairPtr.pointee.right = unsafeBitCast(rightVal, to: UnsafePointer<pchar>.self)
The C code:
#include "pair.h"
#include <string.h>
pchar*
pstrdup(const pchar* str) {
return strdup(str);
}
The module definition:
module pair [extern_c] {
header "pair.h"
export *
}
You can create an UnsafePointer<T> from an UnsafeMutablePtr<T>
simply with
let ptr = UnsafePointer(mptr)
using the
/// Creates an immutable typed pointer referencing the same memory as the
/// given mutable pointer.
///
/// - Parameter other: The pointer to convert.
public init(_ other: UnsafeMutablePointer<Pointee>)
initializer of UnsafePointer. In your case that would for example be
p.left = UnsafePointer(leftVal)

What does error conflicting types for '' mean?

i got an error that said "error: conflicting types for '____'. What does that mean?
Quickfix:
Make sure that your functions are declared once and only once before they are called. For example, change:
main(){ myfun(3.4); }
double myfun(double x){ return x; }
To:
double myfun(double x){ return x; }
main(){ myfun(3.4); }
Or add a separate function declaration:
double myfun(double x);
main(){ myfun(3.4); }
double myfun(double x){ return x; }
Possible causes for the error
Function was called before being declared
Function defined overrides a function declared in an included header.
Function was defined twice in the same file
Declaration and definition don't match
Declaration conflict in the included headers
What's really going on
error: conflicting types for ‘foo’ means that a function was defined more than once with different type signatures.
A file that includes two functions with the same name but different return types would throw this error, for example:
int foo(){return 1;}
double foo(){return 1.0;}
Indeed, when compiled with GCC we get the following errors:
foo.c:5:8: error: conflicting types for ‘foo’
double foo(){return 1.0;}
^
foo.c:4:5: note: previous definition of ‘foo’ was here
int foo(){return 1;}
^
Now, if instead we had a file with two function definitions with the same name
double foo(){return 1;}
double foo(){return 1.0;}
We would get a 'redefinition' error instead:
foo.c:5:8: error: redefinition of ‘foo’
double foo(){return 1.0;}
^
foo.c:4:8: note: previous definition of ‘foo’ was here
double foo(){return 1;}
^
Implicit function declaration
So why does the following code throw error: conflicting types for ‘foo’?
main(){ foo(); }
double foo(){ return 1.0; }
The reason is implicit function declaration.
When the compiler first encounters foo() in the main function, it will assume a type signature for the function foo of int foo(). By default, implicit functions are assumed to return integers, and the input argument types are derived from what you're passing into the function (in this case, nothing).
Obviously, the compiler is wrong to make this assumption, but the specs for the C (and thus Objective-C) language are old, cranky, and not very clever. Maybe implicitly declaring functions saved some development time by reducing compiler complexity back in the day, but now we're stuck with a terrible feature that should have never made it into the language. In fact, implicit declarations were made illegal in C99.
That said, once you know what's going on, it should be easy to dig out the root cause of your problem.
it's probably because your function "_" already exists in your library. It happened to me with this function:
I was using stdio.h
int getline (char s[ ] , int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
When I changed "getline" to "getlinexxx" and gcc compiled it:
int getlinexxx (char s[], int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
And the problem was gone
What datatype is '___'?
My guess is that you're trying to initialize a variable of a type that can't accept the initial value. Like saying int i = "hello";
If you're trying to assign it from a call that returns an NSMutableDictionary, that's probably your trouble. Posting the line of code would definitely help diagnose warnings and errors in it.