Assigning two strings together getting Access Read Violation
- by Jay Bell
I am trying to pass a string to a class mutator and set the private member to that string here is the code that is sending the string
void parseTradePairs(Exchange::Currency *curr, std::string *response, int begin, int exit)
{
int start;
int end;
string temp;
string dataResponse;
CURL *tempCurl;
initializeCurl(tempCurl);
int location = response->find("marketid", begin);
if(location <= exit)
{
start = location + 11;
begin = response->find("label", start);
end = begin - start - 3;
findStrings(start, end, temp, response);
getMarketInfo(tempCurl, temp, dataResponse);
curr->_coin->setExch(temp); // here is the line of code that is sending the string
dataResponse >> *(curr->_coin);
curr->_next = new Exchange::Currency(curr, curr->_position + 1);
parseTradePairs(curr->_next, response, begin, exit);
}
}
and here is the mutator within the coin class that is receiving the string and assigning it to _exch
void Coin::setExch(string exch)
{
_exch = exch;
}
I have stepped through it and made sure that exch has the string in it. "105" but soon as it hits _exch = exch; I get the reading violation. I tried passing as pointer as well. I do not believe it should go out of scope. and the string variable in the class is initialized to zero in the default constructor but again that should matter unless I am trying to read from it instead of writing to it.
/* defualt constructor */
Coin::Coin()
{
_id = "";
_label = "";
_code= "";
_name = "";
_marketCoin = "";
_volume = 0;
_last = 0;
_exch = "";
}
Exchange::Exchange(std::string str)
{
_exch = str;
_currencies = new Currency;
std::string pair;
std::string response;
CURL *curl;
initializeCurl(curl);
getTradePairs(curl, response);
int exit = response.find_last_of("marketid");
parseTradePairs(_currencies, &response, 0, exit);
}
int main(void)
{
CURL *curl;
string str;
string id;
Coin coin1;
initializeCurl(curl);
Exchange ex("cryptsy");
curl_easy_cleanup(curl);
system("pause");
return 0;
}
class Exchange
{
public:
typedef struct Currency
{
Currency(Coin *coin, Currency *next, Currency *prev, int position) : _coin(coin), _next(next), _prev(prev), _position(position) {}
Currency(Currency *prev, int position) : _prev(prev), _position(position), _next(NULL), _coin(&Coin()){}
Currency() : _next(NULL), _prev(NULL), _position(0) {}
Coin *_coin;
Currency *_next;
Currency *_prev;
int _position;
};
/* constructor and destructor */
Exchange();
Exchange(std::string str);
~Exchange();
/* Assignment operator */
Exchange& operator =(const Exchange& copyExchange);
/* Parse Cryptsy Pairs */
friend void parseTradePairs(Currency *curr, std::string *response, int begin, int exit);
private:
std::string _exch;
Currency *_currencies;
};
here is what i changed it to to fix it.
typedef struct Currency
{
Currency(Coin *coin, Currency *next, Currency *prev, int position) : _coin(coin), _next(next), _prev(prev), _position(position) {}
Currency(Currency *prev, int position) : _prev(prev), _position(position), _next(NULL), _coin(&Coin()){}
Currency()
{
_next = NULL;
_prev = NULL;
_position = 0;
_coin = new Coin();
}
Coin *_coin;
Currency *_next;
Currency *_prev;
int _position;
};