problems with static members
-
I'm having some trouble with static members in my program. This is what my code looks like:
#ifndef TIMER_H #define TIMER_H class Timer { private: static LARGE_INTEGER m_lTicksPerSecond; static LARGE_INTEGER m_lTime; static LARGE_INTEGER m_lFirstTime; Timer(); public: static bool Initialize() { if(QueryPerformanceFrequency(&m_lTicksPerSecond) && QueryPerformanceCounter(&m_lFirstTime)) return true; return false; } static double MsSinceStart() { QueryPerformanceCounter(&m_lTime); return ((double)(m_lTime.QuadPart - m_lFirstTime.QuadPart ) / (double)m_lTicksPerSecond.QuadPart ) * 1000.0; } }; #endif
Or you can check it out at http://www.rafb.net/paste/results/SD400728.html (don't know if codeproject supports the formating). Anyway, the error I'm geting is: error LNK2001: unresolved external symbol "private: static union _LARGE_INTEGER Timer::m_lFirstTime" (?m_lFirstTime@Timer@@0T_LARGE_INTEGER@@A) on every member. The program it self is a win32 application. Grateful for any help ;) -
I'm having some trouble with static members in my program. This is what my code looks like:
#ifndef TIMER_H #define TIMER_H class Timer { private: static LARGE_INTEGER m_lTicksPerSecond; static LARGE_INTEGER m_lTime; static LARGE_INTEGER m_lFirstTime; Timer(); public: static bool Initialize() { if(QueryPerformanceFrequency(&m_lTicksPerSecond) && QueryPerformanceCounter(&m_lFirstTime)) return true; return false; } static double MsSinceStart() { QueryPerformanceCounter(&m_lTime); return ((double)(m_lTime.QuadPart - m_lFirstTime.QuadPart ) / (double)m_lTicksPerSecond.QuadPart ) * 1000.0; } }; #endif
Or you can check it out at http://www.rafb.net/paste/results/SD400728.html (don't know if codeproject supports the formating). Anyway, the error I'm geting is: error LNK2001: unresolved external symbol "private: static union _LARGE_INTEGER Timer::m_lFirstTime" (?m_lFirstTime@Timer@@0T_LARGE_INTEGER@@A) on every member. The program it self is a win32 application. Grateful for any help ;)You should define all static members in the timer.cpp file, e.g.
LARGE_INTEGER Timer::m_lTime;
. Robert-Antonio -
I'm having some trouble with static members in my program. This is what my code looks like:
#ifndef TIMER_H #define TIMER_H class Timer { private: static LARGE_INTEGER m_lTicksPerSecond; static LARGE_INTEGER m_lTime; static LARGE_INTEGER m_lFirstTime; Timer(); public: static bool Initialize() { if(QueryPerformanceFrequency(&m_lTicksPerSecond) && QueryPerformanceCounter(&m_lFirstTime)) return true; return false; } static double MsSinceStart() { QueryPerformanceCounter(&m_lTime); return ((double)(m_lTime.QuadPart - m_lFirstTime.QuadPart ) / (double)m_lTicksPerSecond.QuadPart ) * 1000.0; } }; #endif
Or you can check it out at http://www.rafb.net/paste/results/SD400728.html (don't know if codeproject supports the formating). Anyway, the error I'm geting is: error LNK2001: unresolved external symbol "private: static union _LARGE_INTEGER Timer::m_lFirstTime" (?m_lFirstTime@Timer@@0T_LARGE_INTEGER@@A) on every member. The program it self is a win32 application. Grateful for any help ;)Did you define m_lTicksPerSecond, m_lTime,m_lFirstTime and in your cpp file.
// In Timer.cpp
LARGE_INTEGER Timer::m_lTicksPerSecond=0;
LARGE_INTEGER Timer::m_lTime=0;
LARGE_INTEGER Timer::m_lFirstTime=0;Also have you checked out this: http://www.naughter.com/cputicker.html[^] Removing the mfc part only takes a few seconds... John
-
You should define all static members in the timer.cpp file, e.g.
LARGE_INTEGER Timer::m_lTime;
. Robert-AntonioI'm sorry but I can't get it to work :/ When I place LARGE_INTEGER Timer::m_lTime etc inside my .cpp file it begins to talk about redefinition errors. If I remove LARGE_INTEGER m_lTime etc from my .h file it says 'm_lTime' : is not a member of 'Timer'. could you write just a simple class with a static member explaining it to me? :)
-
Did you define m_lTicksPerSecond, m_lTime,m_lFirstTime and in your cpp file.
// In Timer.cpp
LARGE_INTEGER Timer::m_lTicksPerSecond=0;
LARGE_INTEGER Timer::m_lTime=0;
LARGE_INTEGER Timer::m_lFirstTime=0;Also have you checked out this: http://www.naughter.com/cputicker.html[^] Removing the mfc part only takes a few seconds... John
oki, I checked out the cputicker class but still can't figure out what I'm doing wrong, perhaps I'm geting blind to my code. This is what my 2 files looks like now: Timer.h
#ifndef TIMER_H #define TIMER_H class Timer { protected: static LARGE_INTEGER m_lTicksPerSecond; static LARGE_INTEGER m_lTime; static LARGE_INTEGER m_lFirstTime; Timer(); public: static bool Initialize(); static double MsSinceStart(); }; #endif
and this is what Timer.cpp looks like:#include "Timer.h" LARGE_INTEGER Timer::m_lTicksPerSecond = NULL; LARGE_INTEGER Timer::m_lFirstTime = NULL; LARGE_INTEGER Timer::m_lTime = NULL; bool Timer::Initialize() { if(QueryPerformanceFrequency(&m_lTicksPerSecond) && QueryPerformanceCounter(&m_lFirstTime)) return true; return false; } double Timer::MsSinceStart() { QueryPerformanceCounter(&m_lTime); return ((double)( m_lTime.QuadPart - m_lFirstTime.QuadPart ) / (double) m_lTicksPerSecond.QuadPart ) * 1000.0; }
and this is the error which seems relevant: error C2039: 'm_lFirstTime' : is not a member of 'Timer' (which it as you see is) hehe, plz bare with me ;P -
oki, I checked out the cputicker class but still can't figure out what I'm doing wrong, perhaps I'm geting blind to my code. This is what my 2 files looks like now: Timer.h
#ifndef TIMER_H #define TIMER_H class Timer { protected: static LARGE_INTEGER m_lTicksPerSecond; static LARGE_INTEGER m_lTime; static LARGE_INTEGER m_lFirstTime; Timer(); public: static bool Initialize(); static double MsSinceStart(); }; #endif
and this is what Timer.cpp looks like:#include "Timer.h" LARGE_INTEGER Timer::m_lTicksPerSecond = NULL; LARGE_INTEGER Timer::m_lFirstTime = NULL; LARGE_INTEGER Timer::m_lTime = NULL; bool Timer::Initialize() { if(QueryPerformanceFrequency(&m_lTicksPerSecond) && QueryPerformanceCounter(&m_lFirstTime)) return true; return false; } double Timer::MsSinceStart() { QueryPerformanceCounter(&m_lTime); return ((double)( m_lTime.QuadPart - m_lFirstTime.QuadPart ) / (double) m_lTicksPerSecond.QuadPart ) * 1000.0; }
and this is the error which seems relevant: error C2039: 'm_lFirstTime' : is not a member of 'Timer' (which it as you see is) hehe, plz bare with me ;PI am not sure what the problem is. Check to make sure that your cpp file is indeed included in your project and it is compiling... As for the link, since I saw you were trying to do some work with high resolution timers I thought the code there may save you some work... John
-
oki, I checked out the cputicker class but still can't figure out what I'm doing wrong, perhaps I'm geting blind to my code. This is what my 2 files looks like now: Timer.h
#ifndef TIMER_H #define TIMER_H class Timer { protected: static LARGE_INTEGER m_lTicksPerSecond; static LARGE_INTEGER m_lTime; static LARGE_INTEGER m_lFirstTime; Timer(); public: static bool Initialize(); static double MsSinceStart(); }; #endif
and this is what Timer.cpp looks like:#include "Timer.h" LARGE_INTEGER Timer::m_lTicksPerSecond = NULL; LARGE_INTEGER Timer::m_lFirstTime = NULL; LARGE_INTEGER Timer::m_lTime = NULL; bool Timer::Initialize() { if(QueryPerformanceFrequency(&m_lTicksPerSecond) && QueryPerformanceCounter(&m_lFirstTime)) return true; return false; } double Timer::MsSinceStart() { QueryPerformanceCounter(&m_lTime); return ((double)( m_lTime.QuadPart - m_lFirstTime.QuadPart ) / (double) m_lTicksPerSecond.QuadPart ) * 1000.0; }
and this is the error which seems relevant: error C2039: 'm_lFirstTime' : is not a member of 'Timer' (which it as you see is) hehe, plz bare with me ;PHow is
LARGE_INTEGER
defined? Aren't there any macros that interfere with your variable names? The simple example of declaring static members is: pruba.h:class A
{
static int b;
};pruba.cpp:
#include "pruba.h"
int A::b = 1;
Robert-Antonio "Science is a differerntial equation. Religion is a boundary condition."