Author Topic: Header file  (Read 3044 times)

rwaves

  • Guest
Header file
« on: June 01, 2011, 04:55:44 am »
Learning C++ and trying to create a header file to add two intergers.


add.h header file looks like this:

#ifndef ADD_H_INCLUDED
#define ADD_H_INCLUDED

int add(int x, int y);

#endif 


main.cpp source file looks like this:

#include <iostream>
#include "add.h"

int main()
{
  using namespace std; 
  cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
  return 0;
}

Keep getting error:  "In function 'main' undefined reference to 'add(int, int)'. I think my code is OK. Has anyone had trouble with this too? Thank you for your help!!


Offline vdave420

  • Single posting newcomer
  • *
  • Posts: 4
Re: Header file
« Reply #1 on: June 01, 2011, 09:51:39 am »
Nope.  You haven't defined the function, merely declared it.  You won't be able to link an executable without a corresponding definition for the function.

  -dave-

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Header file
« Reply #2 on: June 01, 2011, 11:09:17 am »
Absolutely unrelated to C::B and therefore violating our forum rules.

Topic locked !!