c++ - 命名空间中的类前向声明

标签 c++ namespaces forward-declaration

#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;
    };
};

好的,这是代码,

我收到一条错误消息:

错误 3 错误 C2228:'.version' 的左边必须有类/结构/union

我正在使用 visual studio 2012

谁能告诉我为什么会出现这个错误

最佳答案

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

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

关于c++ - 命名空间中的类前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19647103/

相关文章:

c++ - 我可以将 std::atomic<int64> 放置在共享内存中并期望原子操作吗?

c++ - 使用 cpp WxWidgets 写入文件

c++ - 如果覆盖 char 数组的边界,为什么 strcat_s 会崩溃?

PHP SOAP 请求,删除 ns1 : et ns:2 from request

javascript - Vanilla JavaScript 中的事件处理程序命名空间

c++ - MySQL 连接器/C++ PreparedStatement : forward declaration of ‘class sql::PreparedStatement’

64位的c++指针赋值

c++ - 传参时类类型转换 : why typecasting function is not called

c# - 如何在 Python 中从 C# dll 导入子模块

c++ - 我收到 [Error] 'Eintrag' does not name a type 我不知道为什么