在获取当前进程内存使用情况的时候,出现了重定义的问题,但是源代码应该非常复杂,不希望改动源代码,在百般尝试后,最终用namespace命名空间解决了这个问题。
这个INT命名在psapi.h中已经定义,这里再在utility.h中重命名就出现了这个问题。
解决方法,把psapi.h部分放在命名空间中:
//interface.h
#ifndef INterface_h
#define INterface_h
//命名空间
namespace Memory{
#include <windows.h>
#include <psapi.h>
#include <array>
#include <iostream>
#include <vector>
using namespace std;
void printMemoryUsed() {
HANDLE processHandle = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS pmc;
if (GetProcessMemoryInfo(processHandle, (PROCESS_MEMORY_COUNTERS*)&pmc,
sizeof(pmc))) {
std::cout << "Memory used " << pmc.WorkingSetSize/1024 << "KB" << std::endl;
}
}
}
#endif
//main.cpp
#include"interface.h"
int main() {
// printMemoryUsed();
//加命名空间
Memory::printMemoryUsed();
return 0;
}
ok!