C++ :: Overloading Binary And Assignment Operators In Vector Class

Feb 5, 2013

I am making a vector class and am having some problems creating the overloaded arithmetic operators and assignment operators.

Here is an example of my "+=" code as it stands the errors are similar/the same for the other operators except "=" operator which works fine:

Vector3& Vector3::operator+=(const Vector3 &rhs) {
Vector3 tmp = *this;
Set(tmp.getX() + rhs.getX(), tmp.getY() + rhs.getY(), tmp.getZ() + rhs.getZ());
return *this;
}

I have tried a lot of different approaches ad always get the error:

error: passing 'const Vector3' as 'this' argument of 'double Vector3::getX()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getY()' discards qualifiers
error: passing 'const Vector3' as 'this' argument of 'double Vector3::getZ()' discards qualifiers

View 5 Replies


ADVERTISEMENT

C++ :: Extractor Overloading And Member Wise Assignment For Cartesian Class

Apr 6, 2014

I am making a program with a Cartesian class. I want the user to be able to input 2 coordinates, but when I run it it doesn't ask for any values to be entered. It gives this output Please enter the first coordinates: Please enter the second coordinates:

(4.86129e-270, -1.97785e-41)
(4.86143e-270, -1.97785e-41)

Code:
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;

[Code] ....

Also, I want to add a memberwise assignment function to assign the values of coord1 to coord2. How would I go about doing so?

View 7 Replies View Related

C++ :: Arithmetic Operators Overloading For Class With Pointer

Nov 11, 2014

I am stucked in a problem of overloading arithmetic operators such as "+,*" for a class in the form

class Point {
int N; // dimension of the point
double *Pos; // length of N
}

