What compiler are you using? You have to make sure all the files are part of your project. In addition the file extensions must be correct: .h for the header file, and .cpp for the file with the definitions.Quote:
I have done this. When I compile, it tells me
[Linker error] undefined reference to `someFunc()'
I'm still checking everything over right now.
Using multiple files is not some alternative to classes. It has to do with program organization. If you had a class, you would organize your program the same way:
main.cpp:
header.h:Code:#include "header.h"
int main()
{
One myone;
myone.display();
Two mytwo(3.2);
mytwo.show();
return 0;
}
definitions.cpp:Code:#ifndef __HEADER_H__
#define __HEADER_H__
////CLASS ONE//////
class One
{
private:
int num;
public:
One(int n);
One();
void display();
};
//////CLASS TWO//////
class Two
{
private:
double time;
public:
Two(double t);
Two();
void show();
};
#endif //__HEADER_H__
You don't have to organize your code that way. Instead, you can just include everything in one file, which I suggest you do if you are having too much trouble with the multiple file concept. For instance, the same program above contained in a single file:Code:#include <iostream>
#include "header.h"
using namespace std;
////DEFINITIONS/////
One::One(int n)
{
num = n;
}
One::One()
{
num = 0;
}
void One::display()
{
cout<<num<<endl;
}
Two::Two(double t)
{
time = t;
}
Two::Two()
{
time = 1.0;
}
void Two::show()
{
cout<<time<<endl;
}
Code:#include <iostream>
using namespace std;
////CLASS ONE//////
class One
{
private:
int num;
public:
One(int n);
One();
void display();
};
//////CLASS TWO//////
class Two
{
private:
double time;
public:
Two(double t);
Two();
void show();
};
//////MAIN//////////
int main()
{
One myone;
myone.display();
Two mytwo(3.2);
mytwo.show();
return 0;
}
////DEFINITIONS/////
One::One(int n)
{
num = n;
}
One::One()
{
num = 0;
}
void One::display()
{
cout<<num<<endl;
}
Two::Two(double t)
{
time = t;
}
Two::Two()
{
time = 1.0;
}
void Two::show()
{
cout<<time<<endl;
}