Pregunta

In one of classes I need to use the a function from another class once. I am wondering if there is another way to call the class without using # include "OtherFunction.h" at the top of the page. I thought I could use OtherFunction:: instead, but the eclipse gives me an error when I do that. Is there another way to call this function from another class without using #include or adding the additional overhead of inheritance?

These classes are not static. I am writing unit tests and I am running into a redeclaration error when I test a function in my driver class that calls my supporting class. So I am trying to find a solution where I can test both the functions in the supporting and driver classes.

All my classes are surrounded by

#ifndef SHAREDSCANNINGFUNCTIONS_H_
#define SHAREDSCANNINGFUNCTIONS_H_

#endif 

So turns out that inline function need to be before #endif. Learn something new everyday. Thanks to all those that helped

¿Fue útil?

Solución

No, it's not possible. And why whould you do that ? What is the cost of writing #include ?

To be more precise, how would your file be aware of you class definition if you don't tell it where to find it ?

Edit : @Gonmator One should not do it, it's bad. Having your class definition in the cpp file is horrible. The only moment one can do it is when the class is only used by this file. Then it should be static

Edit2: Well, i can't figure out how to put code in comment. Se, i meant to say :

#ifndef __YOURFILENAME_H__
#define __YOURFILENAME_H__
class myclass{}
#endif

It look like you are having a problem like this :

//classA.hh
class A{
  B instanceofB;
}

//classB.hh
class B{
  A instanceofA;
}

Doing this make some problems. Compiler says "Error : Use of incomplete blahblah.."

All you have to do is to add the class name on the top of your class definitions, like this :

//classA.hh
class B;
class A{
  B instanceofb;
}

//classB.hh
class A;
class B{
  A instanceofA;
}

Edit3: Look like i didn't understand the problem. But my answer can still be right afterall.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top