【发布时间】:2013-10-28 18:59:49
【问题描述】:
您好,我正在尝试在我的班级中创建一个新结构,但我认为某种公共和私有范围存在问题。
typedef struct Currency
{
Currency(Coin *coin, Currency *next, int _position) : _coin(coin), _next(next), _position(0) {}
Currency() : _next(NULL), _position(0) {}
Coin *_coin;
Currency *_next;
int _position;
};
这是我在班级公共部分中的结构 当我尝试这样做时
if(location <= exit)
{
start = location + 11;
begin = response.find("label", start);
end = begin - start - 3;
findStrings(start, end, s, &response);
curr._next = new Currency();
}
它表示 new Currency() 调用的预期类型说明符。 我有什么遗漏或者不应该以这种方式使用结构吗?
类交换 { 公开:
typedef struct Badge
{
Badge(std::string id, Badge next, Badge prev, int length) : _id(id), _next(&next), _prev(&prev), _position(length) {}
Badge() : _id(""), _next(NULL), _prev(NULL), _position(0) {}
std::string _id;
Badge *_next;
Badge *_prev;
int _position;
};
typedef struct Currency
{
Currency(Coin *coin, Currency *next, int _position) : _coin(coin), _next(next), _position(0) {}
Currency() : _next(NULL), _position(0) {}
Coin *_coin;
Currency *_next;
int _position;
};
/* constructor and destructor */
Exchange();
Exchange(std::string str);
~Exchange();
/* Assignment operator */
Exchange& operator =(const Exchange& copyExchange);
void parseTradePairs(Currency curr, const std::string response, int begin, int exit);
private:
std::string _exch;
Currency *_currencies;
Badge *_ident;
};
endif
在类头中的^
Exchange::Exchange()
{
_exch = "";
}
Exchange::Exchange(std::string str)
{
_exch = str;
_ident = new Badge;
_currencies = new Currency;
std::string pair;
std::string response;
CURL *curl;
getTradePairs(curl, response);
int exit = response.find_last_of("marketid");
parseTradePairs(*_currencies, response, 0, exit);
}
void parseTradePairs(Exchange::Currency curr, std::string response, int begin, int exit)
{
int start;
int end;
string s;
int location = response.find("marketid", begin);
if(location <= exit)
{
start = location + 11;
begin = response.find("label", start);
end = begin - start - 3;
findStrings(start, end, s, &response);
curr._next = new Currency();
}
}
^显然是在cpp类中。
【问题讨论】:
-
你能在你的类声明上下文中显示它吗?尤其是你提到的那些问题,如果没有看到就很难说出来。
-
已编辑抱歉。如果您有任何问题,请告诉我
-
如果您的使用代码在类之外,请将类型更改为
Exchange::Currency,这样就可以了。 -
@JayBell 请在提交编辑之前查看预览并修复格式!有人修复了您问题的第一个版本,请参阅如何在添加的部分中执行相同操作。
-
感谢您的修复,我做了四个空格缩进,不知道为什么它没有那样显示!下次生病通过尝试 CTRL+K 来确保它正常工作!
标签: c++ class inheritance structure