C++ Class definition syntax

一笑奈何 提交于 2021-01-29 08:55:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!