0

I have the problem with precompiled header. It looks somewhat like that

ftpch.h


#pragma once
#include <vector>
#include <iostream>
#include <string>

#include <Windows.h>

ftpch.cpp

#include "ftpch.h"

Then I have a header file and cpp in my dll library. test.h

#pragma once

// DLL_EXPORT is a macro that changes for dll and console app like that:
// __declspec(dllexport) and __declspec(dllimport)

class DLL_EXPORT Test
{
    std::string foo() {return "ara ara"};
}

And this code compiles fine when I compile my dynamic library project, but fails to compile when I include "test.h" in my console app project and try to compile it. The error I get is:

C2039: 'string' is not a member of 'std'

Marcin Poloczek
  • 923
  • 6
  • 21
  • 1
    Header order could be relevant, so you should show the console app source file. But in any case, headers should be self-contained; your `test.h` should not rely on anything that came before it to have included ``. – Sebastian Redl Sep 21 '19 at 15:05
  • Your console project does not use the same precompiled header file. It has to `#include ` by itself to make that test.h file work. – Hans Passant Sep 21 '19 at 16:28

1 Answers1

0

Your header files should always be self-sufficient. Include your libraries (in this case <string>) where you need them, everywhere you need them, and only where you need them.

If your header requires a certain library to function, include it in that file; don't reply on a different header to have included that library already, because if that different file changes, you're out of luck.

You've already got #include guards through #pragma once, so adding #include <string.h> to the header files that need it won't cause collision, and will also make them more maintainable and easy to interpret.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37