#include <string> #include <iostream> #include "rapidjson/document.h" class MyClass { private: static std::string mVin_; public: static const std::string& GetVin(); static void InitializeVin(const std::string& jsonString); }; std::string MyClass::mVin_ = ""; const std::string& MyClass::GetVin() { return mVin_; } void MyClass::InitializeVin(const std::string& jsonString) { rapidjson::Document document; document.Parse(jsonString.c_str()); if (!document.HasMember("vin")) { std::cerr << "JSON does not contain 'vin' field." << std::endl; return; } mVin_ = document["vin"].GetString(); } int main() { MyClass::InitializeVin("{\"vin\": \"123\"}"); std::cout << MyClass::GetVin() << std::endl; return 0; }分成.h和.cpp文件写
时间: 2024-03-27 13:37:15 浏览: 70
#include <iostream> #include<iomanip> using namespace std;九九乘法表
MyClass.h:
```cpp
#pragma once
#include <string>
class MyClass {
private:
static std::string mVin_;
public:
static const std::string& GetVin();
static void InitializeVin(const std::string& jsonString);
};
```
MyClass.cpp:
```cpp
#include "MyClass.h"
#include <iostream>
#include "rapidjson/document.h"
std::string MyClass::mVin_ = "";
const std::string& MyClass::GetVin() {
return mVin_;
}
void MyClass::InitializeVin(const std::string& jsonString) {
rapidjson::Document document;
document.Parse(jsonString.c_str());
if (!document.HasMember("vin")) {
std::cerr << "JSON does not contain 'vin' field." << std::endl;
return;
}
mVin_ = document["vin"].GetString();
}
```
main.cpp:
```cpp
#include "MyClass.h"
#include <iostream>
int main() {
MyClass::InitializeVin("{\"vin\": \"123\"}");
std::cout << MyClass::GetVin() << std::endl;
return 0;
}
```
阅读全文