how to include a function from another file

Apr 13, 2009 at 8:46am
Hello,
i was wondering how do you include a function from another file to the main file, something like this

 
#include <DrawLine.cpp> 


and then lets say the function i need is called DrawLine(); from the file DrawLine.cpp, and i need to use it like this:

1
2
3
4
5
6
7
8
9
10
#include <DrawLine.cpp>
using namespace sdt;
int main()

{

//blah blah blah
DrawLine();

}



I hope i wrote clear enough, im a total newb lolz
Last edited on Apr 13, 2009 at 8:48am
Apr 13, 2009 at 8:50am
in header declare the function. in .cpp file define the function.
and then include the .h file and not .cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Drawline.h
void DrawLine();

//Drawline.cpp
void DrawLine()
{

//implementation
}

//main.cpp

#include "Drawline.h"
int main()
{
DrawLine();

return 0;
}
Apr 13, 2009 at 8:56am
so you need 2 files for this to work? and how to you make the .h files? simply rename the .cpp extension? and dev-cp will recognize it? lolz, didnt knew.
0h, and abit of topic, what would be the difference between

void LineDraw()
and
int LineDraw()

?


EDIT: i tried how you said, i created a DrawLine().h file containing int DrawLine();
then i created the DrawLine().cpp file containing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <iostream>

using namespace std;

int DrawLine ()   // this is the line drawing function
{
    const int Length = 80;  //change accordingly
    
    for (int i = 0; i < Lenght; i++)    // loop
    
          cout << "*" << endl;     
    
}



and the main file containing :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <iostream>
#include "DrawLine().h"

using namespace std;

int main(int argc, char *argv[])
{
    DrawLine();
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}





so i opened all 3 files in devcpp on one window (different tabs) and all of them are in the same folder, and when compiling this error comes:

[Linker error] undefined regerence to `DrawLine()'
id returned 1 exit status
[Build Error [Project1.exe] Error 1
Last edited on Apr 13, 2009 at 9:09am
Apr 13, 2009 at 9:40am
simply rename the .cpp extension? and dev-cp will recognize it


yes.

0h, and abit of topic, what would be the difference between

void LineDraw()
and
int LineDraw()



the first one will not return anything
second one will return an int. thats it.


create DrawLine.h and DrawLine.cpp and not DrawLine().h

File one will be
1
2
//DrawLine.h
int DrawLine();




file second:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//DrawLine.cpp
#include <cstdlib>
#include <iostream>
#include "DrawLine.h"

using namespace std;

int DrawLine ()   // this is the line drawing function
{
    const int Length = 80;  //change accordingly
    
    for (int i = 0; i < Lenght; i++)    // loop
    
          cout << "*" << endl;    
    
}




Third file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//main.cpp
#include <cstdlib>
#include <iostream>
#include "DrawLine.h"

using namespace std;

int main(int argc, char *argv[])
{
    DrawLine();    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


Topic archived. No new replies allowed.