Archived

This topic is now archived and is closed to further replies.

DLL trouble w/ static class variables

This topic is 5180 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I was experimenting with the use of DLL''s in my programs but I''m still kind of new to it. During my experiments I ran into a slight problem. It seems that whenever I try to access a static class variable the linker always complains about that variable being unresolved. Here''s how my test code looks like: test.cpp:
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include "testqueueclass.h"

#pragma link "testqueueclass.lib"

const unsigned int MAX_TIMES=50;

int main()
{
	TestQueueClass TheTest(0,0);

	while(!kbhit())
	{
		for(int i=0;i<MAX_TIMES;i++)
		{
			TheTest.PrintCoord();
			cout<<"Pushing Idex..."<<endl;
			TheTest.PushIndex(rand()%10);
			if(i%2==1)
				cout<<"value of Index popped is: "<<TheTest.PopIndex()<<endl;
		}//for

		Sleep(100);
	}//while


	//this line is causing the trouble

	cout<<"TestQueueClass countme is "<<TheTest.countme<<endl;

	return 0;
}//main

TestQueueClass.h:
//File: testqueueClass.h - Interface

#ifndef _TESTQUEUECLASS_H
#define _TESTQUEUECLASS_H

#include <iostream.h>
#include <apqueue.h>

#ifndef DLL_API
#define DLL_API __declspec(dllimport)
#endif

class TestQueueClass
{
	public:
		TestQueueClass(const int x=0,const int y=0);
		~TestQueueClass();
		void PushIndex(const int i);
		int PopIndex();
		void PrintCoord();
		static unsigned int countme;

	private:
		apqueue<int> indexQ;
		int m_x;
		int m_y;
};//TestQueueClass


#endif
TestQueueClass.cpp:
//File: testqueueClass.cpp - Implementation

#ifndef DLL_API
#define DLL_API __declspec(dllexport)
#endif

#include "testqueueclass.h"

DLL_API unsigned int TestQueueClass::countme=0;

DLL_API TestQueueClass::TestQueueClass(const int x,const int y):
m_x(x),m_y(y)
{
	countme++;
}//constructor


DLL_API TestQueueClass::~TestQueueClass()
{
	countme--;
}//destructor


DLL_API void TestQueueClass::PushIndex(const int i)
{
	indexQ.enqueue(i);
}//PushIndex


DLL_API int TestQueueClass::PopIndex()
{
	int num=0;

	if(!indexQ.isEmpty())
		indexQ.dequeue(num);
	return num;
}//PopIndex


DLL_API void TestQueueClass::PrintCoord()
{
	cout<<"value of x is: "<<m_x
		<<"\tvalue of y is: "<<m_y<<endl;
}//PrintCoord

The testqueueclass is the class that''s being compiled as a dll. Funny thing is if I comment out all the lines in my main that accesses the static variables then all is well and it compiles/links fine but if I uncomment it I get that unresolved error: Error: Unresolved external ''TestQueueClass::countme'' referenced from E:\BORLAND\MYWORKSPACE\TEST.OBJ Is there something about the way DLL''s are meant to be used that I''m not aware of? Thanks


--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
never mind I figured out the problem. I just had to change my testqueueclass.h header file to this:


class TestQueueClass
{
public:
DLL_API TestQueueClass(const int x=0,const int y=0);
DLL_API ~TestQueueClass();
DLL_API void PushIndex(const int i);
DLL_API int PopIndex();
DLL_API void PrintCoord();
DLL_API static unsigned int countme;

private:
apqueue<int> indexQ;
int m_x;
int m_y;
};//TestQueueClass



Edit: Hey I found a even better shortcut!


DLL_API class TestQueueClass
{
public:
TestQueueClass(const int x=0,const int y=0);
~TestQueueClass();
void PushIndex(const int i);
int PopIndex();
void PrintCoord();
static unsigned int countme;

private:
apqueue<int> indexQ;
int m_x;
int m_y;
};//TestQueueClass



I think I'm starting to get the hang of this

Edit#2: When are they gonna get that search feature fixed here? I tried doing a search on this problem first before posting and ended up with a stupid administrator error




--{You fight like a dairy farmer!}

[edited by - Greatwolf on October 10, 2003 1:31:32 AM]

[edited by - Greatwolf on October 10, 2003 1:39:45 AM]

Share this post


Link to post
Share on other sites