Call another cpp file

Posts 1–15 of 16 · Page 1 of 2
Call another cpp file
For example, I got the main.cpp , when the main() is finish i want to call the joke.cpp how do i do that?
Use header files..Buddy
you can make your functions inside your header files, then call them in your main function in main.cpp.
Code:
//joke.h (Header file)
#ifndef JOKE_H
#define JOKE_H
#endif

void someFunction(int someParam); //The full function is stored in joke.cpp
Code:
//main.cpp
#include "joke.h"

int main()
{
    someFunction(22);
    return 0;
}
Quote Originally Posted by master131 View Post
Code:
//joke.h (Header file)
#ifndef JOKE_H
#define JOKE_H
#endif

void someFunction(int someParam); //The full function is stored in joke.cpp
Code:
//main.cpp
#include "joke.h"

int main()
{
    someFunction(22);
    return 0;
}
Yeah that works, But i still trying to understand for what is others cpp files..why would i add them? Its not like vb or c# that you can make form2.show()...I dont understand it..I can just add new functions on the main.cpp idk..
Quote Originally Posted by Lyoto Machida View Post


Yeah that works, But i still trying to understand for what is others cpp files..why would i add them? Its not like vb or c# that you can make form2.show()...I dont understand it..I can just add new functions on the main.cpp idk..
Its Called OOP..
Once you get deep into c++ its all about OOP (Object-oriented programming - Wikipedia, the free encyclopediaⓘ )

Like say you got a whole bunch of Directx shit in your project & a whole bunch of other shit
You dont wanna cluster all that into one cpp file. So ya do this

[Highlight=cpp]
//Directx shit header

#pragma once

class Directx{
public:

void DrawSomething();
};
extern Directx D3D;

[/Highlight]


[Highlight=cpp]
// Directx CPP File

#include "Directx.h"

Directx D3D;

void Directx:rawSomething()
{
//Render shit
}


[/Highlight]


[Highlight=cpp]
//Main CPP

#include "Directx.h"

int main()
{
D3D.DrawSomething();
}


[/Highlight]

Do you kinda get what im saying ?
Quote Originally Posted by whit View Post
Its Called OOP..
Once you get deep into c++ its all about OOP (Object-oriented programming - Wikipedia, the free encyclopediaⓘ )

Like say you got a whole bunch of Directx shit in your project & a whole bunch of other shit
You dont wanna cluster all that into one cpp file. So ya do this

[Highlight=cpp]
//Directx shit header

#pragma once

class Directx{
public:

void DrawSomething();
};
extern Directx D3D;

[/Highlight]


[Highlight=cpp]
// Directx CPP File

#include "Directx.h"

Directx D3D;

void Directx:rawSomething()
{
//Render shit
}


[/Highlight]


[Highlight=cpp]
//Main CPP

#include "Directx.h"

int main()
{
D3D.DrawSomething();
}


[/Highlight]

Do you kinda get what im saying ?
hmm i think i get it..thank you all
/request close
Quote Originally Posted by master131 View Post
Code:
//joke.h (Header file)
#ifndef JOKE_H
#define JOKE_H
#endif

void someFunction(int someParam); //The full function is stored in joke.cpp
Improper header guard.

Quote Originally Posted by whit View Post
Its Called OOP..
Once you get deep into c++ its all about OOP (Object-oriented programming - Wikipedia, the free encyclopedia )
Incorrect. C++ is a multi-paradigm language. Ex: STL is not OOP.

Quote Originally Posted by whit
Code:
extern Directx D3D;
Avoid global state. Instantiate the object within main. If it must be global, use the singleton pattern.
Quote Originally Posted by Fovea View Post
Improper header guard.

Incorrect. C++ is a multi-paradigm language. Ex: STL is not OOP.

Avoid global state. Instantiate the object within main. If it must be global, use the singleton pattern.
Get off me, I was doing it in a rush.

Code:
//joke.h (Header file)
#ifndef JOKE_H
#define JOKE_H
void someFunction(int someParam); //The full function is stored in joke.cpp
#endif
Better?
Quote Originally Posted by Lyoto Machida View Post
Yeah that works, But i still trying to understand for what is others cpp files..why would i add them? Its not like vb or c# that you can make form2.show()...I dont understand it..I can just add new functions on the main.cpp idk..
Is this what you're asking if you can do?

Code:
#include <iostream>

using namespace std;
void joke();

int main()
{
    cout << "Hello world!" << endl;
    joke();
    return 0;

void joke()
{
    cout << "Lol Funny Joke" << endl;
}
}
Quote Originally Posted by cfsharp View Post
Is this what you're asking if you can do?

Code:
#include <iostream>

using namespace std;
void joke();

int main()
{
    cout << "Hello world!" << endl;
    joke();
    return 0;

void joke()
{
    cout << "Lol Funny Joke" << endl;
}
}
Syntax error..Tsk Tsk
Quote Originally Posted by whit View Post
Syntax error..Tsk Tsk
Oops didn't mean to put that in main lol, i rushed it D:
It might be beccause you want to reuse some other cpp files from a previous project or to stop cluttering 1 file with a whole bunch of code that can be used again.
You don't "call a CPP file" - a cpp file only contains function definitions (and their bodies, and variables).

You must call a function inside the CPP file. The easiest way to do this is to use the C header file and declare a function which your CPP file has, and call it O:

Brainfuck.
You use multiple cpp files to keep your code organized. It'd be hard to find what you want in a 100k line project if it's all in a single cpp file
Posts 1–15 of 16 · Page 1 of 2

Post a Reply

Tags for this Thread

None

Talk with us