Reputation: 111
I have three files: Main.cpp, Security.h, and Security.cpp.
I have declared my class Security (including a function) in my header file.
I have defined the function in Security.cpp.
My header file has been included in both Main.cpp and Security.cpp.
In Main.cpp, I'm creating an object, and attempting to run the member function and keep getting a compile error.
Main.cpp
#include<iostream>
#include "Security.h"
using namespace std;
int main()
{
Security S1;
S1.Driver();
}
Security.h
class Security
{private:
public:
void Driver();
};
Security.cpp
#include<iostream>
#include<fstream>
#include "Security.h"
using namespace std;
void Securtiy::Driver()
{
cout << "Enter a number: ";
int answer;
cin >> answer;
cout << answer;
}
Upvotes: 1
Views: 797
Reputation: 11058
You should compile both files, because the definition of Security::Driver is in Security.cpp.
The easiest way would be to invoke a single command:
g++ Main.cpp Security.cpp
However, if you want to compile the files separately, you must compile them into an intermediate ('object') format using -c
flag:
g++ -c Main.cpp
g++ -c Security.cpp
This will give you two object files. Now link them:
g++ Main.o Security.o
Upvotes: 2