Help!! some issues with creating a prime class

The ++ operator just can't overload. I want to know what is going on. Every time i cin a number, it only gives the origin one back to me. Thanks for your help!!

class PrimeNumber
{
public:
PrimeNumber();
PrimeNumber(int ininum1);
int getprime();
PrimeNumber& operator ++ () ;
PrimeNumber& operator -- () ;
bool isprime(int ininum1);
friend ostream& operator <<(ostream& river, const PrimeNumber& ini);
private:
int ininum;
};

PrimeNumber::PrimeNumber()
{
ininum = 2;
}
PrimeNumber::PrimeNumber(int ininum1)
{

ininum = ininum1;
}
int PrimeNumber::getprime()
{
return ininum;
}
bool PrimeNumber::isprime(int ininum1)
{
int i;

for (i=2; i<ininum1; i++)
{
if (ininum1 % i == 0)
{
return false;
}
}

return true;
}
PrimeNumber& PrimeNumber::operator ++ ()
{
bool ini = false;
int num = ininum;
while(ini = false)
{
if (!isprime(num))
{
ini = true;
return PrimeNumber();
}
else if (isprime(num))
{
num++;
while (ini = false)
{
if(isprime(num))
{
ini = true;
return PrimeNumber(num);
}
else
num++;

}
}
}
}

ostream& operator << (ostream& river, const PrimeNumber& ini)
{
river << ini.ininum;
return river;
}

int main()
{
PrimeNumber num = PrimeNumber(26);
++num;
cout << num;
system ("pause");
return 0;
}
Last edited on
Num is a class, not a number. You want to access ininum in class num. Do that with dot notation, num.ininum. So to increment and display the number you would go:

1
2
3
PrimeNumber num = PrimeNumber(26);
++num.ininum;
cout << num.ininum;


EDIT: I just noticed ininum is private so you won't be able to access it like that. You'll have to make it public or write a getter function in your class, then use dot notation to call your getter function.
Last edited on
You cannot return a reference to a local variable (or temporary) in a function without undefined behavior.

You should always return something when you promise to return something.

You really should pay attention to the warnings provided by your compiler.

= is assignment. == is comparison. The while condition(s) in your operator++ can never be true.




Topic archived. No new replies allowed.