C++ Help
Posted: Mon Mar 24, 2014 7:47 pm
Hey everyone, I'm just got started learning C++ because I don't want to use C# anymore... But their is an issue I am having with something about having unresolved external symbols.
I have another project in the same solution which references to the .lib file produced by my solution with the Thread.h etc in... It compiles fine, but when I input "thread.h" and use Thread, it gives me a compile error "Error 1 error LNK2019: unresolved external symbol "public: __thiscall Thread::Thread(int (__cdecl*)(void))" (??0Thread@@QAE@P6AHXZ@Z) referenced in function _main"
Here is the code:
Platform.h
Thread.h
Thread.cpp (Win32 thread code)
I have another project in the same solution which references to the .lib file produced by my solution with the Thread.h etc in... It compiles fine, but when I input "thread.h" and use Thread, it gives me a compile error "Error 1 error LNK2019: unresolved external symbol "public: __thiscall Thread::Thread(int (__cdecl*)(void))" (??0Thread@@QAE@P6AHXZ@Z) referenced in function _main"
Here is the code:
Platform.h
Code: Select all
#include <time.h>
#include <iostream>
#define PLATFORM_WIN32
//#define PLATFORM_LINUX
//#define PLATFORM_MACOSX
#ifdef PLATFORM_WIN32
#include "windows.h"
#endif
Code: Select all
#include "Platform.h"
class Thread {
public:
Thread(int (*callback)());
Thread(int (*callback)(void* state));
void Start();
void Start(void* state);
bool Abort();
static void Suspend(int interval) {
//hang until the call time + the interval
int destTime = clock() + interval;
while(clock() < destTime);
}
~Thread();
};
Code: Select all
#include "Platform.h"
#ifdef PLATFORM_WIN32
#include "Thread.h"
int noArgsCallback(void* state);
void* p_NoArgsCallback;
void* p_Callback;
void* p_Handle;
#pragma region Constructor
Thread::Thread(int (*callback)()) {
//since the thread would need a state argument,
//we need to create a new callback which invokes
//the argument-less callback under the new
//threaded context.
p_Callback = (void*)noArgsCallback;
p_NoArgsCallback = callback;
}
Thread::Thread(int (*callback)(void* state)) {
p_Callback = callback;
p_NoArgsCallback = 0;
}
int noArgsCallback(void* state) {
//get the callback with no arguments which
//was passed to the state argument.
int (*callback)() = (int(*)())state;
//invoke the callback
return callback();
}
#pragma endregion
#pragma region Destructor
Thread::~Thread() {
Abort();
//release pointers
delete(p_Callback);
delete(p_Handle);
if(p_NoArgsCallback==0){return;}
delete(p_NoArgsCallback);
}
#pragma endregion
#pragma region Start
void Thread::Start() {
Start(p_NoArgsCallback);
}
void Thread::Start(void* state) {
//create the thread
p_Handle = CreateThread(
0,
0,
(LPTHREAD_START_ROUTINE)p_Callback,
state,
0,
0
);
}
#pragma endregion
#pragma region Abort
bool Thread::Abort() {
bool result = false;
//get the exit code
unsigned long exitCode = 0;
result = GetExitCodeThread(p_Handle, &exitCode);
if(!result) { return false; }
//terminate the thread
return TerminateThread(p_Handle, exitCode);
}
#pragma endregion
#endif