Raghuram N K wrote:
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::__n ormal_iterator< DailyTemp*,
std::vector<Dai lyTemp, 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; }
use init lists
DailyTemp(doubl e x) { temp = x; }
//assignment
DailyTemp &operator=(Dail yTemp& x) {
constantness is not an option
DailyTemp& operator=(const DailyTemp& x) {
temp = x.get_temp(); return *this;
}
///member functions
double get_temp() { return temp; }
again, const
double get() const { ... }
or
const double& get() const { ... }
>
};
bool operator<(Daily Temp a, DailyTemp b)
{
return a.get_temp() < b.get_temp();
}
bool operator==(Dail yTemp a, DailyTemp b)
{
return (a.get_temp() == b.get_temp());
}
//Main routine
int main()
{
vector<DailyTem p*v =new vector<DailyTem p>();
Sorry, i hate pointers and a new allocation is not needed for this
program.
>
int i;
int search = 70;
search should be 78
>
DailyTemp dummy(78);
That variable should be const
DailyTemp* dummy1;
not needed
>
for(i=0; i<7; i++)
v->push_back(Dail yTemp(60 + rand()%30));
you need to "seed" the rand() or the same number sequence gets
generated (see code below).
>
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<DailyTem p>::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++ >>
found is an iterator, deference it with *found.
dummy1 is a pointer (why aren't you calling it p_dummy?
A pointer is not an object - commit that the eternal memory. Its
crucial.
DailyTemp temp;
DailyTemp* p_dummy = &temp;
*p_dummy = *found; // should work.
Damned be the compilers that allow accessing an uninitialized pointer.
>
cout<<"found: "<<dummy1->get_temp()<<en dl;
}
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;
}
For the sake of simplicity, i'm letting the farhenheit computations
remain as integers for the sake of finding basic fahrenheit temps. The
Centigrade temps are doubles (ie: 78.0, not 78).
Look - no pointers.
#include <iostream>
#include <vector>
class DailyTemp {
double temp;
public:
DailyTemp() : temp(0.0) { } // def ctor
DailyTemp(doubl e d) : temp(d) { } // parametized ctor
// copy ctor
DailyTemp(const DailyTemp& copy) { temp = copy.temp; }
// assignment op
DailyTemp& operator=(const DailyTemp& rhv)
{
if(&rhv == this) return *this;
temp = rhv.temp;
return *this;
}
/* member functions */
double get() const { return temp; }
/* operators */
bool operator<(const DailyTemp& rhv) const
{
return temp < rhv.temp;
}
bool operator==(cons t DailyTemp& rhv) const
{
return temp == rhv.temp;
}
};
template< typename T >
void convertFarhCent i( std::vector< T >& r_vf )
{
// convert from Farenheit to Centigrade
std::cout << "Centigrade temperatures:\n ";
std::vector< DailyTemp vcenti(r_vf.siz e());
for ( size_t i = 0; i < vcenti.size(); ++i )
{
vcenti[i] = (r_vf[i].get() - 32.0) * 5.0 / 9.0;
std::cout << "vcentigrad e[" << i << "] = ";
std::cout << vcenti[i].get() << std::endl;
}
}
int main()
{
std::vector< DailyTemp vfarenheit(7);
// seed the rand() generator with computer clock
srand(static_ca st<unsigned>(ti me(0)));
std::cout << "Farenheit temperatures:\n ";
for ( size_t i = 0; i < vfarenheit.size (); ++i )
{
vfarenheit[i] = DailyTemp( 60 + rand() % 30 );
std::cout << "vfarenheit[" << i << "] = ";
std::cout << vfarenheit[i].get() << std::endl;
}
//Finding an entry in the vector
const DailyTemp dummy( 78 );
typedef std::vector< DailyTemp >::const_iterat or VIter;
VIter found = std::find( vfarenheit.begi n(),
vfarenheit.end( ),
dummy );
std::cout << "searching..\n" ;
if ( found == vfarenheit.end( ) ) {
std::cout << dummy.get() << " NOT FOUND" << std::endl;
}
else {
std::cout << "found: " << (*found).get() << std::endl;
}
// convert from Farenheit to Centigrade
convertFarhCent i( vfarenheit );
return 0;
}
/*
Farenheit temperatures:
vfarenheit[0] = 77
vfarenheit[1] = 73
vfarenheit[2] = 81
vfarenheit[3] = 88
vfarenheit[4] = 67
vfarenheit[5] = 64
vfarenheit[6] = 78
searching..
found: 78
Centigrade temperatures:
vcentigrade[0] = 25
vcentigrade[1] = 22.7778
vcentigrade[2] = 27.2222
vcentigrade[3] = 31.1111
vcentigrade[4] = 19.4444
vcentigrade[5] = 17.7778
vcentigrade[6] = 25.5556
*/