gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:36:35 24 4
gpt4 key购买 nike

这是我的项目设置:

主.hpp

#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

main.cpp

#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.hpp(编辑前的 Foo.hpp)

#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

SampInternal.cpp

#include "SampInternal.hpp"

Cache<HMODULE> SampInternal::s_moduleCache;

DWORD APIENTRY SampInternal::Initialize(LPVOID lpArgs)
{

return 0;
}

缓存.hpp

#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

(Cache.cpp 为空 - 只有 #include "Cache.hpp" )

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

Syntax Error: Missing ';' before '<' Unexpected token before ';'

既上线static Cache<HMODULE> s_moduleCache;

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

最佳答案

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

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

关于c++ - 静态类成员显示令人困惑的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com