Hi All, in below class i wanted to have two friend function(overloading) one with default argument and other is without default argument. but during compilation it gives error. could you let me know how to fix (overload) this by not changing the function name , argument type or number of arguments. **error:** warning: void fun(int, int, int) is already a friend of class foo #include<iostream> using namespace std; class foo { public: friend void fun (int a, int b, int c); friend void fun (int a, int b, int c=20); }; int main() { return 1; }

Member Avatar
Member Avatar
+0 forum 1

I am not sure why my program isn't writing to the text file. When I start the program it tells me that the file opens. Then I call the functions that are inherited from other classes and I would assume that it would write to the file and then display them. I was able to get this to work earlier without using the insertion and extraction operators, but I wanted to get it to work with them as well. Does anyone have any ideas? //A Class for a checking account with a set interest. class CheckingWithInterest : public SavingsAccount, public …

Member Avatar
Member Avatar
+0 forum 3

This assignment asks to add some code from to a previous assignment I had. I have been confused about this overload operators alot and im confused on how to do it in my code any help with this will be good A) overload the >> operator to allow for reading a new name and new balance from the keyboard and use the data to update the object's data members B) Overload the << operator to dsiplay the Name, Balance, and Interest Rate of an object on the screen with proper labels. C) Overload the += operator to allow an increase …

Member Avatar
Member Avatar
+0 forum 7

