I have the following problem with my search function.
It expects 3 parameters, and one of them is something like const rational_t* v. I want to pass a vector through that parameter but it doesnt seems to work..
Code:
#include <iostream>
#include <cmath>
#include <vector>
#include "rational_t.hpp"
using namespace std;
bool search(const rational_t* v, const int n, const rational_t& x)
{
for(int i = 0; i < n; i++) {
if(v[i].value() == x.value()) {
return true;
} else {
return false;
}
}
};
int main()
{
rational_t a(1, 2), b(3), c, d(1, 2);
vector<rational_t> v;
v.push_back(a);
v.push_back(b);
v.push_back(c);
cout << "a.value()= " << a.value() << endl;
cout << "b.value()= " << b.value() << endl;
cout << "c.value()= " << c.value() << endl;
cout << search(v, v.size(), d); // Problem here
return 0;
}
I´ve also tried cout << search(v&, v.size(), d); with the reference &.
Any ideas? Thank You.
The class :
#pragma once
#include <iostream>
#include <cassert>
#include <cmath>
#define EPSILON 1e-6
using namespace std;
class rational_t
{
int num_, den_;
public:
rational_t(const int = 0, const int = 1);
~rational_t() {}
int get_num() const
{
return num_;
}
int get_den() const
{
return den_;
}
void set_num(const int n)
{
num_ = n;
}
void set_den(const int d)
{
assert(d != 0), den_ = d;
}
double value(void) const;
rational_t opposite(void) const;
rational_t reciprocal(void) const;
bool equal(const rational_t &r, const double precision = EPSILON) const;
bool greater(const rational_t &r, const double precision = EPSILON)
const;
bool less(const rational_t &r, const double precision = EPSILON) const;
bool cero_equal(const double precision) const;
void write(ostream &os = cout) const;
void read(istream &is = cin);
};
The first argument of search should be a rational_t* but you're passing a vector<rational_t>.
You want
search(v.data(), v.size(), d)
instead of
search(v, v.size(), d)
But I'd write this like this which is cleaner IMO:
bool search(vector<rational_t> & v, const rational_t& x)
{
for (int i = 0; i < v.size(); i++) {
if (v[i].value() == x.value()) {
return true;
}
else {
return false;
}
}
}
...
cout << search(v, d);
Related
I need to sort a vector of pointers with this test data.
main.cpp
#include "cart.h"
int main()
{
Cart cart;
cart.addItem("Desktop", 125.34f);
cart.addItem("Iphone", 46.274f);
cart.addItem("Pen", 118.99f);
cart.addItem("Ruler", 41.34f);
cart.addItem("Printer", 2.99f);
cart.printSortedItems();
cart.destroy();
}
item.h
#include <string>
#include <tuple>
class Item
{
private:
Item(const std::string& name, const ItemType& itemType, const float& price);
~Item();
std::string m_name;
float m_price;
public:
static Item *getInstance(const std::string& name, const float& price);
bool operator>(const Item& item2) const;
void print() const;
};
item.cpp
#include "item.h"
#include <iostream>
Item::Item(const std::string& name, const float& price)
{
m_name = name;
m_price = price;
}
Item::~Item()
{
}
Item *Item::getInstance(const std::string& name, const float& price)
{
return new Item(name, price);
}
bool Item::operator>(const Item & item2) const
{
if (m_name > item2.m_name) {
return true;
}
if (m_name == item2.m_name && m_price > item2.m_price) {
return true;
}
return false;
}
void Item::print() const
{
std::cout << "Item name: " << m_name << " Item price: "<< m_price << std::endl;
}
cart.h
#include <vector>
class Cart
{
public:
Cart();
~Cart();
void addItem(const std::string& name, const float& price);
void printSortedItems();
void sortItems();
void destroy();
private:
std::vector<Item*> m_itemList = {};
};
cart.cpp
#include "cart.h"
#include <iostream>
#include <algorithm>
Cart::Cart(){}
Cart::~Cart(){}
void Cart::addItem(const std::string& name, const float& price)
{
Item *newItem = Item::getInstance(name, itemType, price);
m_itemList.push_back(newItem);
}
void Cart::sortItems()
{
for (size_t i = 0; i < m_itemList.size()-1; i++)
{
for (size_t j = i + 1; j < m_itemList.size(); j++)
{
if (m_itemList[i] > m_itemList[j])
{
std::swap(m_itemList[i], m_itemList[j]);
}
}
}
}
void Cart::printSortedItems()
{
sortItems();
std::cout << "Items" << std::endl;
for (size_t i = 0; i < m_itemList.size(); i++)
{
m_itemList[i]->print();
}
std::cout << std::endl;
}
void Cart::destroy()
{
for (size_t i = 0; i < m_itemList.size(); i++)
{
delete m_itemList[i];
m_itemList[i] = nullptr;
}
}
In function Cart::sortItems(), I compare m_itemList[i] and m_itemList[j], these are both pointers to instance of Item. Theoretically, if I set a break point in bool Item::operator>(const Item & item2) const, it should be hit. But in this case, the break point is not hit, and therefore, the result after sorting is wrong. Could you guys tell me where I am wrong?
Here is the class
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;
class Point {
protected:
int x, y;
double operator-(const Point &def){
return sqrt(pow((x-def.x),2.0)+
pow((y-def.y),2.0));
}
};
class Circle: public Point {
private:
int radius;
public:
Circle(){
this->x=x;
this->y=y;
this->radius=radius;
}
Circle(int x, int y, int radius){
this->x=x;
this->y=y;
this->radius=radius;
}
void printCircleInfo() {
cout << x << " " << y << " " << radius << " " ;
}
This is the operator I want to be the condition in my if statement.
bool operator==(const Circle &def){
return (x==def.x) & (y==def.y) & (radius==def.radius);
}
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
if (anotherCircle.radius + radius >= *this - anotherCircle )
return true;
return false;
}
};
Here is main
int main(){
int x,y,radius;
const int SIZE = 13;
Circle myCircleArry[SIZE];
myCircleArry[0] = Circle(5,3,9);
cout << endl;
myCircleArry[0].printCircleInfo(); cout << " ; ";
ifstream Lab6DataFileHandle;
Lab6DataFileHandle.open("Lab6Data.txt");
while (!Lab6DataFileHandle.eof( )) {
for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>x;
Lab6DataFileHandle>>y;
Lab6DataFileHandle>>radius;
myCircleArry[i] = Circle(x,y,radius);
if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
myCircleArry[i].printCircleInfo(); cout << " ; ";
Here is the If statement
if ( operator==( Circle &def))
{cout <<"*";
}
}
}
}
Lab6DataFileHandle.close();
}
How do I use the overloaded operator as the condition of the if statement? If you need any clarification just ask other wise please leave an example in your answer.
Thank you for your time.
A == needs two arguments (even if the overload is a member), you would write the if as any other if statement:
if(circle1 == circle2) { ... }
and if there's a matching overload the compiler would transform that into something like:
if(circle1.operator ==(circle2)) { ... }
Say I have a simple vector class, vec:
#include <iostream>
#include <stdlib.h>
class vec {
public:
vec() {}
// Constructor.
vec(int n) {
len = n;
data = new double[len];
}
// Destructor.
~vec() { delete [] data; }
// Accessor.
double & operator[](int i) const {
check_index(i);
return data[i];
}
// Other methods...
// ....
protected:
int len;
double * data;
void check_index(int i) const {
if(i < 0 || i >= len) {
std::cerr << "Bad access.\n";
exit(1);
}
}
};
Now suppose I have a special type of vector with sparse structure, e.g., where every even-index is zero. Call this oddvec. Instances of oddvec should be declared just as with the vec class, but underneath, the memory use should be efficient since only half the data is non-zero.
The accessor for the oddvec class should return 0 if the index is even, and return the odd-index element (stored sequentially) otherwise. There a couple problems with this:
The double & return type is violated if the index is even, since the constant value, 0, is returned.
It's not clear to me how to handle the situation when an even index element is used as an lvalue. E.g., v[0] = 3.0 should not be allowed in the oddvec class, but is perfectly acceptable in the vector class. We can't simply throw an error when even indexes are used, because even indexes are fine as long as the intention is as an rvalue.
How do I design the accessor function for the oddvec class, while both keeping the memory storage efficient and inheriting all the methods from the parent?
Non-working example of oddvec:
class oddvec : public vec {
public:
// Constructor.
oddvec(int n) {
len = n;
data = new double[len/2];
}
// Accessor (doesn't work!)
double & operator[](int i) const {
check_index(i);
if (i%2 == 0)
return 0;
else
return data[(i-1)/2];
}
};
Upon compilation:
main.cpp: In member function ‘double& oddvec::operator[](int) const’:
main.cpp:49:20: error: invalid initialization of non-const reference of type ‘double&’ from an rvalue of type ‘double’
return 0;
Working example using proxy classes:
I have implemented a proxy class as suggested in the answer below.
proxies.h
#ifndef PROXIES_H
#define PROXIES_H
#include <iostream>
#include <stdlib.h>
class proxy {
public:
proxy(int i, double v, double * d) {
index = i;
value = v;
data = d;
}
void operator=(double rhs) {
data[index] = rhs;
}
friend std::ostream & operator<<(std::ostream & outs, const proxy & p) {
outs << p.value;
return outs;
}
protected:
int index;
double value;
double * data;
};
class oddproxy : public proxy {
public:
oddproxy(int i, int v, double * d) : proxy(i, v, d) {}
void operator=(double rhs) {
if (index%2 == 0) {
std::cerr << "Even entries of oddvec are not assignable.\n";
exit(1);
}
data[index/2] = rhs;
}
};
#endif
vectors.h
#ifndef VECTORS_H
#define VECTORS_H
#include "proxies.h"
class vec {
public:
vec() {}
// Constructor.
vec(int n) {
len = n;
data = new double[len];
}
// Destructor.
~vec() { delete [] data; }
// Accessor.
proxy operator[](int i) const {
check_index(i);
return proxy(i, data[i], data);
}
inline int length() const { return len; }
// Other methods...
// ....
protected:
int len;
double * data;
void check_index(int i) const {
if(i < 0 || i >= len) {
std::cerr << "Bad access.\n";
exit(1);
}
}
};
class oddvec : public vec {
public:
// Constructor.
oddvec(int n) {
len = n;
data = new double[len/2];
}
// Accessor.
oddproxy operator[](int i) const {
check_index(i);
return oddproxy(i, (i%2 == 0) ? 0 : data[i/2], data);
}
};
#endif
main.cpp
#include <iostream>
#include "vectors.h"
int main () {
int N = 5;
vec V(N);
oddvec O(N);
for(int i=0; i < V.length(); i++) {
V[i] = i;
if(i%2 != 0) {
O[i] = i;
}
}
for(int i=0; i < O.length(); i++) {
std::cout << "V[" << i << "]=" << V[i] << ", "
<< "O[" << i << "]=" << O[i] << "\n";
}
O[0] = 13;
return 0;
}
output
V[0]=0, O[0]=0
V[1]=1, O[1]=1
V[2]=2, O[2]=0
V[3]=3, O[3]=3
V[4]=4, O[4]=0
Even entries of oddvec are not assignable.
You can use proxy object to do this.
simple sample code:
#include <iostream>
#include <vector>
using namespace std;
class very_odd_vector{
public:
class only_odd_proxy;
friend class only_odd_proxy;
only_odd_proxy operator [](int index);
int operator [](int index)const{return index%2==0?0:content[index/2];}
unsigned int size()const{return content.size()*2;}
private:
vector<int> content{1,3,5,7,9};
};
class very_odd_vector::only_odd_proxy{
public:
only_odd_proxy(very_odd_vector& vec,int index):vec(vec),index(index){}
operator int(){return index%2==0 ? 0 : vec.content[index/2];}
only_odd_proxy& operator =(int value){
if(index%2==0)
cout << "BAD OPERATION";//any error you want
else
vec.content[index/2] = value;
return *this;
}
private:
very_odd_vector& vec;
int index;
};
auto very_odd_vector::operator [](int index)->only_odd_proxy{return only_odd_proxy(*this,index);}
int main(){
very_odd_vector v;
cout << "reading value\n";
for(int i=0;i<v.size();++i)
cout << v[i] <<'\n';
cout << "writting value\n";
for(int i=0;i<v.size();++i){
cout << i << ':';
v[i]=10;
cout << '\n';
}
cout << "reading value\n";
for(int i=0;i<v.size();++i)
cout << v[i] <<'\n';
}
Edit for updated part of question :
I think this class will fit your need more.
//Both base and inherit class return this class
class maybe_readonly_proxy {
public:
maybe_readonly_proxy(double* data, bool readonly):readonly(readonly),data(data){}
maybe_readonly_proxy& operator=(double rhs) {
if(readonly){/*whatever error*/}
else {*data = rhs;}
return *this;
}
operator double()const{return *data;}
private:
bool readonly;
double * data;
};
You may need a variable to contain readonly (0 in this case) value, or modify the operator double() the check readonly state
Or just implement get and set method separately and do not use this proxy may be another choice.
I am having difficulty understanding why I get this error in the context of my simple program using a user defined class "Rectangle"
The Rectangle class I made is used to create rectangles by inputting length/width, then printing l/w/area.
I have looked in these locations so far in an attempt to understand the issue, and still can not understand the problem.
https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(C3867)&rd=true
Visual Studio 2015 "non-standard syntax; use '&' to create a pointer to member"
Visual Studio 2015 "non-standard syntax; use '&' to create pointer for member"
(I do not understand what pointers are, I have not learned about them yet in the book Stroustrup: Programming -- Principles and Practice Using C++ 2nd Ed.# Ch.10)
Here is my Rectangle.h
#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle();
Rectangle(double dblp_length, double dblp_width);
bool is_square() const;
void set_length(double dblp_length);
double get_length() const;
void set_width(double dblp_width);
double get_width() const;
void set_area(double dblp_length, double dblp_width);
double get_area() const;
void print(ostream & output);
private:
void Rectangle::init(double dblp_length, double dblp_width);
double dbl_length, dbl_width, dbl_area;
};
My Rectangle.cpp
#include "stdafx.h"
#include "Rectangle.h"
#include <iostream>
Rectangle::Rectangle() {
init(8, 8);
}
Rectangle::Rectangle(double dblp_length, double dblp_width) {
init(dblp_length, dblp_width);
}
void Rectangle::init(double dblp_length, double dblp_width) {
set_length(dblp_length);
set_width(dblp_width);
}
void Rectangle::set_length(double dblp_length) {
if (dblp_length < 0 || dblp_length > 1024) {
dblp_length = 8;
}
double dbl_length = dblp_length;
}
double Rectangle::get_length() const {
return dbl_length;
}
void Rectangle::set_width(double dblp_width) {
if (dblp_width < 0 || dblp_width > 1024) {
dblp_width = 8;
}
double dbl_width = dblp_width;
}
double Rectangle::get_width() const {
return dbl_width;
}
bool Rectangle::is_square() const {
if (get_length() == get_width()) {
return true;
}
}
void Rectangle::set_area(double dblp_length, double dblp_width) {
double dbl_area;
dbl_area = (dblp_length * dblp_width);
}
double Rectangle::get_area() const {
return dbl_area;
}
void Rectangle::print(ostream & output) {
output << "Length: " << get_length() << ", " <<
"Width :" << get_width() << ", " <<
"Area: " << get_area << endl;
}
Here is the corrected version, with reasons and original code commented.
Potential problem:
area has not set by init, and can be set to a value that get_area() != get_width() * get_length()
Rectangle.h
#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle();
Rectangle(double dblp_length, double dblp_width);
bool is_square() const;
void set_length(double dblp_length);
double get_length() const;
void set_width(double dblp_width);
double get_width() const;
void set_area(double dblp_length, double dblp_width);
double get_area() const;
void print(ostream & output);
private:
// Remove "Rectangle::" from here
// This is not work for gcc and clang
// void Rectangle::init(double dblp_length, double dblp_width);
void init(double dblp_length, double dblp_width);
double dbl_length, dbl_width, dbl_area;
};
Rectangle.cpp:
#include "stdafx.h"
#include "Rectangle.h"
#include <iostream>
Rectangle::Rectangle() {
init(8, 8);
}
Rectangle::Rectangle(double dblp_length, double dblp_width) {
init(dblp_length, dblp_width);
}
void Rectangle::init(double dblp_length, double dblp_width) {
set_length(dblp_length);
set_width(dblp_width);
}
void Rectangle::set_length(double dblp_length) {
if (dblp_length < 0 || dblp_length > 1024) {
dblp_length = 8;
}
// "double" is not needed, it introduced a local variable instead of
// changing the instance variable.
// double dbl_length = dblp_length;
dbl_length = dblp_length;
}
double Rectangle::get_length() const {
return dbl_length;
}
void Rectangle::set_width(double dblp_width) {
if (dblp_width < 0 || dblp_width > 1024) {
dblp_width = 8;
}
// "double" is not needed, it introduced a local variable instead of
// changing the instance variable.
// double dbl_width = dblp_width;
dbl_width = dblp_width;
}
double Rectangle::get_width() const {
return dbl_width;
}
bool Rectangle::is_square() const {
// missing the false part
// if (get_length() == get_width()) {
// return true;
// }
// return the boolean value directly instead
return get_length() == get_width();
}
void Rectangle::set_area(double dblp_length, double dblp_width) {
// this line is not needed, it introduced a local variable,
// making future assignment assigns to local instead of instance variable
// double dbl_area;
dbl_area = (dblp_length * dblp_width);
}
double Rectangle::get_area() const {
return dbl_area;
}
void Rectangle::print(ostream & output) {
output << "Length: " << get_length() << ", " <<
"Width :" << get_width() << ", " <<
// missing () after get_area
// "Area: " << get_area << endl;
"Area: " << get_area() << endl;
}
In the following example, how to refer to the current object instance to take opportunity to use the output overload?
class Shape {
private:
double _length, _width;
double straight(double value) {
if (value<0) { return -value; }
if (value==0) { return 1; }
return value;
}
public:
Shape() { setDims(1,1); }
Shape(double length, double width) {
setDims(length, width); }
void setDims(double length, double width) {
_length=straight(length); _width=straight(width); }
friend ostream &operator<<(ostream &output, Shape &S) {
output << S._length << "," << S._width; return output; }
void display() { cout << [THIS] << endl; }
};
int main(int argc, const char * argv[]) {
Shape s1; s1.display();
return 0;
}
Just like this:
void display() { cout << *this << endl; }
this is a pointer. Your operator<< wants an actual Shape object, not a pointer.
So you'll have to dereference the pointer first: *this.
Alternatively just use operator<<
#include <iostream>
using namespace std;
class Shape {
private:
double _length, _width;
double straight(double value) {
if (value<0) { return -value; }
if (value == 0) { return 1; }
return value;
}
public:
Shape() { setDims(1, 1); }
Shape(double length, double width) {
setDims(length, width);
}
void setDims(double length, double width) {
_length = straight(length); _width = straight(width);
}
friend ostream &operator<<(ostream &output, Shape &S) {
output << S._length << "," << S._width; return output;
}
int main(int argc, const char * argv[]) {
Shape s1;
std::cout << s1 << std::endl;
}