declspec making problem with class constructor

In my game engine, before I updated my code I had no problem and my classes looked like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef LIGHTENGINE_EXPORTS
#define LIGHTENGINE_API __declspec(dllexport)
#else
#define LIGHTENGINE_API __declspec(dllimport)
#endif

	struct LIGHTENGINE_API Listener {

		std::string name;

		listenerFunction function;

		Listener(std::string name_, listenerFunction function_) : name(name_), function(function_) {}

	};


And everything was fine until I added a lot of new features and when I tried to build my dll I got bunch of errors saying something like unresolved external symbol __declspec(import) and next to that would be class and it's constructor. So I tried many things to fix that, and I removed that error by doing this:

1
2
3
4
5
6
7
8
9
	struct Listener {

		std::string name;

		listenerFunction function;

		LIGHTENGINE_API Listener(std::string name_, listenerFunction function_) : name(name_), function(function_) {}

	};


I removed LIGHTENGINE_API from class and placed it next to constructor. Why is this happening? Why I can't just simply place that on class instead of constructor like before? Thanks.
Last edited on
It's difficult to explain what exactly happened without seeing the error messages. However, you shouldn't make data public in interfaces that you implement in a DLL.

The compiler was probably warning you about the complexities of managing those public inlined members.
Errors: http://imgur.com/a/WuEdD
I'm sorry, but I don't understand. Why is it bad to have public memebers of an struct? And what should I do about it? Thanks :).
Those warnings are about the default methods that C++ generates for each class/struct. In this case, it must be generating external references to destructors and move constructors. At any rate, it can't find them.

You're supposed to create classes and export them. You should only access them thru their methods and never access data directly. If nothing else, it's just good practice.

These methods should include implementing the default methods that C++ classes are expected to have.
Last edited on
Thanks. But for example, SFML (library for opengl) has Vector2f class which has public float members x and y.

And Listener class is used only by engine not by user of api, I think it's not worth it to write the whole function just to return variable for engine. I'm completely self taught programmer and don't know anything about code design.
You have a struct in a DLL that has public member objects.

The compiler cannot trivially create default metods for these as that depends on the member objects behaviours. So your example is less like a C struct and more like a badly implemented class.

When you use FILE* you never directly access the members of FILE. You always use a method that takes a FILE*. That's a well known example that's different from what you're trying to do.

If all this is a problem for you, use a static library instead.
Last edited on
Topic archived. No new replies allowed.