first time asking.
Here's the deal:
I have a helper class (at least now it's a class) that has several math functions, that I use throughout the project.
#ifndef CUSTOM_UTILS_H
#define CUSTOM_UTILS_H
//---------------------------------------------------------
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
class cUtil {
public:
static int utilsRandom(int from, int to);
static double utilsRandom(double from, double to);
static double giveAngle(double x, double y);
static double FoV(double cx, double cy,
double fx, double fy,
double tx, double ty,
double radius);
};
//---------------------------------------------------------
#endif
Implementation:
#define _USE_MATH_DEFINES
#include "customUtils.h"
//---------------------------------------------------------
int cUtil::utilsRandom(int from, int to) {
if (from == to) {
return from;
}
return (rand() % (to - from)) + from;
}
//---------------------------------------------------------
double cUtil::utilsRandom(double from, double to) {
...
}
//---------------------------------------------------------
double cUtil::giveAngle(double x, double y) {
...
}
//---------------------------------------------------------
double cUtil::FoV(double cx, double cy,
double fx, double fy,
double tx, double ty,
double radius) {
...
}
//---------------------------------------------------------
(removed the 3 bodies to save space for the post)
Now, when I use it , let's say, in a class called 'creature' I include the customUtils.h file in the header of 'creature'. And use any of the 4 functions like so: cUtil::func_name().
Sometimes I get an unresolved external error such as
LNK2001: unresolved external symbol "public: static double __cdecl cUtil::utilsRandom
(double,double)" (?utilsRandom#cUtil##SANNN#Z) C:\Users\Rockstrongo\Documents\Projects
\nnEvo\nnEvo\net.obj
It appears for all functions in cUtil and for all classes that use those functions.
I said it sometimes appears, because it just does that - I'd be rebuilding the project and it would resurface. To scrub it again I would change some part of cUtils code, or the way it is included in other classes or anything that it would get it running again. For some time, cleaning->compiling the customUtils.cpp->then building the rest worked, but not any more.
To an untrained eye like mine this appears to be completely random and I'm all out off straws to grasp. I'm using Microsoft Visual Studio 2010. It's a console project using openGL and glut.
I see you overloaded cUtil::utilsRandom to use doubles and ints. It's ok except for one thing: You can not change the returned type. Both overloaded version must either return an int or a double. If you must have different returned types, then do not overload the function. Rather use different function names.
Related
I have a c++ function in "example.h":
bool myFunc(double const* const* p);
and I want to wrap it with cython code (in .pyx file).
Howerver, when I'm write the following code:
cdef extern from r"example.h":
bool myFunc(double const*const* p)
I'm receiving the following error:
Error compiling Cython file:
Expected ')', found '*'
and pycharm shows this error on double const* const* p:
Unresolved reference 'const'
How can I declare that kind of variables?
In C/C++, there is ongoing battle where to put the const-qualifier: either
void foo(const int *a);
or
void foo(int const *a);
both meaning the same thing.
There is no such battle in Cython, because it accept only the first version.
The above rule, applied to double** leads to:
cdef extern from r"example.h":
bool myFunc(const double * const* p)
Or as a work-around one could drop the const-qualifier altogether:
cdef extern from r"example.h":
bool myFunc(const double **p)
which I would not recommend, all above in large projects, where using const-qualifiers helps a lot when figuring out what happens.
I'm a noob. Using C++ in Clion
I'm building a graph of N random nodes on a Cartesian plane
I have a simple type, node (just a point) (int x, int y)
node pt(x,y)
I have a vector of N randomly generated unique points (would this be considered ordered points btw?)
vector NodeList(N);
I have a class Graph (Incomplete) which has a function GenNodelist which I have tested as a standalone program. I had a hell of a time just getting the constructor to build without compile error.
#include <iostream>
#include <vector>
#ifndef DIJKSTRA_GRAPH_H
#define DIJKSTRA_GRAPH_H
using namespace std;
class Graph {
private:
int x;
int y;
vector<int> NodeList;
int *np;
public:
//constructor
x(x),y(y),NodeList(),np(){}
void GenNodeList(vector<int> NL(), int &np) {x,y,NodeList, &np; }
void GenNodeList(vector<int> *NL, int *p);
};
#endif //DIJKSTRA_GRAPH_H
void Graph::GenNodeList(vector<int>* NL, int* p) {
.
.
} .
... code that builds and has been quasi tested
So everything builds and there's a "hello world" main program in the project. The 2 classes, (node & Graph: 2 headers and 2 cpp files) along with the main "hello world" build and run. Now from main() I wan to call the call the GenNode function from main. I just want to pass a pointer and have the list generate and sit in memory UN-mutable. right now. I'll build the graph off of this later. When I try to call the function nothing works. How can I build this list and access it from main() and Graph()?
main(){
vector<int> NL(N);
int *np;
Graph::GenNodeList( NL, np);
}
Can't seem to figure this out.
This incomplete piece of code
Graph::GenNodeList( NL, np);
is ill-formed because that method is not static. You have to access instance of class Graph for non-static members. Nothing about oop here, just language's rules.
Graph(): x(x),y(y),NodeList(),np(){}
static void GenNodeList(vector<node>* NL, node* np);
void Graph::GenNodeList(vector<node>* NL, node* p) {
int main() {
vector<node> NL(N);
vector<node>* NList;
node *np = nullptr;
NList = &NL;
Graph::GenNodeList(NList,np);
return 0;
https://github.com/Pasqualino31/Dijkstra/tree/Pasqualino31-patch-2
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("")
I would like to have a class member function behave like a function pointer. I need this behavior to integrate my own classes into some existing code.
It seems that this may be possible using Boost::function and Boost::bind, but I can't seem to get it working. The following code is a minimal example that I am using to test my implementation. The last line in the main() program is what I would like to be able to do.
Any help is greatly appreciated. I am using g++ and Boost 1.46.
// Includes
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <stdio.h>
using namespace std;
// Define a pure virtual base class
class Base{
public:
virtual double value(double v, double t) = 0;
};
// Define a derived class
class Derived : public Base{
public:
double value(double v, double t){
return v*t;
}
};
// Main program
int main(){
// A derived class
boost::shared_ptr<Derived> p(new Derived);
// Use class directly
printf("value = %f\n", p->value(100, 1));
// Create a boost::function
boost::function< double (Derived*, double, double) > f;
f = &Derived::value;
printf("f(&A, 100, 2) = %f\n", f(p.get(), 100, 2));
// Use boost::bind
printf("bind f(100,3) = %f\n", boost::bind(&Derived::value, p, _1, _2)(100,3));
// Make a boost::function to the binded operation???
boost::function< double (double, double) > f2;
f2 = boost::bind(&Derived::value, p.get()); // This is wrong
printf("f2(100,4) = %f\n", f2(100,4)); // I want to be able to do this!
}
Based on the documentation (See section "Using bind with pointers to members"), you need to specify that the function has two parameters:
f2=bind(&Derived::value, p.get(), _1, _2);
f2(100, 4); // p.get()->value(100, 4)
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