Hi,
Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error:

cannot convert `__gnu_cxx::__normal_iterator<DailyTemp*,
std::vector<DailyTemp, std::allocator<DailyTemp> > >' to `DailyTemp*'
in assignment

I believe the overloaded assignment operation is unable to recognize the iterator. Can anyone help me to over come this issue?

Thanks.

// Store a class object in a vector.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;

class DailyTemp {
double temp;
public:

//constructors
DailyTemp() { temp = 0; }
DailyTemp(double x) { temp = x; }

//assignment
DailyTemp &operator=(DailyTemp& x) {
temp = x.get_temp(); return *this;
}

///member functions
double get_temp() { return temp; }

};
bool operator<(DailyTemp a, DailyTemp b)
{
return a.get_temp() < b.get_temp();
}

bool operator==(DailyTemp a, DailyTemp b)
{
return (a.get_temp() == b.get_temp());
}

//Main routine
int main()
{

vector<DailyTemp> *v =new vector<DailyTemp>();

int i;
int search = 70;

DailyTemp dummy(78);
DailyTemp* dummy1;

for(i=0; i<7; i++)
v->push_back(DailyTemp(60 + rand()%30));


cout << "Farenheit temperatures:\n";

for(i=0; i<v->size(); i++)
cout << ((*v)[i]).get_temp() << " ";

cout << endl;

//Finding an entry in the vector
vector<DailyTemp>::iterator found;

found = find(v->begin(),v->end(),dummy);

if(found == v->end())
cout<<search<< " NOT FOUND"<<endl;
else
{
cout<<"found: "<<(*found).get_temp()<<endl;
dummy1 = found; //<<ERROR: Assignment fails with g++ >>

cout<<"found: "<<dummy1->get_temp()<<endl;
}

double result;
// convert from Farenheit to Centigrade
cout<<endl;
for(i=0; i<v->size(); i++)
{
result = ((*v)[i].get_temp()-32) * 5/9 ;
//cout <<result;
DailyTemp result1(result);
((*v)[i]) = result1;

}
cout<<endl;
cout << "Centigrade temperatures:\n";

for(i=0; i<v->size(); i++)
cout << (*v)[i].get_temp() << " ";


system("PAUSE");
return 0;
}


_______________________________________________
help-gplusplus mailing list
help-gplusplus@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gplusplus

Reply via email to