0
//file.h
# define PartExport __declspec(dllexport)
namespace Part{

class PartExport MyClass : public data::anotherClass{
  MyClass();
  void read();
};
}

I want to access this function by doing this below. Visual studio suggests to do "Part::read();" and f12 to that function works.

//main.cpp
#include <file.h>

int main(){

   Part::read();
   return 0;
}

But when compiling it complains about syntax errors because it thinks that PartExport is the class name. How can I access this function or create an object of MyClass?

edit: I realized that all the syntax errors on the class comes from the #include . I dont know what that means

Rasmus
  • 65
  • 2
  • 11

2 Answers2

2

Your class MyClass is exported, hence you should write in your main :

Part::MyClass myClass;
myClass.read();

If you want to call your function as you actually do in your main, you should write in your file.h :

namespace Part{

  void PartExport read();

}

But in this case you will lose your class encapsulation.


Another thing : to create your dll you have to specify the __declspec(dllexport) to export the function in the library.

But when your are using it, you should not tell your executable that you want to export this function as it was already exported in your library.

I advise you to compile your dll defining this macro in your project : PART_EXPORT_DLL

Then write it like this in your file.h:

//file.h
#ifdef PART_EXPORT_DLL
#    define PartExport __declspec(dllexport)
#else
#    define PartExport __declspec(dllimport)
#endif
namespace Part{

    class PartExport MyClass : public data::anotherClass{
      MyClass();
      void read();
    };
}

And when you want to import it, be sure not to define PART_EXPORT_DLL

dkg
  • 1,775
  • 14
  • 34
  • This gives me syntax errors on "class PartExport MyClass : public data::anotherClass{". Syntax error: ':', Syntax error: public, Syntax error: missing ';' before '{', Syntax error: missing ';' before ':'. And an error that says that MyClass uses an undefined class 'Part::PartExport' – Rasmus Nov 12 '15 at 09:47
  • That macro is already in there, sorry for leaving that out. All the compilation problems are there because of the #include what could that mean? – Rasmus Nov 12 '15 at 10:11
  • When the pre-processor encounter an `#include "file.h"` instruction, you can consider that it makes a copy/paste of `file.h` in your actual file so he the compiler will have all the definitions it needs to compile your file. Then, if you include exports instructions whereas you want to import dll exported functions, you will have some trouble ... – dkg Nov 12 '15 at 10:18
1

What you want to access is NOT a function, but a member-function. As such you have to specify which class it is a member of.

And as you haven't declared it static which would have made it similiar to a function, you even have to instantiate one of your MyClass before you can access it. (A non-static function has a 'this'-pointer, without an instance of your class there would be no 'this')

The namespace 'Part' is just another wrapper around your class.

To Access something in a namespace you do something like this:

Part::somethinginmynamespace

To call a function in a namespace you do

Part::somefunction();

To access a static memberfunction you use

Part::SomeClass::SomeStaticFunction();

To access a non-static member function you do:

Part::Someclass myinstance;
myInstance.myFunction();

If you get errors on a define, and the compiler thinks it's a classname, then the define is undefined. Maybe you put it in a comment, or the upper/lowercase is wrong.

A.Franzen
  • 725
  • 4
  • 10
  • I wanted to do like dkg said first. But since it wasnt working and visual studio suggested to go direct to the function I thought I could try that out. It is not recognizing MyClass as a class. It suggests "Part::PartExport" as the class in the dropdown meny from my Main – Rasmus Nov 12 '15 at 09:59
  • You shouldn't completely rely on IntelliSense, that is sometimes badly broken. – A.Franzen Nov 12 '15 at 10:19