Calling member functions of a class object which is present in header file included
I am new to the forum i wanted to call a member function of an included header file..Here is code i have written
#include<stdio.h>
#include "Abc.h"
CAbc *a;//CAbc is a class present in Abc.h
int main(int argc,char **argv)
{
int i=10;
float j=15.5;
bool x;
x=a->method(i,j);//method is a member function of CAbc
if(x)
{
printf("Working Correctly");
}
else
{
printf("Not Working");
}
}
If i compile this using
g++ -I/path/to/include code.cpp
I get the Error
/tmp/cc5JgLfF.o: In function `main':
code.cpp:(.text+0x3d): undefined reference to `CAbc::method(int,float)'
collect2: ld returned 1 exit status
I also tried giving
x=a::method(i,j);
for which i get a is not a class or namespace
Please开发者_如何学编程 can anyone tell me am i doing it correctly or not?
It looks as though you are forgetting to include the implementation source/object.
Try this:
g++ -I/path/to/include Abc.cpp code.cpp
as long as your implementation class for Abc.h corresponds to Abc.cpp.
Regards,
Dennis M.
Does your make file or whatever link CABC.obj to the whole exe? If so, then you have forgotten to write the implementation (body, definition) of method...
As others have said, make sure there's an implementation of CAbc.method being linked. Additionally, you need to allocate and deallocate the memory for pointer 'a'; at the moment it's uninitialized.
a = new CAbc();
...
del a
精彩评论