-
March 11th, 2021, 02:38 PM
#1
Error: No Operator << Matches These Operands
Code:
#include "Inventory.h"
#include "Car.h"
#include "CarSpec.h"
#include <cmath>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// output abstraction object properties to the console window
void show(const Car & item)
{
const CarSpec& is{ item.get_spec() };
cout << item.get_id() << " "
<< "'" << item.get_model() << "' "
<< std::fixed << std::setprecision(2) << item.get_price() << " "
<< is.get_wheel_size() << " "
<< is.get_seat() << " "
<< is.get_type_as_string() << " "
<< endl;
}
Car max_price(const Inventory & inv)
{
Car max_price;
Car* ref_a = & max_price;
for (size_t i = 0; i <= inv.get_count(); i++)
{
float price = inv.get_item(i).get_price();
*ref_a = (price > ref_a->get_price()) ? inv.get_item(i) : *ref_a;
}
// cout << "The highest priced Car is: " << max_price.get_price() << endl;
return max_price;
}
float avg_price(const Inventory & inv)
{
float sum = 0.0f;
for (size_t i = 0; i <= inv.get_count(); i++)
{
sum += inv.get_item(i).get_price();
}
float avg = sum / inv.get_count();
//cout << "Average price of Car's are: " << avg << endl;
return avg;
}
// solution entry function
Code:
int main()
{
Inventory inv;
// specification is constructed separately
CarSpec spec_bmw{ CarSpec::Type::BMW, 16, 5 };
inv.add_item(1, "BMW A100", 1450.0, spec_bmw);
// specification is constructed during passing of an argument
inv.add_item(2, "AUDI A150", 1380.50, CarSpec{ CarSpec::Type::AUDI, 15, 4 });
// constructing and using shared specification
CarSpec spec_scada{ CarSpec::Type::SCADA, 17, 5 };
inv.add_item(3, "SCADA RIO", 4500.0, spec_scada);
inv.add_item(4, "SCADA SUPER", 9500.0, spec_scada);
// provide some querying values (some can be default (e.g., "", 0) to denote unset criteria)
show(inv.find_item(CarSpec{ CarSpec::Type::ANY, 20, 0 }));
// test with another query values
show(inv.find_item(spec_scada));
show(inv.find_item(Car{ 0, "", 0.0, CarSpec{ } }));
// testing for non-matching criterion
show(inv.find_item(CarSpec{ CarSpec::Type::KIA, 0, 0 }));
show(inv.find_item(Car{ 5, "",0.0, CarSpec{ } }));
//show(max_price(inv));
// const double epsil{ 0.005 };
// assert(19500 == max_price(inv).get_price());
//max_price(inv);
// avg_price(inv);
cout << "Average price of Car's are: " << avg_price(inv) << endl;
cout << "The highest priced Car is: " << max_price(inv) << endl;
return 0;
}
error is this line
Code:
cout << "The highest priced Car is: " << max_price(inv) << endl;
Last edited by VictorN; March 11th, 2021 at 03:03 PM.
Reason: added CODE tags
-
March 11th, 2021, 03:06 PM
#2
Re: Error: No Operator << Matches These Operands
It looks like your main() has no idea what max_price() is.
Victor Nijegorodov
-
March 11th, 2021, 03:13 PM
#3
Re: Error: No Operator << Matches These Operands
i want to call max_price Function
-
March 11th, 2021, 03:21 PM
#4
Re: Error: No Operator << Matches These Operands
Originally Posted by saliya1000
i want to call max_price Function
Well you can call it but
Code:
cout::Operator << ()
seems to have no idea how to use the type/value that is returned by this function.
Victor Nijegorodov
-
March 12th, 2021, 04:39 AM
#5
Re: Error: No Operator << Matches These Operands
You haven't provided a compilable example. Are the 2 sets of code in different compilation units? if yes, is max_price() declared in a header file used by the compilation unit that has main() ?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 12th, 2021, 08:49 AM
#6
Re: Error: No Operator << Matches These Operands
max_price returns a Car. Shouldn't it return an amount?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|