0

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.

pa12
  • 1,493
  • 4
  • 19
  • 40

2 Answers2

3

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()
*/
}
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
1

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*) {
   ...
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012