class monomial
{
public:
	monomial(){};
	monomial(string vars); 
	// Assignment operator
	void operator= (monomial & p);
	monomial operator* ( monomial &p);
	monomial operator/ (monomial &p);
	bool operator== (  monomial &p) ;
	bool operator> (  monomial &p) ;
	bool operator< (  monomial &p) ;
	void print();
public:
	list<unsigned int> var;//variables separated by ":"
	
};

The code in main:

monomial a1("a:b:c");
	monomial a2("a:c");
	monomial a8;
	a8=a1/a2
        a8.print();
	cout<<endl;

After compilation, I run it and got an error message:
no match for ‘operator=’ in ‘a8 = monomial::operator+(monomial&)(((monomial&)(& a2)))’.
Well, when I changed the declaration of assignment

void operator= (monomial & p);

to

void operator= (monomial  p);

everything is fine.
I just cannot understand why?
I also tried it in visual studio and it works perfect.

thanks
gepo

void operator= (monomial & p);
	monomial operator* ( monomial &p);
	monomial operator/ (monomial &p);
...
        monomial a1("a:b:c");
	monomial a2("a:c");
	monomial a8;
	a8=a1/a2
        a8.print();
	cout<<endl;

The problem, i believe, is that you are trying to pass a non const monomial reference to the assignment operator. This is fine if you are passing a reference to a variable like

monomial a1("a:b:c");
	monomial a2("a:c");
	monomial a8;
        a8 = a1;

However, the value you get back from the operator / hasn't been assigned to a variable yet. You can't change the value of this monomial, because it doesn't have a permanent address yet. These values are always const. Consequently, you need to change you assignment operator to take a const monomial for an argument. In general, you should use const protection wherever it makes sense

monomial& operator= ( const monomial & p){
         var = p.var;
         return *this;
}

The assignment operator should always return a reference to the monomial being assigned, so that assignments can be chained like a=b=c=d; This is done by having the code return *this. The argument should always be const, because while assigning p to this, you should never change the values in p

monomial operator/ ( const monomial &p) const;

This function creates a new monomial, sets its value, and then returns the new monomial. The values in p should never be changed, so we pass the argument as a const monomial reference. This will also allow the operator to operate on a monomial that hasn't been saved in a variable yet like monomial a = b/c/d since c/d is a new monomial that hansn't been put in a variable yet, we would need the / operator to accept a const reference.
The second const (at the end of the function declaration) says that the function will not change the value of this monomial (the one calling the operator function). The operator will combine this and p to create a new value, but it should never change the value of this or p while doing so. The const declaration enforces this behavior.

This should fix your compile issues. In general, if you use the const declaration properly, you can make your code safer, more correct, and more compile friendly in c++. Hope this helped!

Thanks, it works. Following your instructions, I changed all parameters to const type.
Well, after that, a new problem ariose.

class monomial
{
public:
	monomial(){};
	monomial(string vars); 
	void operator= (const monomial &p);
	void operator= (const string &p);
	monomial operator* (const monomial &p);
	monomial operator/ (const monomial &p);
	bool operator== (const  monomial &p) ;
	bool operator> ( const monomial &p) ;
	bool operator< ( const monomial &p) ;
	void print();
public:
	list<unsigned int> var;
};

in function:

bool monomial::operator< (const  monomial &p)
{
	list<unsigned int>::iterator it1;
	list<unsigned int>::iterator it2;
	it1=var.begin();
	it2=p.var.begin();

	while((it1!=var.end())&&(it2!=p.var.end()))
	{
		if((*it1)<(*it2)) return true;
		if((*it1)>(*it2)) return false;
		++it1;++it2;
	}
	if(var.size()<p.var.size()) return true;
	else return false;
}

for the red line, there is an error message:
error: no match for ‘operator=’ in ‘it2 = p->monomial::var.std::list<_Tp, _Alloc>::begin [with _Tp = unsigned int, _Alloc = std::allocator<unsigned int>]()’

that is so weird, because "it1" works fine. Is that caused by const type parameter?

thanks a lot.

I just got it done.
I need use const_iterator to refer const type parameters.

thanks anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.