I was trying to convert one class type data to another class type therefore I was using casting operator method in my oops program in c++.
class example2;
class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id=a;
numbers=b;
cost=c;
}
// getid() added here
// getnumber() added here
// getcost() added here
operator example2()
{
example2 temp;
temp.id=id;
temp.value=numbers*cost;
return temp;
}
};
class example2
{
int id;
int value;
public:
example2(int x,int y){
id=x;
value=y;
}
void display(){
cout<<"id "<<id<<endl;
cout<<"value "<<value<<endl;
}
};
when I was using casting from example1 to example2. It is showing error.
int main()
{
example1 s1(100,5,140.0);
example2 s2;
s2=s1;
s2.display();
return 0;
}
it is giving error but why?. I made an operator overloading member function in example1 class because object of example1 has to be changed into object of example2.So this function invoked from class example1's method only which I think.It should be correct
Error was like:
error: return type 'class example2' is incomplete and example2 temp; has incomplete type
Somehow I resolved this in another way I added a constructor in example2 class side:
example2(example1 e)
{
id=e.getid(); //these functions already added in my code i didnt mentioned them in here.
value=e.getnumber()*e.getcost();
}
and make comment of 'operator example2()' part in example1. now it is working. But previous way was not accepting. Please help me to correct me in my previous way of doing this thing.
One of the classes must be defined first and the second cannot fully be used until after it is defined. This means you have to break up the class definitions a bit.
Here is an example of how to get at the class that's not yet defined, based off of the OP's post. Explanation comes in the form of comments embedded in the code to keep the code all in one cut-n-pasteable chunk.
#include <iostream>
class example2; // forward declaration to satisfy compiler until example2
// is defined
class example1
{
int id;
int numbers;
int cost;
public:
example1(int a, int b, int c)
{
id = a;
numbers = b;
cost = c;
}
example1(const example2 & e) // the const and reference are just because
// no point copying e, and const ensures no
// side effects to e
{
*this = e; // why duplicate code? Just calling operator=
}
example1& operator=(const example2 & e);
// note the lack of an implementation. This is because at this point
// the compiler only knows example2 exists, but not what it looks
// like. Can't copy what what you haven't seen.
int getid() const //const to allow me to use const references.
{
return id;
}
int getnumber() const
{
return numbers;
}
int getcost() const
{
return cost;
}
void display()
{
std::cout << "Example 1" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "numbers " << numbers << std::endl;
std::cout << "cost " << cost << std::endl;
}
};
class example2
{
int id;
int value;
public:
example2(int x, int y)
{
id = x;
value = y;
}
example2(const example1 &e)
{
*this = e; // once again just calls the equals operator
}
example2 & operator=(const example1 & e) // OK. This time we know what
// example1 looks like and can
// actually implement the method
{
id = e.getid();
value = e.getnumber() * e.getcost();
return *this;
}
int getid() const
{
return id;
}
int getvalue()const
{
return value;
}
void display()
{
std::cout << "Example 2" << std::endl;
std::cout << "id " << id << std::endl;
std::cout << "value " << value << std::endl;
}
};
// and now for the implementation of example1's equals operator
example1& example1::operator=(const example2 & e)
{
id = e.getid();
numbers = -1; //do real work to get cost and numbers from e.getvalue()
cost = -1;
return *this;
}
int main()
{
example1 s1(100, 5, 140.0);
example2 s2(1, 2);
s2 = s1;
s2.display();
example2 s3(314, 278);
s1 = s3;
s1.display();
return 0;
}
Now for overloading the cast operator. This is something you almost never want to do because there are almost always much more obvious ways to accomplish the same goal.
For example:
#include<iostream>
class Integer
{
public:
Integer(int val):mVal(val)
{
}
operator int()
{
return mVal;
}
int getVal()
{
return mVal;
}
private:
int mVal;
};
int main()
{
Integer t(42);
int x = (int)t; // We're turning the Integer into an int? Kinda makes sense.
std::cout << x << std::endl;
x = t.getVal(); // hey! we're getting the integer's value! Do I need this comment?
std::cout << x << std::endl;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments