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;
}
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
Securtiy is a misspelling in your Security.cpp file.
Related
Long story short I want to put my .h and .cpp files in subfolders (include and src respectively) and reference them in my main.cpp file but I am receiving an error of:
main.cpp:(.text+0x47): undefined reference to `Kmer::Kmer()'.
when compiling using:
g++ -I /path/to/MyFolder/include main.cpp.
My files are structured like below:
MyFolder
main.cpp
include
Kmer.h
src
Kmer.cpp
//main.cpp
#include <iostream>
#include "Kmer.h"
using namespace std;
int main() {
Kmer k;
return 0;
};
//Kmer.h
#pragma once
class Kmer{
public:
Kmer();
protected:
private:
};
//Kmer.cpp
#include "Kmer.h"
#include <iostream>
using namespace std;
Kmer::Kmer(){
// code here
cout << "Kmer created" << endl;
}
I appreciate the help!
You are not compiling Khmer.cpp. You need to add it to your g++ compile line
g++ -o <YOUR APPLICATION NAME> -I /path/to/MyFolder/include main.cpp src/Khmer.cpp
I am trying to declare two classes C1 and C2 in files nstest1.h and nstest2.h which are defined in files nstest1.cpp and nstest2.cpp respectively. Both the classes are defined under same namespace.
Following are the files :
//nstest1.h
namespace Mine{
class C1{
public:
void callme();
};
}
//nstest2.h
namespace Mine {
class C2 {
public:
void callme();
};
}
//nstest1.cpp
#include<iostream>
#include "nstest1.h"
using namespace std;
using namespace Mine;
void Mine::C1::callme(){
std::cout << "Please call me " << std::endl;
}
//nstest2.cpp
#include<iostream>
#include "nstest2.h"
using namespace std;
using namespace Mine;
void Mine::C2::callme(){
std::cout << "Please call me too" << std::endl ;
}
Following file tries to use this classes using namespace Mine.
//nstest.cpp
#include<iostream>
#include "nstest1.h"
#include "nstest2.h"
using namespace std;
using namespace Mine;
int main(){
Mine::C1 c1;
Mine::C2 c2;
c1.callme();
c2.callme();
return 0;
}
When I compile using command "g++ nstest.cpp", I get following error :
/tmp/cc2y4zc6.o: In function `main':
nstest.cpp:(.text+0x10): undefined reference to `Mine::C1::callme()'
nstest.cpp:(.text+0x1c): undefined reference to `Mine::C2::callme()'
collect2: error: ld returned 1 exit status
If the definitions are moved to the declaration files (nstest1.h and nstest2.h), it works fine. Not sure whats happening here. Am I missing something ?
Thanks in advance :) .
You need to include the other .cpp files when building the program.
Option 1: Compile all the files and build the executable in one command
g++ nstest.cpp nstest1.cpp nstest2.cpp -o nstest
Option 2: Compile each file separately and then build the executable after that
g++ -c nstext1.cpp
g++ -c nstest2.cpp
g++ -c nstest.cpp
g++ nstest.o nstest1.o nstext2.o -o nstest
Your problem happens at link time. Your headers are fine. But you should compile the other cpp files aswell.
This is a minimal program that I made to understand this problem better.
ADT.h
#ifndef ADT_H
#define ADT_H
class ADT {
public:
void print();
};
#endif
ADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
void ADT::print()
{
cout << "This program works." << endl;
}
testADT.cpp
#include <iostream>
#include "ADT.h"
using namespace std;
int main(void)
{
ADT sa;
sa.print();
return 0;
}
I compiled it with the vim/minGW compiler my school provided me like so:
g++ testADT.cpp
Which produced the following error:
C:\Users\King\AppData\Local\Tempcc6eoWAP.o:testADT.cpp(.text+0x15 reference to 'ADT::print()'
collect2.exe error: ld returned 1 exit status
Can you explain this error message and indicate the error in my code?
You didn't post the error, but I see that you're missing the semicolon after void print()in the header.
EDIT: That's a linker error. Each source file should be compiled into an object file; then the object files linked:
g++ -c -oADT.o ADT.cpp
g++ -c -otestADT.o testADT.cpp
g++ -oADT ADT.o testADT.o
You can also do it in one line as in michaeltang's answer, but then you can't recompile the sources individually (the 2 step method scales better).
You should also compile ADT.cpp
g++ -o testadt testADT.cpp ADT.cpp
I wanted to learn using header files. and I got an error. here is my code:
printmyname.h:
void printMyName();
printmyname.cpp:
#include "printmyname.h"
void printMyName() {
cout << "omer";
}
try.cpp (main file):
#include <iostream>
#include "printmyname.h"
using namespace std;
int main() {
printMyName();
return 0;
}
Here is the error:
undefined reference to `printMyName()`
What's is the problem?
Undefine reference has nothing to do with your header file in this case. It means the linker cannot find the implementation of printMyName which is in printmyname.cpp. If you are using g++, you should try:
g++ try.cpp printmyname.cpp -o yourBinaryName
If you are using a makefile, you should add dependency(printmyname.cpp) correctly for try.cpp.
Edit:
As #zmo suggest in his comment:
you can also do it through a two times compilation (more suitable with Makefiles):
g++ -c printmyname.cpp
g++ try.cpp printmyname.o -o yourBinaryName
If you are using Windows, you need to add the printmyname.cpp to your project too.
Consider adding an include guard to your header
#ifndef PRINTMYNAME_INCLUDED
#define PRINTMYNAME_INCLUDED
void printMyName();
#endif
You will also need to move the #include <iostream> and using namespace std; from the try.cpp to the printmyname.cpp file.
You need to add code/definition in printMyName.cpp inside printMyName.h only.
void printMyName();
{
cout << "omer";
}
I have created three separate files. The first is the main.cpp the 2nd is a header file called "statistics.h" which has the declarations for the two functions i am getting the error and the 3nd is a file called statistics.cpp which holds the implementation of the two functions.
Here is my main file:
#include <iostream>
#include "statistics.h"
using namespace std;
int main()
{
cout<<"This program provides the average and the standard deviation of 1,2,3 or 4 numbers."<< endl;
while(true){
start:
unsigned int howmany;
cout<<endl;
cout<<"How many numbers do u wish to receive as input?? ";
cin >> howmany;
if (howmany>4){
cout<<"You should pick at most 4 numbers u idiot!!!"<< endl<<endl;
goto start;
}
if(howmany==0){
return 0;
}
cout<< endl;
double nums[howmany];
for (int i=0;i<howmany;i++){
cout<<"Give me the number "<<i+1<<":";
cin>>nums[i];
}
double avg=average(nums,howmany); /////////////////////////////
double stdev=standard_deviation(nums,howmany,avg); ////////////////////
cout<< endl<<"Average: "<<avg<<". Standard Deviation: "<< stdev<< endl;
}
}
My header file is:
#ifndef STATISTICS_H_INCLUDED
#define STATISTICS_H_INCLUDED
double average(double ar[],int hm);
double standard_deviation(double ar[],int hm,double avrg);
#endif // STATISTICS_H_INCLUDED
And my implementation file statistics.cpp is:
#include<iostream>
#include"statistics.h"
#include <math.h>
using namespace std;
double average(double ar[],int hm){
double sum=0.0;
double average;
for(int i=0;i<hm;i++){
sum+=ar[i];
}
average=sum/hm;
return average;
}
double standard_deviation(double ar[],int hm,double avrg){
double std_dev;
double sum=0;
double ans;
for(int i=0;i<hm;i++){
sum+= ((ar[i]-avrg)*(ar[i]-avrg));
}
ans=sqrt(sum/hm);
return ans;
}
I am getting the errors in my main file (i have marked the corresponding lines with consecutive ////////). What could be wrong?? I am sure its something stupid from my part.
As i am using codeblocks i finally found the solution. I just needed to add #include "statistics.cpp" to my main.cpp file.
You forgot to link statistics.o together with main.o, so your hypothetical executable will not contain the function definitions.
(The hypothetical executable can thus not exist, so you're getting a linker error.)
Variation 1
Where you're writing something like:
g++ main.cpp -o myExecutable
instead write:
g++ main.cpp statistics.cpp -o myExecutable
Variation 2
Where you're writing something like:
g++ -c main.cpp
g++ main.o -o myExecutable
instead write:
g++ -c main.cpp
g++ -c statistics.cpp
g++ main.o statistics.o -o myExecutable
If you're still having trouble, try reversing the order in which you provide the filenames to g++ (though the above should be correct).
Incidentally, you are not allowed to define an array with variable dimensions. Use a std::vector instead.
As i am using codeblocks i finally found the solution. I just needed to add #include "statistics.cpp" to my main.cpp file.
!!! This is NOT a solution. Do not include implementation files. You will fall into a pit of despair, decay and disorganization.
Everything was fine with your use of header files. You just need to tell your IDE to compile both files as a single project. Consult the documentation.