Advertisement

C++ won't compile std::find section

Started by July 04, 2018 08:42 PM
8 comments, last by Oberon_Command 6 years, 5 months ago

#pragma once
#include <string>
#include <algorithm>
#include "stock.hpp"

class person {
private:
	std::string name;
	float cash;
	std::vector<stock> investments;
public:
	person(std::string name, float cash) { this->name = name; this->cash = cash; }
	~person() = default;
	void buy_stock(std::shared_ptr<stock> shares, int quantity) 
	{ 
		cash -= (shares->getPrice() * quantity); //quantity TODO
		investments.push_back(*shares);
	}
	void sell_stock(std::shared_ptr<stock> shares, int quantity)
	{
		std::vector<stock>::iterator it = std::find(std::begin(investments), std::end(investments), shares);
		if (it == std::end(investments))
		{
			std::cout << "\n\nERROR - person.sell_stock tried to sell shares not in portfolio" << std::endl;
			char exit; std::cin >> exit;
		}
		else
		{
			ptrdiff_t index = std::distance(std::begin(investments), it);
			investments.erase(it);
			cash += (shares->getPrice() * quantity);
		}
	}
};

The void sell_stock function won't compile but I can't understand the compiler errors that come out, please can someone advise? Is it something to do with the fact I don't have an overload for ==?

 

by the way the ptrdiff_t index = std::distance I know is doing nothing

 

note: see reference to function template instantiation '_InIt std::_Find_unchecked<stock*,_Ty>(const _InIt,const _InIt,const _Ty &)' being compiled
1>        with
1>        [
1>            _InIt=stock *,
1>            _Ty=std::shared_ptr<stock>
1>        ]

Post the entire error message, including the error code.

Hello to all my stalkers.

Advertisement

1>------ Build started: Project: stock-simulator, Configuration: Debug Win32 ------

1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1772): warning C4244: 'argument': conversion from 'double' to 'float', possible loss of data
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1836): note: see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<const char(&)[9],double>(const char (&)[9],double &&)' being compiled
1>        with
1>        [
1>            _Ty=stock
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1837): note: see reference to function template instantiation 'std::_Ref_count_obj<_Ty>::_Ref_count_obj<const char(&)[9],double>(const char (&)[9],double &&)' being compiled
1>        with
1>        [
1>            _Ty=stock
1>        ]
1>c:\users\jonathan\documents\projects\stock-simulator\main.cpp(22): note: see reference to function template instantiation 'std::shared_ptr<stock> std::make_shared<stock,const char(&)[9],double>(const char (&)[9],double &&)' being compiled
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): error C2678: binary '==': no operator found which takes a left-hand operand of type 'stock' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(284): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(278): note: or       'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(272): note: or       'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(266): note: or       'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(330): note: or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(325): note: or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(320): note: or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\memory(1621): note: or       'bool std::operator ==<stock>(std::nullptr_t,const std::shared_ptr<stock> &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): note: while trying to match the argument list '(stock, const std::shared_ptr<stock>)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3592): note: see reference to function template instantiation '_InIt std::_Find_unchecked1<_InIt,_Ty>(_InIt,const _InIt,const _Ty &,std::false_type)' being compiled
1>        with
1>        [
1>            _InIt=stock *,
1>            _Ty=std::shared_ptr<stock>
1>        ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3601): note: see reference to function template instantiation '_InIt std::_Find_unchecked<stock*,_Ty>(const _InIt,const _InIt,const _Ty &)' being compiled
1>        with
1>        [
1>            _InIt=stock *,
1>            _Ty=std::shared_ptr<stock>
1>        ]
1>c:\users\jonathan\documents\projects\stock-simulator\person.hpp(22): note: see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,std::shared_ptr<_Ty>>(_InIt,const _InIt,const std::shared_ptr<_Ty> &)' being compiled
1>        with
1>        [
1>            _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<stock>>>,
1>            _Ty=stock
1>        ]
1>Done building project "stock-simulator.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

3 minutes ago, ICanC said:

error C2678: binary '==': no operator found which takes a left-hand operand of type 'stock' (or there is no acceptable conversion)

That's the relevant part.

Hello to all my stalkers.

Thanks, fixed it searching for shares->getName() instead of the object itself and adding:

bool operator==(std::string n)
    {
        return this->name == n;
    }

not too sure how to compare 2 objects yet, so just looking at the name will do for now

I've decided to change the code slightly and work with a vector of pairs instead. However now I'm having problems getting std::find to work again, please see the following code:


void sell_stock(std::shared_ptr<stock> shares, int quantity)
	{
		std::vector<std::pair<stock, int>>::iterator it = std::find(std::begin(investments), std::end(investments), shares->getName());
		if (it == std::end(investments))
		{
			std::cout << "\n\nERROR - person.sell_stock tried to sell shares not in portfolio" << std::endl;
			char exit; std::cin >> exit;
		}
		else
		{
			investments.erase(it);
			cash += (shares->getPrice() * quantity);
		}
	}
};

// ====================================================================================================================================

and the overload in the stock class :

bool operator==(std::pair<std::string,float> n)
	{
		return this->company.first == n.first;
	}

 

gives me error:

c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): error C2678: binary '==': no operator found which takes a left-hand operand of type 'std::pair<stock,int>' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\random(2557): note: could be 'bool std::operator ==(const std::bernoulli_distribution &,const std::bernoulli_distribution &)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(284): note: or       'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(278): note: or       'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(272): note: or       'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\system_error(266): note: or       'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(330): note: or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(325): note: or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\exception(320): note: or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3579): note: while trying to match the argument list '(std::pair<stock,int>, const std::string)'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xutility(3592): note: see reference to function template instantiation '_InIt std::_Find_unchecked1<_InIt,_Ty>(_InIt,const _InIt,const _Ty &,std::false_type)' being compiled

Advertisement

Did you implement operator== for the "stock" type that compares stocks with each other? std::pair<stock, int> would store the stock in the pair by value, so comparing pairs would mean comparing the values of the stocks.

the stock class now looks like this, but it generates the same error:

 


#pragma once
#include <iostream>
#include <iomanip>
#include <string>

class stock {
private:
	std::pair<std::string,float> company;
public:
	stock(std::string name, float price);
	~stock() = default;
	void display() const
	{
		std::cout << company.first << std::endl << std::fixed << std::setprecision(2) << company.second << std::endl;
	}
	const float getPrice() { return company.second; }
	const std::string getName() { return company.first; }
	bool operator==(stock n)
	{
		return this->company.first == n.company.first;
	}
};

stock::stock(std::string name, float price)
{
	company.first = name;
	company.second = price;
}

 

Oh - your std::find is confusing the types. You're passing a string into std::find, but the container contains std::pair<stock, int>. That isn't going to work - you can't compare std:pair<stock, int> to std::string. :P 

You probably want to use std::find_if to use a custom search predicate.

This topic is closed to new replies.

Advertisement