Kevin
Kevin

Reputation: 111

undefined reference to class::function when I try to compile

I have three files: Main.cpp, Security.h, and Security.cpp.

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

Answers (2)

edtheprogrammerguy
edtheprogrammerguy

Reputation: 6049

Securtiy is a misspelling in your Security.cpp file.

Upvotes: 2

nullptr
nullptr

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

Related Questions