Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 命名空间中的类转发声明 #包括 #包括 #包括 使用std::string; 使用std::vector; 使用std::endl; 使用std::cout; 名称空间啊 { 类消息模板; }; 使用名称空间AAH; int main() { messageTemplate templateMSG32(“hello world”); cout_C++_Namespaces_Forward Declaration - Fatal编程技术网

C++ 命名空间中的类转发声明 #包括 #包括 #包括 使用std::string; 使用std::vector; 使用std::endl; 使用std::cout; 名称空间啊 { 类消息模板; }; 使用名称空间AAH; int main() { messageTemplate templateMSG32(“hello world”); cout

C++ 命名空间中的类转发声明 #包括 #包括 #包括 使用std::string; 使用std::vector; 使用std::endl; 使用std::cout; 名称空间啊 { 类消息模板; }; 使用名称空间AAH; int main() { messageTemplate templateMSG32(“hello world”); cout,c++,namespaces,forward-declaration,C++,Namespaces,Forward Declaration,如果不声明对象的原型,就不能使用对象。前向声明只适合于在不包含和添加大量依赖项的情况下声明指针或引用 在头文件中,您不需要许多不必要的包含。许多包含可能会导致较长的编译时间。因此,当您只需要在类或函数原型中声明指针或引用时,最好使用前向声明而不是包含 请看示例: 文件A.h #include <iostream> #include <string> #include <vector> using std::string; using std::vector;

如果不声明对象的原型,就不能使用对象。前向声明只适合于在不包含和添加大量依赖项的情况下声明指针或引用

在头文件中,您不需要许多不必要的包含。许多包含可能会导致较长的编译时间。因此,当您只需要在类或函数原型中声明指针或引用时,最好使用前向声明而不是包含

请看示例:

文件A.h

#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;
using std::endl;
using std::cout;

namespace AAH 
{
    class messageTemplate;
};

using namespace AAH;

int main()
{
    messageTemplate templateMSG32("hello world");
    cout << templateMSG32.version << endl;
    return EXIT_SUCCESS;
}

namespace AAH {    
    class messageTemplate 
    {
    public:
        messageTemplate() : version("XX.XX.XX.001") {}
        messageTemplate(string ver) : version(ver) {}
        string version;
    };
};
class A{
public:
    void func() const;
};
文件B.h

#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;
using std::endl;
using std::cout;

namespace AAH 
{
    class messageTemplate;
};

using namespace AAH;

int main()
{
    messageTemplate templateMSG32("hello world");
    cout << templateMSG32.version << endl;
    return EXIT_SUCCESS;
}

namespace AAH {    
    class messageTemplate 
    {
    public:
        messageTemplate() : version("XX.XX.XX.001") {}
        messageTemplate(string ver) : version(ver) {}
        string version;
    };
};
class A{
public:
    void func() const;
};
文件B.cpp

//fwd declaration instead of include
class A;

class B{
public:
  //use fwd decl.
  B(const A& a);
};

如果未声明对象的原型,则无法使用该对象。正向声明仅适用于在不包含和添加大量依赖项的情况下声明指针或引用

在头文件中,您不需要许多不必要的包含。许多包含可能会导致较长的编译时间。因此,当您只需要在类或函数原型中声明指针或引用时,最好使用前向声明而不是包含

请看示例:

文件A.h

#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;
using std::endl;
using std::cout;

namespace AAH 
{
    class messageTemplate;
};

using namespace AAH;

int main()
{
    messageTemplate templateMSG32("hello world");
    cout << templateMSG32.version << endl;
    return EXIT_SUCCESS;
}

namespace AAH {    
    class messageTemplate 
    {
    public:
        messageTemplate() : version("XX.XX.XX.001") {}
        messageTemplate(string ver) : version(ver) {}
        string version;
    };
};
class A{
public:
    void func() const;
};
文件B.h

#include <iostream>
#include <string>
#include <vector>

using std::string;
using std::vector;
using std::endl;
using std::cout;

namespace AAH 
{
    class messageTemplate;
};

using namespace AAH;

int main()
{
    messageTemplate templateMSG32("hello world");
    cout << templateMSG32.version << endl;
    return EXIT_SUCCESS;
}

namespace AAH {    
    class messageTemplate 
    {
    public:
        messageTemplate() : version("XX.XX.XX.001") {}
        messageTemplate(string ver) : version(ver) {}
        string version;
    };
};
class A{
public:
    void func() const;
};
文件B.cpp

//fwd declaration instead of include
class A;

class B{
public:
  //use fwd decl.
  B(const A& a);
};

如上所述,只让我们声明指针或引用


在您的示例中,技术上不需要前向声明,因为您可以在main函数之前声明AAH类。

如上所述,前向声明只允许声明指针或引用


在您的示例中,技术上不需要前向声明,因为您只需在主函数之前声明AAH类。

如果未声明对象的原型,则无法使用对象。前向声明仅适用于声明指针或引用。我认为前向声明类似于函数原型思考应该如何使用它
sizeof
可以工作。它不能工作。如果不声明对象的原型,就不能使用对象。前向声明只允许声明指针或引用。我认为前向声明就像函数原型思考
sizeof
应该如何工作。它不能工作。谢谢,我认为这是怎么回事,但我从未使用过在我使用类的任何地方都要使用包含:D谢谢你的许可谢谢,我想是怎么回事,但我以前从未使用过包含,在我使用类的任何地方都要使用包含:D谢谢你的许可