My assign operator is :
Point& Point::operator= (const Point& pt) {
N= pt.N;
if(Pos == NULL) Pos = new double[N];
memcpy(Pos, pt.Pos, N*sizeof(double));

[Code] ....

The add operator "+" is:
Point operator+( const Point& pt1, const Point& pt2 ) {
Point ptr = Point(pt); // this is a constructor
for (int i=0; i<pt1.N; i++) ptr.Pos[i] += pt2.Pos[i];
return ptr;
}

Based on the above overloading, What I am going to do is :

P = alpha*P1 + beta*P2; // alpha and beta are double constants, P1 and P2 are Points objes

It is ok with Intel C++ 14.0 compiler, but does not work with the microsoft visual c++ 2012 compiler in debug mode in visual studio 2012.

I stepped in those operators and found that visual c++ compiler deconstructs the ptr in operators "*" and "+" before its return while intel c++ finished the operation P = alpha*P1 + beta*P2; and delete those ptrs at last.

Portability of my operator overloading is worse. How to get those arithmetic operators overloading for class with pointers in it.

View 3 Replies View Related

Visual C++ :: Class Fraction - Overloading Operators

Sep 25, 2013

the question am having problems with..

1.Write a class function that defines adding, subtracting, multiplying and dividing fractions by overloading standard operators for the operations.

2. Write a function member for reducing factors and overload I/O operators to input and output fractions. how would i set this up?

View 5 Replies View Related

C++ :: Error Overloading Operator / Class Template Vector

Feb 7, 2013

I'm trying to implement a vector class in C + +. However when I go to actually run the main, it generates an interrupt operating system.

MAIN.cpp
#include "header.h"
int main() {
// Definisco le istanze delle classi
vettore <int> vet;
vettore <int> vet2;
vettore <int> vet3;

[code].....

View 7 Replies View Related

C++ :: Overloading Subscripting Operators?

Nov 7, 2014

I have two class GameOfLife and Cell and i want to overload square braket for class GameOfLife."if g is a GameOfLife object, g[10][5] will return the Cell at row 10 and column 5. If there is no such Cells, then it will return a new Cell with position (-1000,- 1000)."

but if g[10][1000] and 1000>cols,then it returns different Cell exp (3,2) How i do control the col ( [row][col] )?

Code: vector<Cell> GameOfLife::operator [](const int row){
vector<Cell> rowCell;
for(int i=0; i<cols; ++i)
{
if( isLive(row,i) )
rowCell.push_back( Cell(row,i) );
else
rowCell.push_back( Cell(-1000,-1000) );
}
return rowCell;
}

View 6 Replies View Related

C++ :: Overloading Comparison Operators For Using In A Set

Oct 30, 2014

I have a small piece of code that used the set::insert function on a set of myClass. For that, I need to overload the < and > operators, which I have, but I still get a huge error saying it can't compare.

set<MyClass> mySet;
MyClass myClass

All the class information gets filled in. Then, I try to insert...
mySet.insert(myClass);

bool operator<(MyClass &lhs, MyClass &rhs) {
return lhs.name < rhs.name; //name is a string
}

The error says
...stl_function.h:230:22: error: no match for 'operator<' in '__x < __y'
MyFile.h:80:6: note: candidate is bool operator<(MyClass&, MyClass&)

View 5 Replies View Related

C/C++ :: Overloading Comparison Operators

Aug 28, 2014

I made a program that allows the user to enter information of credit cards on an array of size 5, however I need to allow the user to compare the five credit cards with each other and I am having problems with this particular part. I made my bool operator functions for the operator< and the operator> but how to make the user be able to select which cards he wants to compare and how to compare them. My code is the following:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 5;
enum OPCIONES {CARGAR=1, ABONAR, NADA};

[Code] ......

View 2 Replies View Related

C++ :: Does Polymorphism Affects Operators Overloading?

Nov 24, 2013

I must overload [] but at the same time I must use polymorphism. I wonder if using polymorphism affects operators overloading since when I modified the first class by writing "virtual":

virtual void mostrarDatos(char*, char*, char*);
virtual void calcularEdad(int);

So I can do the polymorphism, it affects the part of the code where suppose to do an addition:

s=student1+=student2;
t=student3+=student4;
u=s+=t;

if I do that, it shows some strange numbers instead of the right ones. Here is the complete code:

.h
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
class persona {

[Code] ......

View 2 Replies View Related

C++ :: Overloading Char And String Operators?

Dec 20, 2013

I build a nice class that i need the output be possible in char * and in string. But how I can do for the compiler don't be confused between them?

Code:
operator char*() {
return (char *) result.c_str();
}

These works fine. and for string, i must casting it. but i want do anotherone for string... how can i do it? I tried:

Code:
operator string() {
return result;
}

But the compiler stays confused or something

View 14 Replies View Related

C++ :: Overloading Stream Operators On Derived Classes

Apr 16, 2014

S I want to have different >> operators for several derived classes. Has I tested...

Code: class base{
friend std::istream & operator>>(std::istream & in, base & v);
public:
base();

[Code]......

I noticed that the base operator is the only one being called for all 3 objects. which makes sense and now that I think about it I am more surprised that the "derived operators" compiled at all.

Still, how would I go about declaring different overloaded operators for the different classes?

View 4 Replies View Related

C++ :: Vector Of Classes With Overloaded Operators Not Compiling

Dec 6, 2014

I am working on a school project and am stuck in the debugging. I am compiling with g++ and am using c++11.

Error message

In file included from /usr/include/c++/4.7/algorithm:63:0,
from date.h:7,
from date.cpp:1,
from schedule.h:1,
from schedule.cpp:1,
from date_driver.cpp:1:

[Code] ....

Here are my overloaded operators for my date class.

bool Date :: operator == (Date otherDate){
if (((otherDate.getDay () == getDay ()) && (otherDate.getMonth () == getMonth ())) && (otherDate.getYear () == getYear ()))

[Code] .....

View 1 Replies View Related

C/C++ :: Binary Operator Overloading Using Member Function

Dec 26, 2014

I want to create a program that shows the total of 2 subjects & percentage of student using binary operator overloading with member function in C++.

View 10 Replies View Related

C++ :: Vector Subscript Operator Overloading

Jun 24, 2014

To get a value I would always use setter and getter. Would it be much better (performance) to use vector subscript operator overloading? And does vector subscription operator overloading require a size for the vector?

vector<int> v(10);

because otherwise, it doesn't work...

View 9 Replies View Related

C++ :: Overloading Plus Operator For Linked Lists (binary Numbers)

Mar 1, 2013

I have tried to understand the concept of linked lists and I have read the assigned chapter 2 times. My teacher is a little laid back when it comes to teaching! This is only a portion of my program. This function is supposed to add 2 binary numbers 11101+1101 and store the result in the temp list. The answer I get is 10000.I don't think that it is adding the carry.

binary operator+( binary num1, binary num2) {
binary temp;
int carry, sum;

[Code]..

View 19 Replies View Related

C++ :: Assignment Operator For A Class With Reference

Mar 6, 2014

Class A
{..........}

Class B:
{....
private U& u;}

I need to write the copy constructor and assignment operator for Class B above. Copy would look something like this:

B::B(conts B& bo): u(bo.u){}

is this correct ... and how will the assignment operation look like??

View 3 Replies View Related

C/C++ :: How To Solve Illegal Structure Operation Error In Binary Operator Overloading

Aug 1, 2013

#include<iostream.h>
#include<conio.h>
class sum {
    int a,b,ans;
public:
void  geta() {
  cin>>a;

[Code] ....

View 1 Replies View Related

C++ ::  Overloaded Assignment Operator For Class Template Objects?

May 23, 2013

I designed a class template to create unique arrays. I was able to successfully input data to and output data from my array objects, irrespective of the datatype. However, I can't for the life of me fathom why my overloaded assignment operator worked perfectly well only for integer datatype and not for double/string datatypes.

Here is the class definition:

template <class dataType>
class myArray {
public:
void setArrayData();

[code]....

And here is the definition of the overloaded assignment operator:

template<class dataType>
const myArray<dataType>& myArray<dataType>::operator=(const myArray<dataType>& rightArray) {
int i;
if(this != &rightArray) {
delete [] arrayPtr;

[Code] ....

And here is my main function that tests the operations on objects of the class:

int main(){
//object declarations
myArray<double> list(5); //a single-parameter object declaration of class myArray
myArray<double> myList(2,13); //a two-parameter object declaration of class myArray

[code]....

The problem I'm having starts from where the assignment operator is being tested: for double and string datatypes, the upper input/output section works fine, but the assignment section freezes the display until the program execution is manually terminated!

View 19 Replies View Related

C++ :: Create Assignment Operator For A Class That Uses Pointer For Its Private Variable?

Mar 30, 2013

i am trying to create the assignment operator for a class that uses a pointer for it's private variable. The error is saying expected constructor, deconstructor, or type conversion before "operator. (which is the assignment operator. I have tried everything i could think of or find online and nothing has worked. below is the code for the assignment operator in the .h file and the .cpp file.

//Assignment constructor
indexList &operator=(const indexList <T> &rhs);
template <class T>
indexList<T>::indexList operator=(const indexList <T> &rhs) {
if(this != &rhs) {
numberOfElements = rhs.numberOfElements;

[Code]...

View 1 Replies View Related

C++ :: Class Template Friend Operators

Sep 19, 2013

The code below doesn't compile. Two things to clear up:

1) I know x is going to be garbage.
2) I used the same type name label ElementType.

#include <iostream>
#include <ostream>
template <typename ElementType>
class Example {

[Code] .....

View 3 Replies View Related

C++ :: Arithmetic Operators For Custom Class?

Oct 3, 2014

I have made a custom class matrices class which allows me to add, multiply, subtract (etc.) matrices. I have the following method for multiplication but I get an error that says

'invalid use of 'this' outside of a non-static member function'

How can I refer to the current instance without getting this error.

void operator *(Matrices& matrix2) {
this.multiplyMatrix(matrix2);
}

View 2 Replies View Related

C++ :: Force Derived Class To Overload Certain Operators

Dec 14, 2014

Is is possible to force derived classed to overload certain operators, like we do with pure virtual functions?

Is this possible to dynamically bind objects to their respective overloaded operators?

I am getting errors with undefined references to my base class vtable when I hackly try to overload: Code: operator+ I am not sure whether this is possible.

View 7 Replies View Related

C/C++ :: Overload Two Different Operators One Inside Class And Other Outside Using Friend

Nov 23, 2014

I'm trying to understand the basics of oop ...

#include <iostream>
using namespace std;
template <typename T>
class max_vector {
private:
T* elemente;
int lungime;

[Code] ....

The purpose of this program is to overload two different operators one inside the class, and the other one outside using friend. The problem is that i get 1 error at the '*' one.

View 1 Replies View Related

C++ :: Rational Number Class Implementation - Overloaded Operators

Sep 13, 2013

I'm implementing a rational number class:

#include <iostream>
#include <stdint.h>
using namespace std;

typedef int64_t RAT_INT;
struct RAT{
RAT_INT Num, Den;

RAT(RAT_INT num = 0, RAT_INT den = 1){
Num = num;
Den = den;

[Code].....

Two questions:
1) In the second line in main, how does C++ know to convert 2 to the appropriate RAT?
2) Is it possible to make the third line in main valid without adding global operators for all the member operators to support plain integers?

View 5 Replies View Related

C/C++ :: Open A File And Use Overloaded Operators For Complex Number Class

Apr 10, 2015

This code is meant to open a file and use overloaded operators for a complex number class. I am getting a lot of errors in my class declaration/definition but I am not sure why.

#include <iostream>
#include <cmath>
#include <fstream>
class CN {
public:
double real;
double im;

[Code] ....

View 5 Replies View Related

C++ :: Function Overloading In Child Class?

Jan 17, 2014

I am facing some problems while overloading base class functoin in child class. I have 2 programs as listed below.

Program 1 :

#include <iostream>
using namespace std;
class base
{

[Code].....

Compilation Errors:

child_overload.cpp: In function "int main()":
child_overload.cpp:27: error: no matching function for call to "child::func(const char [16])"
child_overload.cpp:17: note: candidates are: void child::func(double)

I thought as base class members are also as part of child class through "public" access specifier, it should access base class function, when funct() is called with a string. if I use "using base::func" in child, it works fine. But why I need that when base class memebers are part of child class?

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved