Reputation: 1503
I have a class A(has a.h and a.cpp file) which I am importing to main.cpp. I created an object of the class A and trying to access the methods in class I get undefined reference to `A::Reset(unsigned int*, unsigned int*)'.
I am not sure whats wrong in my code
//a.h
#ifndef _A_H_
#define _A_H_
class A
{
public:
A();
void Reset();
};
#endif
//a.cpp:
#include "A.h"
A::A()
{
Reset();
}
void A::Reset()
{
}
//main.cpp
#include "A.h"
int main(int argc, const char * argv[])
{
A *aObj = new A;
aObj->Reset();
}
Any help would be appreciated.
Upvotes: 0
Views: 136
Reputation: 500923
First of all, you need to compile and link both A.cpp
and main.cpp
when building the executable. For example:
g++ -o main A.cpp main.cpp
As to the missing compare()
function, make sure that it's declared in A.h
:
class A {
...
int compare(unsigned int*, unsigned int*);
}
and defined in A.cpp
:
int A::compare(unsigned int*, unsigned int*) {
...
}
Upvotes: 1
Reputation: 2509
Correct you main.cpp file as this:
#include "a.h"
int main(int argc, const char * argv[])
{
A *aObj = new A;
aObj->Reset();
/*
or
A aObj;
aObj.Reset()
*/
}
Upvotes: 3