2

I have 2 class as follows each has header file class one has a function as follows:

  int call_thread() 
  {
    pthread_create(&thread, NULL, &Print_data, NULL);
    return 0;
  }

I am trying to call this method in class 2:

void position::tick(schedflags_t flags) 
{
    call_thread();
    }

I always get an error undefined reference to 'call_thread()'. I also tried to declare it as static but it gave me an error: that is "" Static function declared but not defined"". What am I missing? Note: I included the header file of class 1 ofcourse.

tostao
  • 2,803
  • 4
  • 38
  • 61
Zeyad
  • 771
  • 3
  • 9
  • 14
  • 3
    How are you compiling? Seems to me that compiler can't find the .cpp file with the definition. – olevegard Jun 18 '13 at 11:44
  • If `call_thread()` is a mere function, it MUST be declared at least once in a `.h`-file or even `.cpp`-file – bash.d Jun 18 '13 at 11:45
  • the call_thread is declared in the h file of the 1st class and the second class just includes the h file of the 1st – Zeyad Jun 18 '13 at 11:48
  • @olevegard I am comping using a g++ and including the -lpthread – Zeyad Jun 18 '13 at 11:49
  • @Zeyad what's the full compiler command? Does it include the .cpp file in which the function is defined? – olevegard Jun 18 '13 at 11:51
  • There are three different meanings to "static" in C++, I guess you are confused there. Please distill a minimal example from your code, from there it should be obvious. Also, read the FAQ concerning the differences between a function and a memberfunction, that info is essential! – doomster Jun 18 '13 at 11:51
  • g++ pos.cpp -o position -lpthread this is what i used to compile exactly – Zeyad Jun 18 '13 at 11:53
  • 1
    You need all *cpp files on that commandline, #include doesn't solve that for you. – doomster Jun 18 '13 at 11:54
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). In this case, the problem is probably [this one](http://stackoverflow.com/a/12574400/20984). – Luc Touraille Jun 18 '13 at 11:59
  • 1
    @Zeyad Does pos.cpp contain the definition for the function? – olevegard Jun 18 '13 at 12:10
  • BTW: Use "-pthread" instead of "-lpthread". – StellarVortex Jun 18 '13 at 12:34
  • @olevegard yes it does. – Zeyad Jun 18 '13 at 13:48

2 Answers2

2

My guess is that you have declared and defined call_thread() in Class1:

class Class1
{
    public:
    int call_thread(){...}
}

Then you are trying to call this method in class position:

void position::tick(schedflags_t flags) 
{
    call_thread();
}

call_thread() is a Class1 member function and you need either an instance of Class1 to call it or a class name if it's static member:

void position::tick(schedflags_t flags) 
{
    //for static member
    Class1::call_thread(); 

    //for instance member
    Class1 object;
    object.call_thread();
}
undercover
  • 176
  • 4
1

If you have classes you should have objects so you can call the public methods (functions).

Try something like below:

in Class1.h:

class Class1{ 
public:
  Class1(); //constructor
  ~Class1(); //destructor

  int call_thread();
}

Then in Class2 you should have an object, something like:

void position::tick(schedflags_t flags) 
{
  Class1 obj;
  obj.call_thread();
}
flazzari
  • 173
  • 5