使用C ++的PImpl:为什么代码不起作用?

问题描述

我已经通过在#include "stdafx.h"之前而不是之后放置#include "PImpltest.h"(此问题在原始问题中遗漏了,很抱歉)解决了这个问题。

但是我仍然感到困惑,为什么它不能紧随其后。

// stdafx.h
#include <tinyxml2.h>
#include <queue>
#include <map>
#include <vector>
#include <list>
#include <set>
#include <stack>
#include <string>
#include <memory>
#include <assert.h>

-----------------------原始问题--------------------- -----------

我正在尝试在C ++中使用PImpl模式。头文件是:

// PImpltest.h

#pragma once
#include <memory>

class PImpl
{
private:
    class Impl;
    std::unique_ptr<Impl> m_impl;
};

如果我在标头的相应cpp文件中实现Impl类,则使我感到困惑:

// 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在上面的代码注释中报告了2个错误。但是,如果在这样的文件中实现它:

// main.cpp
#include "PImpltest.h"

class PImpl::Impl
{ 
};

int main()
{
    return 0;
}

那就没事了。

出什么问题了?如何解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...