C++ C++;can';t访问向量返回的类中的公共属性';s迭代器
所以我有一个叫做order的类,当用户下订单时,我调用这个类。 我有另一个类叫orderbook,它是订单的向量 我想在收到新订单时尝试遍历订单簿,以检查是否有重复的订单,但现在我无法遍历向量 我正在使用迭代器,将新订单的符号与订单簿符号中的每个订单进行比较 我读到迭代器就像一个指针,它会返回向量的顺序的内存位置,它会给我顺序本身,如果我向它添加一个.symbol,它会给我顺序的symbol属性,但现在它甚至不会编译 我在尝试时遇到此错误:C++ C++;can';t访问向量返回的类中的公共属性';s迭代器,c++,vector,iterator,C++,Vector,Iterator,所以我有一个叫做order的类,当用户下订单时,我调用这个类。 我有另一个类叫orderbook,它是订单的向量 我想在收到新订单时尝试遍历订单簿,以检查是否有重复的订单,但现在我无法遍历向量 我正在使用迭代器,将新订单的符号与订单簿符号中的每个订单进行比较 我读到迭代器就像一个指针,它会返回向量的顺序的内存位置,它会给我顺序本身,如果我向它添加一个.symbol,它会给我顺序的symbol属性,但现在它甚至不会编译 我在尝试时遇到此错误: ../src/oncemore.cpp:123:40:
../src/oncemore.cpp:123:40: error: 'std::vector<order>::iterator' {aka 'class __gnu_cxx::__normal_iterator<order*, std::vector<order> >'} has no member named 'symbol'
123 | if (_clientOrder.symbol.compare(*it.symbol)==0) {}
| ^~~~~~
。/src/oncemore.cpp:123:40:错误:'std::vector::迭代器'{aka'类{uu_gnu_cxx::u normal_迭代器'}没有名为'symbol'的成员
123 |如果(_clientOrder.symbol.compare(*it.symbol)==0{}
| ^~~~~~
下面是代码,我删除了一些与问题无关的东西
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <string.h>
#include "windows.h"
#include <vector>
#include <chrono>
#include <algorithm>
using namespace std;
class order {
public:
string orderID;
char buysell;
string symbol;
double price;
int qty;
void newOrder(string &_orderID, char &_buysell, string &_symbol, double &_price, int &_qty){
orderID=_orderID;
buysell=_buysell;
symbol=_symbol;
price=_price;
qty=_qty;
}
};
class orderbook {
private:
string orderID;
char buysell;
string symbol;
double price;
int qty;
vector<order> the_orderbook;
vector<order>::iterator it;
public:
string insert(order &_clientOrder){
for (it = the_orderbook.begin(); it != the_orderbook.end(); it++) {
if (_clientOrder.symbol.compare(*it.symbol)==0) {} <-- this line fails
}
the_orderbook.push_back(_clientOrder);
return "bla";
}
};
int main() {
cout << "!!!Hello once more" << endl; // prints !!!Hello once more
string user_input = "";
string done= "done trading";
string orderID;
string orderaction;
string orderRecvedTime;
char buysell;
string symbol;
double price;
int qty;
string error;
orderbook thebook;
order user_order;
while(user_input.compare(done) != 0) {
cout << "enter order"<< endl;
getline(cin, user_input);
stringstream lineStream(user_input);
lineStream >>orderaction>>orderID>> buysell >> symbol >> price>> qty;
if (orderaction.compare("D") == 0) {
string tag150;
user_order.newOrder(orderID, buysell,symbol,price,qty);
tag150 = thebook.insert(user_order);
cout << "You made a new order."<< endl;
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括“windows.h”
#包括
#包括
#包括
使用名称空间std;
阶级秩序{
公众:
字符串orderID;
煤焦买卖;
字符串符号;
双倍价格;
整数数量;
作废新订单(字符串和\u订单ID、字符和\u买卖、字符串和\u符号、双倍和\u价格、整数和\u数量){
orderID=\u orderID;
buysell=\u buysell;
符号=_符号;
价格=_价格;
数量=_数量;
}
};
类订单{
私人:
字符串orderID;
煤焦买卖;
字符串符号;
双倍价格;
整数数量;
对订单进行矢量化;
向量::迭代器;
公众:
字符串插入(顺序和clientOrder){
for(it=the_orderbook.begin();it!=the_orderbook.end();it++){
如果(_clientOrder.symbol.compare(*it.symbol)==0){}>买卖>>符号>>价格>>数量;
if(orderaction.compare(“D”)==0){
字符串tag150;
用户\订单.新订单(订单ID,买卖,符号,价格,数量);
tag150=书本插入(用户订单);
当你写作的时候
*it.symbol
由于
的运算符优先级高于*
,因此表达式变为:
*(it.symbol)
错误是迭代器没有名为symbol
的成员,这是真的
您可以通过执行以下操作强制优先级:
(*it).symbol
或者更好,通过使用适当的运算符:
it->symbol