i have a objective-c program, i added a little lib with linked list in plain c.
this is the header:
typedef struct { int v; int w; } Edge;
Edge EDGE(int, int);
typedef enum tagTipoNodo {
k_casellaPolozia,
k_casellaKiller
} tipoNodo;
typedef struct node *link;
struct node { int v; link next; };
typedef struct node2v *link2v;
struct node2v { int v; int val; link2v next; };
link2v NEW2v(int v, int val, link2v next);
void DEL2v(int v, link2v *lista);
void Update2v(int v, int val, link2v lista);
link sort2x(link2v lista);
link sort2xDecr(link2v lista);
int maxInList(link2v lista);
struct graph { int V; int E; link *adj; int *tipo;};
typedef struct graph *Graph;
Graph GRAPHinit(int);
void GRAPHinsertE(Graph, Edge);
void GRAPHremoveE(Graph, Edge);
int GRAPHedges(Edge [], Graph G);
Graph GRAPHcopy(Graph);
void GRAPHdestroy(Graph);
void GRAPHShow(Graph G);
void ingr();
link nodeInDistance(Graph G, int A, int distance);
link nodeInDistanceOfType(Graph G, int A, int distance, tipoNodo tipo);
int distance(Graph G, int A, int B,tipoNodo tipo);
int shortestPath(Graph G, int A, int B, tipoNodo tipo, int *percorso);
link shortestPathList(Graph G, int A, int B, tipoNodo tipo);
int distanceForAllocation(Graph G, int A, int B, tipoNodo tipo);
void setPesoForNode(int n,int poliziotto);
void resetPesoForNode(int poliziotto);
void resetPesoForAllNodes();
void initCasPoliz();
link NEW(int v, link next);
void DEL(int v, link *lista);
link copyList(link l);
int lengthList(link l);
int lengthListOfType(Graph G,link l,tipoNodo tipo);
void deleteList(link *lista);
int isPresentInList(link lista,int val);
void printCaselleVietate();
in simulator all compiles well, but when i try to compile for device
this error occurs
link redeclared as different kind of symbol in...[...]
how can i fix this?
thanks
i have found that i have an #include "linkedList.h" in
helloworld.h and helloworld.m...
in simulator all went well...strange thing!
however another strange thing...:
in "linkedList.h" i have link and link2k defined in a similar way...but link2k does not gave me that error...mha!
use #import instead of #include. It's main function is to remove the need to use header guards
Related
I need to solve a OOP problem in which I have to manage multiple classes inherited by each other. First I need to read all the data for all the Employees of a Company. The reading runs very well but I also need to display the read data after reading the command 1 (I need to use switch). I created a function "afisare_angajati()" which only works outside "if" and "switch" statements. I don't know why those statements disable my function. This happened to me before but I couldn't find the cause. Is something that I am not seeing? You can see my function at the end of the code. Thx for help.
#include<iostream>
#include<string>
#include<vector>
class Angajat{
protected:
std::string nume;
float salariu_baza;
std::string functie;
float procent_taxe_salariu;
public:
float get_salariu_net(){return 0;}
float get_salariu_brut(){return 0;}
std::string get_nume(){return 0;}
void marire_salariu(){}
Angajat(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
nume(nume),salariu_baza(salariu_baza),functie(functie),procent_taxe_salariu(procent_taxe_salariu){}
void display(){
std::cout<<nume<<'\n';
std::cout<<functie<<'\n';
}
};
class Analist:public Angajat{
public:
Analist(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
Angajat(nume,salariu_baza,functie,procent_taxe_salariu){}
};
class Programator:public Analist{
protected:
float procent_deducere_salariu_it;
public:
Programator(std::string nume,float salariu_baza,std::string functie,float procent_taxe_salariu=40):
Analist(nume,salariu_baza,functie,procent_taxe_salariu){}
};
class LiderEchipaProgramare:public Programator{
protected:
int vechime;
float bonus_vechime;
public:
LiderEchipaProgramare(std::string nume,float salariu_baza,std::string functie,int vechime,float procent_taxe_salariu=40):
Programator(nume,salariu_baza,functie,procent_taxe_salariu),vechime(vechime){
bonus_vechime=500;
}
};
class FirmaProgramare{
private:
std::vector<Angajat*> vec_ang;
public:
void afisare_angajati(){
for(Angajat* a:vec_ang){
a->display();
}
}
void mareste_salarii(float,float,float){}
void promoveaza(std::string){}
void adauga_angajat(Angajat* a){
vec_ang.push_back(a);
}
};
int main(){
std::string nume;
std::string functie;
float salariu_baza;
int vechime;
int nr_ang,comanda;
FirmaProgramare pula;
std::cin>>nr_ang;
for(int i=0;i<nr_ang;++i){
std::cin.ignore();
std::getline(std::cin,nume);
std::cin>>functie;
std::cin>>salariu_baza;
Angajat* p = nullptr;
if(functie=="Analist"){
p = new Analist(nume,salariu_baza,functie);
}
else{
if(functie=="Programator"){
p = new Programator(nume,salariu_baza,functie);
}
else{
p = new LiderEchipaProgramare(nume,salariu_baza,functie,vechime);
}
}
pula.adauga_angajat(p);
}
std::cin>>comanda;
//pula.afisare_angajati(); output is correct if I put the function outside of brackets
switch(comanda)
{
case 1:{
pula.afisare_angajati();
break;
}
}
}
I'm looking for an annotation something like
-(SomeStruct *) structFromInternals __attribute__((returns_malloced_ptr))
{
SomeStruct *ret = malloc(sizeof(SomeStruct));
//do stuff
return ret;
}
to soothe the clang static analyzer beasts.
The only viable attributes link I can find is for GCC, but it doesn't even include ns_returns_retained, which is in an extension, I assume.
EDIT:
as to why this is needed, I have a scenario that I can't repro in a simple case, so it may have to do with a c lib in an Objective-C project... The gist is, I get a static analyzer warning that the malloc in createStruct is leaked:
typedef struct{
void * data;
size_t len;
}MyStruct;
void destroyStruct(MyStruct * s)
{
if (s && s->data) {
free(s->data);
}
if (s) {
free(s);
}
}
MyStruct * createStructNoCopy(size_t len, void * data)
{
MyStruct * retStruct = malloc(sizeof(MyStruct));
retStruct->len = len;
retStruct->data = data;
return retStruct;
}
MyStruct * createStruct(size_t len, void * data)
{
char * tmpData = malloc(len);
memcpy(tmpData, data, len);
return createStructNoCopy(len, tmpData);
}
MyStruct * copyStruct(MyStruct * s)
{
return createStruct(s->len, s->data);
}
The function annotation ownership_returns(malloc) will tell the Clang static analyser that the function returns a pointer that should be passed to free() at some point (or a function with ownership_takes(malloc, ...)). For example:
void __attribute((ownership_returns(malloc))) *my_malloc(size_t);
void __attribute((ownership_takes(malloc, 1))) my_free(void *);
...
void af1() {
int *p = my_malloc(1);
return; // expected-warning{{Potential leak of memory pointed to by}}
}
void af2() {
int *p = my_malloc(1);
my_free(p);
return; // no-warning
}
(See the malloc-annotations.c test file for some more examples of their use.)
At the moment, these annotations only take effect when the alpha.unix.MallocWithAnnotations checker is run (which is not run by default). If you're using Xcode, you'll need to add -Xclang -analyzer-checker=alpha.unix.MallocWithAnnotations to your build flags.
What does one do to make something like this work?
void (^)(void) *someBlock = ^{
//some code
};
Dmitry's answer is exactly right. Think of the block syntax as a C function declaration:
// C function -> <return type> <function name> (<arguments>)
void someFunction(void)
{
// do something
}
// block -> <return type> (^<block variable name>) (<arguments>)
void (^someBlock)(void) = ^{
// do something
};
Another example:
// C function
int sum (int a, int b)
{
return a + b;
}
// block
int (^sum)(int, int) = ^(int a, int b) {
return a + b;
};
So just think of the block syntax as a C function declaration:
First the return type int, then the name of the block variable (^sum) and then the list of arguments types (int, int).
If, however, you need a certain type of block frequently in your app, use a typedef:
typedef int (^MySumBlock)(int, int);
Now you can create variables of the MySumBlock type:
MySumBlock debugSumBlock = ^(int a, int b) {
NSLog(#"Adding %i and %i", a, b);
return a + b;
};
MySumBlock normalSumBlock = ^(int a, int b) {
return a + b;
};
Hope that helps :)
Just block syntax
void (^someBlock)(void) = ^{
//some code
};
Code that I am supposed to fill out which was easy enough.
#define MAX_NAME_LEN 128
typedef struct {
char name[MAX_NAME_LEN];
unsigned long sid;
} Student;
/* return the name of student s */
const char* getName(const Student* s) {
return s->name;
}
/* set the name of student s */
void setName(Student* s, const char* name) {
/* fill me in */
}/* return the SID of student s */
unsigned long getStudentID(const Student* s) {
/* fill me in */
}
/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) {
/* fill me in */
}
However it says what is the logical error in the following function?
Student* makeDefault(void) {
Student s;
setName(&s, "John");
setStudentID(&s, 12345678);
return &s;
}
I do not see any problems. I tested it. It works fine.
Is it because this should probably be a void function and does not need to be returning anything?
You can't return a pointer to a locally declared variable (Student s). The variable "s" will disappear (become garbage) after the return.
Instead, you need to allocate a student first.
You should probably do this:
void makeDefault(Student* pS) {
setName( pS, "John");
setStudentID( pS, 12345678);
}
And then let the calling application allocate the student.
I am storing points in a custom container and I would like to build the Delaunay triangulation on a subset of these points.
As the points already exist in the container I don't want the Delaunay triangulation to store copies of these points.
My point class is derived from Point_3 and contains several informations (booleans and ints).
In order to do that, I created a custom triangulation_vertex class :
template < typename GT, typename Pt, typename DSVb = Triangulation_ds_vertex_base_3<> >
class Convection_vertex : public DSVb
{
public:
typedef typename DSVb::Cell_handle Cell_handle;
typedef GT Geom_traits;
typedef typename GT::Point_3 Point;
typedef typename Pt::Point_handle Point_handle;
template < typename TDS2 >
struct Rebind_TDS {
typedef typename DSVb::template Rebind_TDS<TDS2>::Other DSVb2;
typedef Convection_vertex<GT, Pt, DSVb2> Other;
};
private:
static int rank_id;
int number_id;
bool discovered;
Point_handle _ph;
public:
Convection_vertex() : DSVb(), number_id(rank_id++), discovered(false) {}
Convection_vertex(const Point_handle& p) : DSVb(), _ph(p), number_id(rank_id++), discovered(false) {}
Convection_vertex(const Point_handle& p, const Cell_handle& c) : DSVb(c), _ph(p), number_id(rank_id++), discovered(false) {}
Convection_vertex(const Cell_handle& c) : DSVb(c), number_id(rank_id++), discovered(false) {}
const Point& point() const
{ return (*_ph); }
Point& point()
{ return (*_ph); }
void set_point(const Point& p){ }
void set_point(const Point_handle& ph)
{ _ph = ph; }
void set_point_handle(Point_handle ph)
{ _ph = ph; }
const Point_handle& point_handle() const
{ return _ph; }
Point_handle& point_handle()
{ return _ph; }
};
To insert a point in the Delaunay triangulation I do:
DVertex_handle dvh = dt.insert(*p);
dvh->set_point_handle(p);
Where p is a point_handle (ie My_point*).
To delete a point in the Delaunay triangulation I do:
dt.remove(dvh);
where dvh is a vertex_handle.
Inserting points in the triangulation is working fine, but I'm having issues removing points. Is my custom vertex class incorrect ?
Is there a better way to do that ?
--edit-----
dt is the Delaunay triangulation:
typedef CGAL::Convection_vertex<K,Point> Conv_Vb3d;
typedef CGAL::Convection_cell<K> Ce3d;
typedef CGAL::Triangulation_data_structure_3<Conv_Vb3d,Ce3d > Tds3d;
typedef CGAL::Delaunay_triangulation_3<K,Tds3d > Dh;
Dh dt;
--
#sloriot: Is this a good start ?
template < typename CK, bool UseStaticFilters, typename Pt >
struct Convection_traits
: public Filtered_kernel_adaptor<
Type_equality_wrapper<
typename CK:: template Base< Convection_traits<CK, UseStaticFilters,Pt> >::Type,
Convection_traits<CK, UseStaticFilters,Pt> >,
UseStaticFilters >
{
typedef Pt Point_3;
[...] // functors
};
Here is what I have done to use my point_handle as point in the Delaunay_triangulation:
template < typename K_, typename Pt >
class My_traits
{
K_ K;
public:
typedef Pt Point_3;
typedef My_traits<K_, Pt> Self;
//triangulation traits
typedef typename K_::Segment_3 Segment_3;
typedef typename K_::Tetrahedron_3 Tetrahedron_3;
typedef typename K_::Triangle_3 Triangle_3;
typedef typename K_::Construct_segment_3 Construct_segment_3;
typedef typename K_::Construct_triangle_3 Construct_triangle_3;
typedef typename K_::Construct_tetrahedron_3 Construct_tetrahedron_3;
typedef typename K_::Compare_xyz_3 Compare_xyz_3;
typedef typename K_::Coplanar_orientation_3 Coplanar_orientation_3;
typedef typename K_::Orientation_3 Orientation_3;
Construct_tetrahedron_3 construct_tetrahedron_3_object () const{
return K.construct_tetrahedron_3_object ();
}
Construct_triangle_3 construct_triangle_3_object () const{
return K.construct_triangle_3_object ();
}
Construct_segment_3 construct_segment_3_object () const{
return K.construct_segment_3_object ();
}
Compare_xyz_3 compare_xyz_3_object () const{
return K.compare_xyz_3_object ();
}
Coplanar_orientation_3 coplanar_orientation_3_object () const{
return K.coplanar_orientation_3_object ();
}
Orientation_3 orientation_3_object () const{
return K.orientation_3_object ();
}
//delaunay triangulation traits
typedef typename K_::Line_3 Line_3;
typedef typename K_::Object_3 Object_3;
typedef typename K_::Ray_3 Ray_3;
typedef typename K_::Coplanar_side_of_bounded_circle_3 Coplanar_side_of_bounded_circle_3;
typedef typename K_::Side_of_oriented_sphere_3 Side_of_oriented_sphere_3;
typedef typename K_::Compare_distance_3 Compare_distance_3;
Coplanar_side_of_bounded_circle_3 coplanar_side_of_bounded_circle_3_object() const{
return K.coplanar_side_of_bounded_circle_3_object();
}
Side_of_oriented_sphere_3 side_of_oriented_sphere_3_object() const{
return K.side_of_oriented_sphere_3_object();
}
Compare_distance_3 compare_distance_3_object() const{
return K.compare_distance_3_object();
}
};