静态类成员显示令人困惑的语法错误

时间:2016-02-14 18:31:20

标签: c++ compiler-errors

这是我的项目设置:

Main.hpp

ctr

Main.cpp的

#pragma once
#ifndef MAIN_HPP
#define MAIN_HPP

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <stdio.h>
#include <stdarg.h>

#include <iostream>
#include <string>
#include <map>

#include "Util/Cache.hpp"

#include "SampInternal.hpp"

BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved);

#endif

SampInternal.hpp(编辑前的Foo.hpp)

#include "Main.hpp"

BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
    DisableThreadLibraryCalls(hModule);

    if (dwReason != DLL_PROCESS_ATTACH)
        return FALSE;

    return (CreateThread(NULL, NULL, &SampInternal::Initialize, NULL, NULL, NULL) > 0);
}

SampInternal.cpp

#pragma once
#ifndef SAMPINTERNAL_HPP
#define SAMPINTERNAL_HPP

#include "Main.hpp"

#define D3D9_NAME                           "d3d9"
#define SAMP_NAME                           "samp"

#define D3D9_DLL                            "d3d9.dll"
#define SAMP_DLL                            "samp.dll"

class SampInternal
{

public:

    static DWORD APIENTRY Initialize(LPVOID lpArgs);

private:

    static Cache<HMODULE> s_moduleCache;

};

#endif

Cache.hpp

#include "SampInternal.hpp"

Cache<HMODULE> SampInternal::s_moduleCache;

DWORD APIENTRY SampInternal::Initialize(LPVOID lpArgs)
{

    return 0;
}

(Cache.cpp为空 - 仅#pragma once #ifndef CACHE_HPP #define CACHE_HPP #include "../Main.hpp" template<typename T> class Cache { public: void Add(std::string name, T value); T Get(std::string name); private: std::map<std::string, T> m_storage;; }; template<typename T> void Cache<T>::Add(std::string name, T value) { m_storage[name] = value; } template<typename T> T Cache<T>::Get(std::string name) { if (m_storage.find(name) != m_storage.end()) return m_storage[name]; return nullptr; } #endif

我是C ++的初学者,但我在这里看不到任何错误。 编译器(使用Visual Studio 2015)输出我:

#include "Cache.hpp" Syntax Error: Missing ';' before '<'

这两行Unexpected token before ';'

很抱歉,如果这看起来很微不足道,但我不知道这里有什么问题。

1 个答案:

答案 0 :(得分:3)

这似乎是一个正常的循环包含依赖问题:Cache.hpp需要Main.hpp,需要SampInternal.hpp需要Cache.hpp ...等等。

打破此圈子的最简单方法(据我所见)是Main.hpp文件中不包含Cache.hpp。在Cache.hpp文件中包含明确需要的文件,例如<map><string>