By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Assignment error with user defined vector container

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

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;
}
Nov 12 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
if you remove
DailyTemp* dummy1;

and define dummy1 so
vector<DailyTemp>::iterator found, dummy1;

it should be Ok
Nov 12 '06 #2

P: 2
if you remove
DailyTemp* dummy1;

and define dummy1 so
vector<DailyTemp>::iterator found, dummy1;

it should be Ok

But could you please tell me why the reported issue occurs? I am interested to know the reason and want the code that way.. meaning dummy1 to be of type DailyTemp.

DailyTemp* dummy1;
and assign the found member to this variable.
Nov 12 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.