Reputation: 808
So I have this class called order, and when a user places an order i invoke that class. I have another class called orderbook, it is a vector of orders.
I wanted to try to traverse the orderbook upon receipt of a new order to check if there are duplicates, but right now i am stuck traversing the vector.
I am using the iterator and comparing the new order's symbol vs each order in my orderbook's symbol.
I read that the iterator is like a pointer and it will return the memory location of my vector's orders and *it would give me the order itself and if I add a .symbol to the it that should give me the order's symbol attribute, but right now it won't even compile.
I get this error when I try:
../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) {}
| ^~~~~~
below is the code, I have deleted a bunch of stuff not relevant to the question.
#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;
}
Upvotes: 1
Views: 96
Reputation: 60440
When you write
*it.symbol
since the operator precedence for .
is higher than *
, so the expression becomes:
*(it.symbol)
and the error says that an iterator doesn't have a member named symbol
which is true.
You can force the precedence by doing:
(*it).symbol
or even better, by using the appropriate operator:
it->symbol
Upvotes: 3