0

So, as far as my cpp knowledge goes, I can call functions which are declared/defined in header files I #include.

Is there any possibility to call functions from the file that includes?

For example: "main.cpp" includes "test.h", So any function from "main.cpp" can make calls to functions from "test.h".

But, in that case, can code from "test.cpp" make calls to functions from "main.cpp" without including it?

Lakmi
  • 1,379
  • 3
  • 16
  • 27
Daniel D.
  • 178
  • 2
  • 15
  • 1
    No, you will need to include main.cpp for it. But this will cause [circular dependency](https://stackoverflow.com/questions/10979031/c-two-classes-with-mutual-needs). – JLev Nov 13 '17 at 11:56
  • Jup, thats what i was thinking... – Daniel D. Nov 13 '17 at 12:00
  • So what if i have to access a global variable from main.cpp? – Daniel D. Nov 13 '17 at 12:01
  • You could pass it as a function argument – xEric_xD Nov 13 '17 at 12:02
  • Then id have to pass the pointer, right? – Daniel D. Nov 13 '17 at 12:11
  • `test.cpp` cannot (or at least, should not) `#include "main.cpp"`, but it *can* `#include "main.h"`. However that is a circular dependency, and is bad. Much better to have both `main.cpp` and `test.cpp`, `#include "global.h"` (which declares the global variable), and then have `global.cpp` which defines it. – Martin Bonner supports Monica Nov 13 '17 at 12:35
  • Of course, not having globals, and passing a reference or pointer around is even better (easier to test for one thing). – Martin Bonner supports Monica Nov 13 '17 at 12:36
  • For able to call function which defined in another file (which not directly included in current file), you can declare function with keyword "extern", for example: **extern void f();**. If that what you want... – Pavel K. Nov 13 '17 at 13:30

1 Answers1

0

No. You cannot call functions declared in another source files from another source files. Also what is the in that? And a good usage separate source from header; Header files include prototypes and declarations while source includes definitions.

eg:

// Shape.h
#ifndef SHAPE_H
#define SHAPE_H

class Shape{
    //...
};
#endif

// Shape2D.h
#ifndef Shape2D.h
#define Shape2D.h

class Shape2D{
    //...
};
#endif

Now source files:

// Shape.cpp
#include "Shape.h"

// Definitions here:
Shape::Shape(){}

// Shape2D.cpp

#include "Shape2D.h"

// Some definitions here:

Shape2D::Shape2D(){}

Now in main.cpp:

#include "Shape2D.h"
// #include "Shape.h"

int DoSomeStuff();

int main(){

    Shape2D a;
    Shape b;

    return 0;
}

int DoSomeStuff(){
    // ....
}

As you can see above I just included Shape2D.h and can use class Shape without including its header file Shape.h That is because Shape2D.h includes it so it's content is added to it then all the content of two files is present in my main source file.

Main.cpp includes Shape.h indirectly.

  • Also keep in mind that what is declared in a source file is scoped to it which means cannot be seen from outside.

Try to something like this:

In Shape.cpp call DoSomeThing:

#include "main.cpp"

int x = DoSomeThing();
  • Above there's at least a compile-time errors: At least class Shape and Shape2D are already defined.

  • What I recommend: Don't include source files, Use Inclusions guards or pragma once to avoid multiple file inclusion.

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
  • But what if a function i defined in a source file calls a function that comes later in the same file...then my compiler say that the function is not declared, since the declaration appears only in the header file which is not directly included in the source file – Daniel D. Nov 14 '17 at 21:55