我在頭文件中定義了一個結構體。然後我有一個單身類,我試圖使用該結構。當我從另一個類中調用ResetVars()時,當它碰到test.numResponses =「TEST」行時,我得到一個訪問衝突。我認爲這與初始化有關,但我一直無法解決它。我是新來的C++,我不知道如何解決這個問題。謝謝你的幫助。寫入到typdef時出現C++訪問衝突struct
struct.h
typedef struct POLL_DATA
{
std::string numResponses;
std::string type;
std::string question;
} POLL_DATA;
ControlPolls.h
class ControlPolls
{
private:
static bool instanceFlag;
static ControlExitPolls *controlSingle;
ControlExitPolls();
POLL_DATA test;
public:
static ControlExitPolls* getInstance();
void ResetVars();
};
ControlPolls.cpp
#include "ControlPolls.h"
bool ControlPolls::instanceFlag = false;
ControlPolls* ControlPolls::controlSingle = NULL;
//Private Constructor
ControlExitPolls::ControlExitPolls()
{
};
//Get instance
ControlPolls* ControlPolls::getInstance()
{
if(!instanceFlag)
{
controlSingle = &ControlPolls();
instanceFlag = true;
return controlSingle;
}
else
{
return controlSingle;
}
}
void ControlExitPolls::ResetVars()
{
test.numResponses = "TEST";
}
callingClass.cpp
ControlPolls *controlSingleton;
controlSingleton = ControlPolls::getInstance();
controlSingleton->getInstance()->ResetVars();
請編輯您的問題包含一個[MCVE] – Slava
爲什麼你需要'的typedef struct'在C++擺在首位? – Slava
class A.h include struct.h – baruti