Dear Kind DaniWebbers, I am trying to overload the fstream operators (ofstream/ifstream), so that I can save a class to a 'Binary' file and also display it with cout. But the ways in which each need to be implemented is different. Please can you show me how to define different operators for each. Here is my test class: class CBase { public: //CBase(void) {}; CBase(int i = 0, float f = float(0.0), double d = double(0.0), char c = 'A') : iMyInt(i), fMyFloat(f), dMyDouble(d), cMyChar(c) {} ~CBase(void) {} int iMyInt; float fMyFloat; double dMyDouble; char cMyChar; friend std::ostream& operator<<(std::ostream& os, …

Member Avatar
Member Avatar
+0 forum 4

Hello, I have a particular set up of cast operators and constructors which is leading to a compile-time ambiguity error. I am wondering if somebody can explain why this is happening and what can be done to fix it. For example, here we have 2 classes: class A { int val; public: A(){val=0;} A(const A& o){val=o.val;} A(int i){val=i;} A&operator=(const A&o){val=o.val;return *this;} A&operator=(int i){val=i;return *this;} operator int(){return val;} }; class B { int val; public: B(int i){val=i;} operator A(){return A(val);} operator double(){return 0.0;} }; They work fine but don't really do anything at all. The purpose of the A operator in …

Member Avatar
Member Avatar
+0 forum 4

I have been told it is good to overload the stream extraction operator to load data from a file. Would I simple use this in my class: friend ifstream &operator>>( ifstream &input, Class &C ) { input >> C.variable1 >> C.variable2 >> C.variable3; return input; } What are the advantages of overloading the stream extraction operator to read data from a file? Also I am confused what to do in main. At the moment to read in the data I have for example: ifstream fin; fin.open("filename.txt"); if(fin.good()) { object1 = new Class[10]; for (int i=0; i<10; i++) { fin >> …

Member Avatar
Member Avatar
+0 forum 7

Hi all, A question asks me to write overloading functions to square,circle ,rectangle and triangle.... But since the square and circle have only one parameter I had to change the parameter of area of circle to double...... Is this the only way to accomplish this..... Are there any other ways of writing it with out this kind of cheating.... public class ShapeArea{ public static int Area(int sideLength){ return Area(sideLength,sideLength); } public static int Area(int sideA,int sideB){ return sideA*sideB; } public static double Area(double r){ return Math.PI*r*r; } public static void main(String cmd[]){ System.out.println(Area(5.0)); } } Please suggest me towards a …

Member Avatar
Member Avatar
+0 forum 2

---------------------------------------------TSet.h------------------------------------------------------ #include <iostream> using namespace std; template <class V> class TSet { public: TSet(); TSet(int size); ~TSet(); void add(V new_item); void remove(V item_to_remove); int num_of_items(); bool isFull(); bool is_item_in_set(V item); friend bool operator==(const TSet<V>& left, const TSet<V>& right); friend bool operator!=(const TSet<V>& left, const TSet<V>& right); friend ostream& operator<<(ostream& outs,const TSet<V>& list); private: V *item_list; int size_of_list; int current_size; }; ----------------------------------------TSet.cpp----------------------------------------- #include <iostream> #include "TSet.h" using namespace std; template<class V> TSet<V>::TSet() : size_of_list(0), current_size(0) { item_list = new V[0]; } template<class V> TSet<V>::TSet(int size) : size_of_list(size), current_size(0) { item_list = new V[size]; } template<class V> TSet<V>::~TSet() { delete [] item_list; …

Member Avatar
+0 forum 0

Please Help me with this Date Operation Program. I think that this works....yet! Thanks #include <iostream.h> #include <conio.h> int a[7]={1,3,5,7,8,10,12}; int b[7]={4,6,9,11}; class manipdate { int date; int month; int year; public: int d,m,y; void getdata() { cout<<"\n\n Date : "; cin>>date; cout<<"\n Month : "; cin>>month; cout<<"\n Year : "; cin>>year; } void display(int d, int m, int y) { cout<<" "<<d<<" / "<<m<<" / "<<y; } manipdate sub(manipdate q) { if(q.d < 0) { q.d = -q.d; if(q.d>30) { for(int i=0;i<7;i++) { if(q.m==a[i]) { if(q.d>31) { q.d -= 31; q.m--; } } else if(q.m==b[i]) { q.d -= 30; …

Member Avatar
Member Avatar
+0 forum 5

Hi -i've started using netbeans and compiling in it recently and I'm not sure what the problem is here. Not sure if it isn't reading the Element.cpp/.h files while building/running the main.cpp In my main: Element a, b(5); cout<<(5+b)<< endl; Then in the .h: Element operator+(const Element&obj); And in the element.cpp: Element Element::operator+(const Element&obj){ return Element(defaultArgument + obj.defaultArgument); } defaultArgument is a double variable just so btw! The error states: **error: no match for 'operator+' in '5 + b'** - maybe just something small. Bit rusty with c++ atm.

Member Avatar
Member Avatar
+0 forum 2

Hi everyone, At the moment I'm trying to create a class that contains a DOMNode* pointer, something like this class MyDOMNode { public: MyDOMNode (DOMNode* node) : mDomNode (node) { } bool operator== (const MyDOMNode& rhs) { if (mDomNode == rhs.mDomNode) return true; return false; } bool operator!= (const MyDOMNode& rhs) { return !(mDomNode == rhs.mDomNode); } private: DOMNode* mDomNode; }; What I'm trying to do in the main program is this MyDomNode node = ... while (node != 0) { do stuff } However when I try to compile I'm getting an error "no match for operator !=" in …

Member Avatar
Member Avatar
+0 forum 7

Hi again and thanks in advance !Any help would be very appreciated cause i'm having a test tomorrow and i need some help! So i wanna have a class called Bag and a subclass Set.In bag we have 2 constructors, an insert function , getsize function , getsum (which adds all elements of the bag) , and contains which finds if an element is in the bag or not. In the subclass is only given that a set cannot have twice the same element i've made 2 classes but i don't know if they are correct. And finally i have …

Member Avatar
Member Avatar
+0 forum 2

Hello. I need to create a program to manipulate polynomials. It contains basic evaluation, addition, subtraction and multiplication of polynomials. Right now, I'm working on overloading the '+' operator to add two polynomials. While the function adds the two polynomials together correctly, when it goes to output through the overloaded '<<' operator, it always produces the following output: ![Debug1](/attachments/large/3/Debug1.JPG "Debug1") What could be wrong? I tested the '<<' overload within the '+' operator and it outputs the two polynomials added correctly. Below is my code: Polynomial header: #include <iostream> using namespace std; class Polynomial { friend ostream& operator<<(ostream&, const Polynomial&); …

Member Avatar
Member Avatar
+0 forum 1

Hello friends...I'm having confusion about binary operator overloading in the following program. The question goes like this. Qn. Create a class time with two member variables as hours and minutes of integer type,write default,parameterized and copy constructor. Overload necessary operators to compute T3=5+T1+T2, where T1,T2 and T3 are time objects. And the constant 5 is to be added to 'hours' member only. For this i've created two friend function to overload the operators and the code is as given below. But its not working, It would be very much helpful if u could provide me with the logic behind this …

Member Avatar
Member Avatar
+0 forum 1

I can't figure out what this error is saying. Error: C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp||In function 'bool operator==(const statistic&, const statistic&)':| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|225|error: passing 'const statistic' as 'this' argument of 'float statistic::sum()' discards qualifiers| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|225|error: passing 'const statistic' as 'this' argument of 'float statistic::sum()' discards qualifiers| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|226|error: passing 'const statistic' as 'this' argument of 'int statistic::length()' discards qualifiers| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|226|error: passing 'const statistic' as 'this' argument of 'int statistic::length()' discards qualifiers| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|227|error: passing 'const statistic' as 'this' argument of 'float statistic::max()' discards qualifiers| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|227|error: passing 'const statistic' as 'this' argument of 'float statistic::max()' discards qualifiers| C:\Users\Kyle\School\CS 372\Statistician\Statistician.cpp|228|error: passing 'const …

Member Avatar
Member Avatar
+0 forum 4

im trying to overload the operator+= and use that to implement operator+ in my string class. i keep getting a segmentation fault when i try to evaluate something like s1 += s2 where MyString s1("");and MyString s2("hello"); any suggestions as to why this is happening? this is my operator+= function [CODE] MyString operator+=(const MyString& s1, const MyString& s2) { MyString temp; delete[] temp.data; temp.len = s1.len + s2.len; temp.data = new char[temp.len + 1]; strcpy(temp.data, s1.data); strcat(temp.data, s2.data); return temp; } [/CODE] and this is my operator+ function [CODE]MyString operator+(const MyString& s1, const MyString& s2) { MyString temp; delete [] …

Member Avatar
Member Avatar
+0 forum 3

[CODE]#include <iostream> using namespace std; class bike { public: bike () { cout << "Bike: no parameters\n"; } bike (int a) { cout << "Bike: int parameter\n"; } void color(int a,int b) { cout<<"Inside Bike Color"<<endl; } }; class ducati : public bike { public: ducati (int a) { cout << "Ducati: int parameter\n\n"; } void color(int a,int b,int c) { cout<<"Inside Ducati Color"<<endl; } }; class honda : public bike { public: honda (int a) : bike (a) { cout << "honda: int parameter\n\n"; } }; int main () { ducati monster(0); ducati *dObj=new ducati(10); bike *bObj=&monster; bObj->color(10,20,30); //it's …

Member Avatar
Member Avatar
+0 forum 1

hello my dear friends I have read a book that was simple to learn c++,recently But in operator ovorloading threads ,I dont underestand many example for ovoreloading in another books & booklets for example : in the example below 1 // Fig. 11.3: PhoneNumber.h 2 // PhoneNumber class definition 3 #ifndef PHONENUMBER_H 4 #define PHONENUMBER_H 5 6 #include <iostream> 7 using std::ostream; 8 using std::istream; 9 10 #include <string> 11 using std::string; 12 13 class PhoneNumber 14 { 15 friend ostream &operator<<( ostream &, const PhoneNumber & ); 16 friend istream &operator>>( istream &, PhoneNumber & ); 17 private: 18 …

Member Avatar
Member Avatar
+0 forum 2

so i have this program written and i am getting a seg fault and the stack said in operator<<. I have no idea how to fix this...It is due in thirty minutes if someone can please help me asap. Here is where i think the problem lies: [CODE]#include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #include "listT.h" using namespace std; struct purchase { string description; float cost; //Overloading equality(==) operator bool operator== (purchase& otherPurchase) { if(description == otherPurchase.description) { if(cost == otherPurchase.cost) { return true; } else return false; } else return false; } //Less than(<) operator definition bool operator< …

Member Avatar
Member Avatar
+0 forum 1

So I'm trying to write a program that is due tomorrow. I am trying to overload operators and dealing with template classes. I have everything written already but keep getting this error whenever i try to compile the driver file: NOTE: proj4.cpp has #include "box.h" and box.h compiles fine, but it says box.h has errors whenever i try to compile proj4.cpp student@student-desktop:~$ g++ -c proj4.cpp box.h: In member function ‘void listT<elemType>::insert(elemType) [with elemType = Box]’: proj4.cpp:51: instantiated from here box.h:18: error: ‘bool Box::operator<(Box&)’ is private listT.cpp:63: error: within this context Here is some of the code that I think is …

Member Avatar
Member Avatar
+0 forum 4

I am doing this assignment for my C++ class, and we have to instantiate 4 objects of the Student class, set the data members and then sort them according to the GPA variable. Ive whittled down the errors to just one, and I am stumped. Here is the error: error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Student' (or there is no acceptable conversion) It happens in the for-loop at the end of main... anyone please help? [CODE]#include <iostream> #include <string> #include <algorithm> using namespace std; class Student { private: int stuID; string …

Member Avatar
Member Avatar
+0 forum 5

Hi, I need to finish a assignment on operator overloading but this one piece of code doesnt seem to make sense.. in my int main() i have int main() { Element a, b(5), c(7), d; cout << (5+b) << endl; this is just part of it, I have already implemented the overloading functions but from my understanding the overloading operators of class works eg: a + b would be a.operator+(b) but then how can 5+b be 5.operator+(b) since 5 is a int. Please can same one help? Thanks

Member Avatar
Member Avatar
+0 forum 7

Hey guys! I hope what I'm about to do is not frowned upon :) I just would like some input/critics/heads up on my class definition and implementation I did for my project. I am by no means a trained programmer and I doubt my coding style is very good so I thought it might be a good idea to get some inputs on something I put some time and effort in to create with my best knowledge. I created a class PNVector that allows to me to create an N-sized vector on which I overloaded several operations. Apart from an …

Member Avatar
Member Avatar
+0 forum 6

I'm reading a big csv file into a data structure defined as such: [CODE]struct PriceInfo { double Open; double High; double Low; double Close; unsigned int Volume; unsigned int Time; std::string Date; };[/CODE] So somewhere in my main function, I have this line: // read contents of datafile by overloading the input operator into a vector of type <PriceInfo> called prices [CODE]while (! dataFile.eof()) { dataFile >> prices }[/CODE] The csv file is comma delineated, so I use an extra function to do the parsing, beyond a template and the overloading function (with the following header std::istream& operator>>(std::istream& istream, PriceInfo& …

Member Avatar
Member Avatar
+0 forum 11

Hi, I am working on an exercise in operator overloading. I've created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently. My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter). Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think). However, I thought that …

Member Avatar
Member Avatar
+0 forum 2

Is it possible to create operator overloads for non-objects (like typedef-ed arrays)?

Member Avatar
Member Avatar
+0 forum 3

Is it possible to overload an operator for a built-in type? And if so what is it's syntax? EG: [CODE]//Use of overloaded operator^ for types double and double (assuming it is overloaded for exponent): double a=10.0; double b=2.5; double pow=a^b;//pow is approximately 316.22....[/CODE]

Member Avatar
Member Avatar
+0 forum 1

Hello everyone, I am having some problems with my code and allowing it to do what I want. I need within main to have the account balance from my class to be allowed to change value through "transactions" adding negative numbers for a withdraw and positive numbers to put in a deposit within the accounts balance. I want to do this an unlimited amount of times and I cannot seem to get the loop for it to work correctly. Here is my code: The problem happens near the bottom. [CODE] // Case 2 Chapter 9 #include <iostream> #include <iomanip> using …

Member Avatar
Member Avatar
+0 forum 5

This is an answer to a question in C++ area. [url]http://www.daniweb.com/software-development/cpp/threads/355232[/url] It should be ready to be modified to code in C++, C#, or Java, because VB.Net is fully OOP. Even though it can be used for vectors, you may still want to modify it to a Vector Class.

Member Avatar
Member Avatar
+0 forum 2

I am working on making a 'safetype' abstract class, which will have a complete set of pure virtual operator overloads to help make sure that a class inherited from it is safe to pass to virtual any function. The problem is that I don't know the syntax of a pure virtual operator overload or if any of the operators are unable to be virtualized. Can somebody give me an example of how to write a pure virtual operator, as well as list any operators that cannot be made purely virtual?

Member Avatar
Member Avatar
+0 forum 2

The End.