I'm doing a refresher for C++ and have gotten to operator overloading. I'm trying to perform an operator overload with the insertion (<<) operator, but I have encountered a problem.
Here's my class [In a header file "Shinigami.h"]
#include<string> namespace K{ class Quincy; class Shinigami{ friend std::ostream& operator<<(std::ostream&, const Shinigami&);
[Code] .....
If the operator function is a friend of the 'Shinigami' class, why doesn't it recognize any of it's private members? I need it to be in this file because I'm doing a bit of association with the 'Quincy' class.
I thought it was the namespace, but I included that.
Say I have a class that requires dynamic allocation to implement a few of the operators. Take "+" for example; I need to create a new object to hold the sum of the two parameters whose size is not known at compile time.
I'm pretty sure the standard way to indicate a failure inside the overloading function would be to throw an exception. However I am currently involved in an embedded(ish) project where the spec. says no exceptions are to be used.
I think I have 2 options:
1. Return an "invalid" object (with a flag indicating an error has occurred) and check for this after each operation.
a = b + c if (a.err) // handle error or
2. To forsake operator overloading entirely and think up a new way of doing things where all functions that involve dynamic allocation can return error codes. but this seems rather terrible too as I may end up with something like:
objA a if (add(&a, b, c) == -1) // assuming b and c are initialized before this snippet starts // handle error
Is there a number 3 that I haven't thought of? It seems that not allowing exceptions is fairly common even in the non-embedded world [URL] so how is this normally done? or is operator overloading usually avoided when exceptions are not allowed?
We're trying to overload the delete[] operator with specific arguments. Which is the right way to call it? We use the GNU compiler and obtain compiler errors with all of these samples:
#include<iostream> using namespace std; typedef unsigned int P;
I'm trying to come up with the union of two Vector ADT bags, so I have to overload the '+' operator, but I'm getting a bunch of error messages saying:
VectorBag.cpp: In instantiation of ‘VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType>) [with ItemType = int]’: proj2.cpp:161:42: required from here VectorBag.cpp:81:24: error: no match for ‘operator[]’ (operand types are ‘VectorBag<int>’ and ‘int’) newBag.add(anotherBag[i]); ^ Here is the function to overload the operator:
template<class ItemType> VectorBag<ItemType> VectorBag<ItemType>::operator+(VectorBag<ItemType> anotherBag) { VectorBag<ItemType> newBag; for (int i = 0; i < anotherBag.getCurrentSize(); i++) newBag.add(anotherBag[i]); }
The add() function is pre-defined by me somewhere else in the code. It basically does push_back().
I want to overload prefix and postfix increment(++) operators with friend function. I also have to use the constructors for this. How can I do this? in C++
i am doing some practice problems and i can't seem to figure out how to do this. basically we have a students number of test scores, then the name followed by the scores they have in a text file. Then we have to make a class with a constructor, copy constructor, destructor, and overload the = operator and the input and output operator. Are we suppose to call the text file in the input overload operator?
Here is what i have so far.
This is my header file.
#ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> #include <iostream> using namespace std; class StudentTestScores{ private: string studentName;
[Code]...
i am 100% sure the overloading the input is wrong
here is the implementation of the constructor copy constructor and desctructor
#include <iostream> #include "StudentTestScores.h" using namespace std; StudentTestScores::StudentTestScores(string name = "", int numScores = 0) { studentName = name; numTestScores = numScores; if (numScores <= 0) testScores = NULL; else
[Code]...
and here is the notepad file
3 Justin Bieber491.469.184.681.081.5 Miley Cyrus380.080.090.083.3 Kim K490.575.661.481.677.2
The program is suppose to use all the information and read from the notepad and output the exact things as the notepad file
I have code here that uses assignment operators that doesn't return by reference and it still works. So why does my book say you need to return by reference?
Here is a quote from my book:
The return type of operator= is a reference to the invoking object, so as to allow chained assignments a=b=c.
The code below is from my book. I simply removed '&', in the original code that has assignment operators return by reference, from IntCell & operator=. This way the assignment operator no longer returns a reference, and it still works.
#include <iostream> using namespace std; class IntCell { public: explicit IntCell( int initialValue = 0 ) { storedValue = new int{ initialValue }; }
I am wondering why return type for an assignment operator cant be a void or int? Cant I write assignment operator for student class like this as we do nothing with returned value?
Student { char name[20]; int marks; public: student(char*name,int marks)
The task is to use the assignment operator of a class, but change all the data except certain ones. For example, below we are to assign all Person data of 'other' except for 'name' and 'ID':
#include <iostream> #include <string> struct Person { std::string name; int ID, age, height, weight;
[Code] .....
Name = Bob ID = 2047 Age = 38 Height = 183 Weight = 170
Name = Frank ID = 5025 Age = 25 Height = 190 Weight = 205
Bob pretends to be Frank, but keeps his name and ID.
Name = Bob ID = 2047 Age = 25 Height = 190 Weight = 205
But I think the way I did it is pretty lousy (note the wasted steps changing the name and ID only to revert them back? So the ideal solution should require no wasted steps, unlike the method above, and changes to what the exclusions should be should be in only one place (not two like above). Of course, we assume that Person shall have many, many data members (and constantly increasing), so that simply defining Person::operator= (const Person& other) to handle all data except for 'name' and 'ID' is out of the question.
How do i write main test program to test the copy constructor and assignment operator in this program...how do i know if its working as its suppose to?i just want to know about copy and assignment operator..i have figured out the test program for other things..Here my program :
I've been working on some project and I got to wondering when you know you need to use a copy constructor and an assignment operator. Is there a rule of thumb? I know there is the Rule of Three, but is there something that tells you when you need those three?
Consider the class specification below. Write the prototype (i.e. header) of a member function to overload the insertion operator (i.e. <<). The << operator is to output the data members of an instance of class StudentTestScores into an output stream. Your definition should allow for chaining of output operations (e.g. cout << x << y; where x and y are of type StduentTestScires).
#include <string> using namespace std; class StudentTestScores{ private: string studentName; float *testScores; // used to point to an array of test scores int numTestScores; // number of test scores
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:
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!
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.
I am working on a double linked list and inside of my function to insert a node, I am getting an error of "Incompatible types in assignment". Here is my function code. Line 55 is where I am receiving the error.
I am getting this error when compiling my program with quincy:
Error: I value required as left operand of assignment
The program is meant to calculate how much parking costs based on the amount of hours in a park and what type of vehicle it is. the error is coming from my function definitions which i have just started to add in.
Code: float calcCarCost (char vehicletype, int time, float car) { if ((time > MINTIME) && (time <= 3)) calcCarCost =( CAR * time ); }
The error is on line 72 which is: calcCarCost =( Car * time);
I should probably point out CAR is already defined as a constant with a numerical value given and time is previously asked to be input in when the program runs.