I've solved the problem by putting #include "stdafx.h"
(this statement is missed in the original question, sorry for that) BEFORE #include "PImplTest.h"
instead of AFTER it.
But I'm still confused why it cannot stay after it.
// stdafx.h
#include <tinyxml2.h>
#include <queue>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <string>
#include <memory>
#include <assert.h>
----------------------- the original question --------------------------------
I'm trying to use the PImpl pattern in C++. The header file is:
// PImplTest.h
#pragma once
#include <memory>
class PImpl
{
private:
class Impl;
std::unique_ptr<Impl> m_impl;
};
What confuses me is, if I implement the Impl
class in the corresponding cpp file of the header like this:
// PImplTest.cpp
#include "PImplTest.h"
#include "stdafx.h" // this statement was missed in my first post
class PImpl::Impl // error C2079 'Impl' uses undefined class 'PImpl'
{ // error C2653 'PImpl':is not a class or namespace name
};
// main.cpp
#include "stdafx.h" // this statement was missed in my first post
#include "PImplTest.h"
int main()
{
return 0;
}
VS reports the 2 errors in the code comments above. However, if it is implemented in a file like this:
// main.cpp
#include "PImplTest.h"
class PImpl::Impl
{
};
int main()
{
return 0;
}
Then nothing goes wrong.
What's the problem? How to fix it?