问题
when I tried to compile the following code I got an error:
"include/IBCppClient/client/SoftDollarTier.h|3|error: variable ‘TWSAPIDLLEXP SoftDollarTier’ has initializer but incomplete type|".
I guess the code is correct since it is part of a stock broker's API. But I struggle to understand the part on class definition "class TWSAPIDLLEXP SoftDollarTier". As far as I know, this syntax is not legal in C++. What did I miss?
class TWSAPIDLLEXP SoftDollarTier{
std::string m_name, m_val, m_displayName;
public:
SoftDollarTier(const std::string& name = "", const std::string& val = "", const std::string& displayName = "");
std::string name() const;
std::string val() const;
std::string displayName() const;
};
回答1:
C++ allows macro definitions, which allow the C preprocessor to essentially find-and-replace these macros with more elaborate definitions. A common practice in C/C++ is to insert function attributes before their names using preprocessor macros, and what likely happened is that your environment does not include the header file which defines TWSAPIDLLEXP, so the compiler interprets the string as the class name.
From a quick search, it seems that TWSAPIDLLEXP is defined in tws-api (not sure if this is the same broker) as:
#define TWSAPIDLLEXP __declspec(dllimport)
in IBJts/samples/Cpp/TestCppClient/StdAfx.h.
来源:https://stackoverflow.com/questions/59524420/c-class-definition-syntax