-1

I don't understand why the linker says it can't find this function when I add something to my class.

// In .h file
class Importer
{public:
     void importOBJFile();
}

// In .cpp file
// INCLUDE ALL THE ASSIMP LIBRARY HEADERS HERE
void Importer::importOBJFile()
{
      Assimp::Importer importerInst;
      const aiScene* scene = importerInst.ReadFile("filename", aiProcess_Triangulate);
}

Everything works fine like this. However if I add:

const class aiScene* scene = nullptr;

... to my class definition, or forward declare class aiScene; and then just add:

const aiScene* scene = nullptr;

... to my class definition, then I get an error compiling:

Error LNK2019 unresolved external symbol "public: class aiScene const

  • __cdecl Assimp::Importer::ReadFile(char const *,unsigned int)" (?ReadFile@Importer@Assimp@@QEAAPEBVaiScene@@PEBDI@Z) referenced in function "public: class aiScene const * __cdecl Assimp::Importer::ReadFile(class std::basic_string<char,struct std::char_traits,class std::allocator > const &,unsigned int)" (?ReadFile@Importer@Assimp@@QEAAPEBVaiScene@@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z)

Why is it complaining that it can't find the readFile function when I just forward declare aiScene class and add a pointer declaration to the class definition?

Pasting all the Assimp library headers in the .h file before my Importer class fixes this, but I wanted to avoid pasting headers in headers, and I can't understand why this is happening.

I am on Visual Studio 2019

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • Where does `aiScene` come from? Where is it implemented? What you mentione in your examples aren't forward declarations BTW. – πάντα ῥεῖ Sep 27 '20 at 08:23
  • @πάνταῥεῖ It's in a header, it's part of the Assimp importer library – Zebrafish Sep 27 '20 at 08:25
  • And did you link that library? – πάντα ῥεῖ Sep 27 '20 at 08:25
  • @πάνταῥεῖ Yes, otherwise it wouldn't have worked before adding that extra member to my class. It works fine until I add that extra pointer. – Zebrafish Sep 27 '20 at 08:26
  • @πάνταῥεῖ `const` **`class`** `aiScene* scene = nullptr;` is a forward declaration actually because of keyword class used. – Swift - Friday Pie Sep 27 '20 at 08:28
  • @πάνταῥεῖ Yes, I'm saying everything works fine until I either add "const class aiScene* scene = nullptr;" or forward declare "class aiScene;" and then add "const aiScene* scene = nullptr;" to the class – Zebrafish Sep 27 '20 at 08:30
  • Can a compilable example be formed (with a minimal Importer)? The situation is a nonsense, something else is wrong. Can it be that `aiScene` isn't actually fully defined name but a macro or alias, namespaces used in project, etc? – Swift - Friday Pie Sep 27 '20 at 08:32
  • @Swift Ohhh, I found the problem. The problem is that Visual Studio for some reason doesn't allow you to forward declare a class as a struct and vice versa. – Zebrafish Sep 27 '20 at 08:40
  • 1
    @Zebrafish that's standard. it is required to match otherwise it kinda leads to ODR breach. – Swift - Friday Pie Sep 27 '20 at 08:42

1 Answers1

0

I found the problem. The problem is that Visual Studio for some reason doesn't allow you to forward declare a class as a struct and vice versa. Not sure why this is the case. I asked a question about this previously, other compilers don't see class and struct as different for this purpose.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119