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.
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 want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable
The question is: Define the class Counter. An instance of this class is used to count things, but the counter should never be less than 0 (non negative number). The member variable should be private. I realize what I'm suppose to be using but can't implement the member functions needed..
int main(){ int value; cin >> value; Counter myCounter(value); for (int i = 1; i <= MAXLOOP; i++) { myCounter.increment();
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?
I have a class called Book and I am trying to create a dynamic pointer based array of the class. When I try to run the program I keep getting the error: pointer being freed was not allocated at the line of code that says "delete [] A;". I am using Xcode to run the program.
Book *changeArraySize(Book *A, int &size, double factor) { int i; int newsize = size*factor; Book *A2 = new Book[newsize];
if possible i want avoid the '&' when i assign the variable address.(variant2 f=varname;//like you see i don't use the '&') for the moment i just need put the address to Variant pointer. but i receive several errors .
when i compile the following program i get a compiler warning, but i don't understand why. for me the code seems to be all right and does legitimate this warning. so here is the code
The log file gives me: In function ‘memFileAlloc’ assignment makes pointer from integer without a cast..When compiling the drivers for the Matrox card in the DL580. The offending code is:
How can you actually create private fields in a C struct? I got the concept of VTable and inheriting methods aswell overriding them, but private variables can be obtained with static keyword in sourcefile. If you have variables in a struct, you can access them as soon as you got a ref to the struct. The functions are easy:
Code:
struct classA { pointerToFunction *p; } static void* thePrivateFoo() { } void* publicFoo() { thePrivateFoo(); /* or something like that */ } ... /*in init code somewhere in the c file */ classAInstance->p = publicFoo;
But I was thinking about the variables... How is this achievable? I was thinking of a struct with only get/set functions and with no datamembers at all. All vars to be static outside the struct. But this kind of destroys the encapsulation.
For now I've done a function that creates menus and prints them, and a function that creates the character as an object.
Now I want to be able to show the stats of the player on the main menu, the problem is that I don't know how to make a copy of the map as it's private...
I've created a class called Voter with a private member variable ID, also I have a variable in my main function to be ID as well. I'm trying to compare the two ID's but when I do so:
if (ID == V.ID)
I get the error - 'std::string Voter::ID' is private within this context.
I know that because it's private I can't access it, but how do I?
I have a this program of storing students in a 2-3-4 tree. I have a template class called hw6_234tree and another class called Student. Now my private member variable (m_root) keeps being reinitialized every time I process a new line. For example the first student I run through my m_root->A = Student A...(m_root is a private variable of type Node* inside of the 2-3-4tree class).The next pass through now my m_root->A == Student B. So essentially my first run through m_root->A = Student A. Then the next run m_root->A = Student B. My question is how can I keep this variable the same and stop it from reinitializing m_root->A to student B.
So I have an ImageManager class, Board class, and Box class. In Board.h I can declare ImageManager imgr; and in Board's constructor I can use imgr and its functions and such. However, in Box.h when I try and declare ImageManager imgr; I get the error "cannot access member declared in class ImageManager". Both declarations are under private, and exactly the same, but one doesn't work. Also, is there a way to only have one instance of ImageManager?