can a .cpp file call another .cpp file?

Pages: 12
Bourgond Aries wrote:
What did I get wrong?
It makes no sense to call this functions from outside. The concept must be changed -> thread
@OP No offence, but are you comfortable with how functions work? Because it doesn't look that way.
@OP

You obviously have no idea what you're doing, so let me help you clarify your definition of '.cpp file':

Back in the day when we did not have IDe (integrated development environments, which is most likely what you're useing), you would have to put everything into 1 file.

The advantages of an IDE are that you can create projects. Using this, you can create different .cpp and .h files. you emplement functions in .cpp files, and write their definition (like a forward declaration) in the header files (which includes declaring default arguments). Using the preprocessor directive #include , you can add these .cpp files to any other .cpp files as though you are adding those 'forward declarations'. You CAN NOT CALL .CPP FILES. You can only call the ufunctions within those .cpp files, if they are declared in the header files. It is good practice to include the header file in the .cpp file it is related to.

for example, if i have common.cpp, and i have common.h, i would include common.h in common.cpp so that if i dont want to have some functions in common.h that use other functions in common.cpp, their default arguments are defined.

So, if you really wanted to generalize it, it would be more like adding functions to other .cpp files more conveniently. I like to use header/.cpp files as a way to create functions that i can use in multiple programs simply by adding their .cpp/header into the project, and including them in the files i need to use their functions in.

Good luck.
Last edited on
ok i understand you can't call a .cpp file from another .cpp so i changed some things:
first.cpp is the main, second.cpp contains the code for all the functions
second.cpp now has a header file where all the functions are defined

i am trying to call this function from second.cpp and run it in first.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void CServiceBase::Start(DWORD dwArgc, PWSTR *pszArgv)
{
	log << "CSampleBase start is running" << std::endl;
    try
    {
        SetServiceStatus(SERVICE_START_PENDING);

        OnStart(dwArgc, pszArgv);
.
        SetServiceStatus(SERVICE_RUNNING);
    }
    catch (DWORD dwError)
    {
        WriteErrorLogEntry(L"Service Start", dwError);

        SetServiceStatus(SERVICE_STOPPED, dwError);
    }
    catch (...)
    {
        WriteEventLogEntry(L"Service failed to start.", EVENTLOG_ERROR_TYPE);

        SetServiceStatus(SERVICE_STOPPED);
    }
}


i tried calling the function by using void Start(DWORD dwArgc, PWSTR *pszArgv); but it doesn't work. am i calling the function wrong?
i used #include second.h in first.cpp
Last edited on
can you show how you're calling it and what errors/warning you're getting please?
@OP:

Here is how you make class .cpp/headers:

in header:

1
2
3
4
5
6
7
8
class Class_name{
public:
    Class_name();

    void example(string, int);
private:
    int myprivate();
};


In .cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Class_name::example(string s = "", int x = 0)
{
    //blablabla
}

int Class_name::myprivate()
{
    //blablabla
    return 0;
}

//constructor
Class_name::Class_name()
{
    cout<< "Set up whatever you want..."<< endl;
}


all these files have to be part of the same project.
Topic archived. No new replies allowed.
Pages: 12