How to access method from another source file?

Hi...

I have been struggling concerning using methods from another source file...

For example I have a file called a.cpp, and I have a function in it
int sum(int a, int b)
{
............
}

I tried to access this method from another b.cpp.

But I get errors o.O

Can I get a small example of how to do this correctly?

Thanks...
Make a header file, say h1.h and put this in it:
int sum(int, int);

and then just #include "h1.h" in every .cpp file you want.
The way to do this is to create a 'header' file for your source, called a.h
1
2
3
4
5
6
7
// a.h
#ifndef _A_H // must be unique name in the project
#define _A_H

int sum(int a, int b); // prototype declaration of the function in a.cpp

#endif 

You then include that header file in any file you want to access the function in:
1
2
3
4
5
6
7
8
// b.cpp
#include "a.h" // get the prototype declaration for the functions in a.cpp

int main()
{
    sum(2, 8); // use the function
}
Last edited on
Thanks for the answers. But I tried that, I think it's because I haven't initialized my object?

I have my code down below:

1
2
3
4
5
6
7
8
9
#pragma once
class people
{
public:
	people(void);
	~people(void);
	int sum(int a, int b);
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "StdAfx.h"
#include "people.h"


people::people(void)
{
}



people::~people(void)
{
}

int sum(int a, int b)
{
	int result;
	int result = a + b;

	return result;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "people.h"

int _tmain(int argc, _TCHAR* argv[])
{
  srand ( time(NULL) );
  printf ("First number: %d\n", rand() % 2 + 1);
  
  sum(3,4);	

  system("PAUSE");	
  return 0;
}

I get red lines under my "sum"

I try to use my function sum() in my main method. But it says "Error:identifier "sum" is undefined. I tried to put "int" infront, then my comma gets an error "Error: expected a')'"

Thanks in advance...
Last edited on
At that line in your main function, the function "sum" does not exist.

This is because it has not been declared anywhere, or defined. You have defined a function called sum in what I assume is your people.cpp file, but your main code cannot see that. Your main code can see the header file, which also does not declare or define a function called sum (although you do declare a class function within your people class called sum, but
sum(3,4);
is not trying to use that class function).

Solution; add declaration of the sum function, like this,

int sum(int a, int b);

to the top of your main code file (sorry, I don't know what you've called it, that one with _tmain in) or to your people.h file

It is true that you haven't initialised your people object, but you're also not even trying to create one or use one.

Last edited on
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
// main.cpp
#include <iostream>
#include "sum.h"

int main ()
{
  int x;
  x = sum(3,4);
  std::cout << x;
  return 0;
}


1
2
3
4
// sum.h

//declare sum function
int sum(int a, int b);



1
2
3
4
5
6
7
// sum.cpp

//define sum function
int sum(int a, int b)
{
  return(a+b);
}


I've not compiled it to test, but it should give you an idea of how this works. Be sure you compile both sum.cpp and main.cpp in your project.

Edit: I have now compiled and tested, and on my system at least this is happy. Hopefully this makes it clear how to go about doing this. I think you were confusing yourself by having that unused "people" class in there
Last edited on
Thank alot for the help, with the help of you guys' code examples, I found out it was because I couldn't do this:
1
2
3
4
5
6
7
8
9
//sum.h

class sum
{
public:
	sum(void);
	~sum(void);
	int sum(int a, int b);
};


I had to make it this way.
1
2
3
4
5
6
7
8
9
10
int sum(int a, int b);

class sum
{

public:
	sum(void);
	~sum(void);

};


Although I dont really understand why :P

But thanks alot

Last edited on
I don't think you quite understand what a class is for. There is no need at all for you to be defining a class if you're not going to use a function from within it.
That's right. This is not Java; you do not have to have a class containing every function you ever use, and you don't even use the class in this case anyway.
Topic archived. No new replies allowed.