本網站和 C 的新手,但希望看到大家的一些指導。
我有一個非常有趣的專案想法來學習 C 深入挖掘 API、類、參考等。目前我有一個代碼的作業示例,其中所有內容都存在于 main.cpp 檔案中。我面臨的問題是,當我將類(內部和外部)移動到它們各自的頭檔案時,代碼不再編譯。
嵌套類的原因是 OuterAPI 作為 API 的主要入口點,并且有許多可以在其下訪問的較低級別的 API(人員、許可證、角色等)。通過這種方式,API 的用戶只需為 OuterAPI 創建一個物件,然后為底層資源和方法創建點符號。
這是 main.cpp 中的作業示例
#include <iostream>
#include <nlohmann/json.hpp>
#include <cpr/cpr.h>
using json = nlohmann::json;
class OuterAPI {
private:
class InnerAPI {
private:
OuterAPI& api;
public:
InnerAPI(OuterAPI& a) :api(a) {}
json get() {
cpr::Response r = cpr::Get(
cpr::Url{ api.baseUrl "resource" },
cpr::Bearer{ api.token }
);
return json::parse(r.text)
};
std::string token;
std::string baseUrl = "";
public:
InnerAPI people;
OuterAPI(std::string t) : token(t), people(*this) {}
};
int main(int argc, char** argv)
{
std::string token = "";
OuterAPI api(token);
json jsonData = api.people.get();
std::cout << jsonData.dump(4) << std::endl;
return 0;
}
這是我將所有內容移動到相應的頭檔案/cpp 檔案
外部API.h
#pragma once
class OuterAPI {
private:
class InnerAPI;
std::string token;
std::string baseUrl = "";
public:
OuterAPI(std::string t);
~OuterAPI();
InnerAPI* people;
};
外部API.cpp
#include "WebexAPI.h"
#include "PeopleAPI.h"
OuterAPI::OuterAPI(std::string t) : token(t) {
people = new InnerAPI(*this);
}
OuterAPI::~OuterAPI() { delete people; }
InnerAPI.h
#pragma once
#include <nlohmann/json.hpp>
#include <cpr/cpr.h>
#include "OuterAPI.h"
using json = nlohmann::json;
class OuterAPI::InnerAPI {
private:
OuterAPI& api;
public:
InnerAPI(OuterAPI& a);
json get();
};
InnerAPI.cpp
#include "InnerAPI.h"
OuterAPI::InnerAPI::InnerAPI(OuterAPI& a) : api(a) {}
json OuterAPI::InnerAPI::get() {
cpr::Response r = cpr::Get(
cpr::Url{ api.baseUrl "resource" },
cpr::Bearer{ api.token }
);
return json::parse(r.text);
main.cpp (finally) - this is where the compiler error occurs at api.people.get() "expression must have class type but has type "OuterAPI::InnerAPI *"
int main(int argc, char** argv)
{
std::string token = "";
OuterAPI api(token);
json jsonData = api.people.get(); // COMPILER ERROR "expression must have class type but has type "OuterAPI::InnerAPI *"
std::cout << jsonData.dump(4) << std::endl;
return 0;
}
From this I believe the issue is associated with me having to define the InnerAPI object people as a pointer inside of OuterAPI but from here I cant seem to come to a resolution.
Also, feel free to critique my design as well, like I say I am new to C so want to make sure I can do a good job. Thanks.
uj5u.com熱心網友回復:
在OuterAPI*您已宣告people為 type 的成員InnerAPI*。
您可以使用api.people->get()或將成員設為 aInnerAPI來呼叫您的 API 。
編輯:
除了指標之外,錯誤似乎來自您處理檔案包含的方式。我設法在 REPL.it 上獲得了一個作業版本。我做了一些細微的調整,這樣我就不必把這兩個庫都集中在它的要點上。這里是:
外部API.h
#pragma once
#include <string>
class OuterAPI {
private:
class InnerAPI;
std::string token;
std::string baseUrl = "";
public:
OuterAPI(std::string t);
~OuterAPI();
InnerAPI* people;
};
內部API.j
#pragma once
#include "./OuterAPI.h"
class OuterAPI::InnerAPI {
private:
OuterAPI& api;
public:
InnerAPI(OuterAPI& a);
std::string get();
};
外部API.cpp
#include "./OuterAPI.h"
#include "./InnerAPI.h"
OuterAPI::OuterAPI(std::string t) : token(t) {
people = new InnerAPI(*this);
}
OuterAPI::~OuterAPI() { delete people; }
內部API.cpp
#include "./OuterAPI.h"
#include "./InnerAPI.h"
OuterAPI::InnerAPI::InnerAPI(OuterAPI& a) : api(a) {}
std::string OuterAPI::InnerAPI::get() {
return api.baseUrl "resource";
}
uj5u.com熱心網友回復:
確保在每個檔案中包含您打算使用的所有內容。
分離宣告和定義是很常見的。
這是一種減少大型專案編譯時間的方法。
值得慶幸的是,模塊很快就會? 使鏈接成為過去。
要解決錯誤訊息:您宣告people為類的原始成員指標OuterAPI……您無法通過指標訪問成員 using operator .,您需要使用operator ->。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360389.html
