Hi.

In a project of mine, I have many different files 1.h, 1.cpp, 2.h, 2.cpp, 3.h, 3.cpp etc.
Some of the functionality in them depend on veriables or functions in the others.
If I juggle the #includes around I can get around most of it, for instance, if functionality in 2.cpp requires a variable from 3.cpp, then #include "3.h" goes first.

Sometimes however I can not do that, and use global variables and the extern keyword which works fine.
However, I am at a point (very deep into my project) where I could do with, not a variable but a function from another file, the function is not within a class.

My question is, is there a similar method for functions as there is with extern keywaod for variables.

I know my project might not have been thought thougrily through and some things are afterthoughts you see. So until I re-design it I'm hoping there is a workaround.

Thanks for reading.

I struggle to think what you have done with your code if you are unable to use a function that has been written in a different cpp file. So long as the cpp file you want to use it in has seen the prototype of that function (which is usually done with a header file), when time comes to link the code together, the linker will find that other function and use it.

Here's an example:

function.h

int someFunction(int x);

function.cpp

int someFunction(int x)
{
  return 7;
}

main.cpp

#include "function.h"

int main()
{
  int value = 8;
  someFunction(value);
}

That's it. The function from function.cpp is used in main.cpp so long as you've told the linker to use function.o (which the compiler made from function.cpp). If you're using Visual Studio that probably just means you need to be sure they're in the same "project" or whatever word VS uses.

It's a mess I know. This is my first attempt at bringing all the code together, and it will be re-thought and designed when I look at the whole thing working.

What needs this new function a added is a class member, where the method is defined inside the class in the class.h file.

From my search about this problem I read I should #include "newfuncs.cpp" but that just seems wrong and weird, I also don't think you're supposed to include header files in other header files.

I ought to have mentioned this before, I think I can do it with hacky nasty code, just wondering if there was some functionality in C++ I might have been missing, to at least patch it up in a half decent fashion.

From my search about this problem I read I should #include "newfuncs.cpp"

As a rule of thumb, never ever ever #include a cpp file.

I also don't think you're supposed to include header files in other header files.

You absolutely are supposed to do that. When you #include a file, nothing magic happens. It just copies that file into the place where you #include it, exactly as if you had copied it out letter by letter yourself.

just wondering if there was some functionality in C++ I might have been missing, to at least patch it up in a half decent fashion.

Your use of global variables and extern is the nasty hacky version. What I did above is the elegant, intended way.

where the method is defined inside the class in the class.h file.

So you have a class method of one class, and you want another class to have an identical method, or do you want that other class to call the first class' method?

Like declaring global variables as extern, you can do that with functions as well. Here is an example from a header file in my current work project:

extern int setup_notify_events();

@rubberman: using just extern in front of a function declaration in C/C++ has absolutely no effect. Function declarations declare functions with external linkage by default, adding the extern keyword doesn't change anything.

@Suzie999:
You need to provide some sort of explicit example of what you are trying to do. Could you just write up a small example (similar to the example code Moschops presented) that demonstrate the problem you are facing? It will be a lot easier to understand that way. I'm really confused as to what you are trying to do or what your problem is, it will be easier with some simple bits of code to show it.

How about a nice example?

//moo.h

#include <string>
extern std::string moo; //Declare the variable externally without actually creating it.


//moo.cpp
#include "moo.h"
std::string moo="Moo."; //Now we initialize the variable and give it an actual definition.


//moomain.cpp
#include "moo.h"
#include <iostream>
int main(){
    //Print out a happy message using the externally declared variable.
    std::cout<<"The cow says '"<<moo<<"' How I hate that cow."<<std::endl;
    return 0;
}


//Command line I used:
(Compiling)
$ g++ moo.cpp moomain.cpp -o moomain
(Running)
$ ./moomain
(Expected output)
The cow says 'Moo.' How I hate that cow.

Thank you all for your replies, helped me to realise a very simple solution, to just put the extra function into its own cpp and h files and include it before all other includes.

I really appreciate your time Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.