C++类定义语法

C++ Class definition syntax

本文关键字:语法 定义 C++      更新时间:2023-10-16

当我尝试编译以下代码时,出现错误:

"include/IBCppClient/client/SoftDollarTier.h|3|error: variable 'TWSAPIDLLEXP SoftDollarTier' 具有初始值设定项但不完整的类型|"。

我想代码是正确的,因为它是股票经纪人 API 的一部分。但我很难理解关于类定义的部分"class TWSAPIDLLEXP SoftDollarTier".据我所知,这种语法在C++是不合法的。我错过了什么?

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;
};

C++允许宏定义,这允许 C 预处理器基本上查找这些宏并将其替换为更复杂的定义。C/C++ 中的常见做法是使用预处理器宏在其名称之前插入函数属性,可能发生的情况是您的环境不包含定义TWSAPIDLLEXP的头文件,因此编译器将字符串解释为类名。

从快速搜索来看,似乎TWSAPIDLLEXP在tws-api中定义为:

#define TWSAPIDLLEXP __declspec(dllimport)

IBJts/samples/Cpp/TestCppClient/StdAfx.h.