Assignment error with user defined vector container

R

Raghuram N K

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)).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).get_temp()-32) * 5/9 ;
//cout <<result;
DailyTemp result1(result);
((*v)) = result1;

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

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


system("PAUSE");
return 0;
}
 
S

Salt_Peter

Raghuram said:
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; }

use init lists
DailyTemp(double x) { temp = x; }

//assignment
DailyTemp &operator=(DailyTemp& 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<(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>();

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(DailyTemp(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)).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++ >>


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()<<endl;
}

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

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

for(i=0; i<v->size(); i++)
cout << (*v).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(double 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==(const DailyTemp& rhv) const
{
return temp == rhv.temp;
}
};

template< typename T >
void convertFarhCenti( std::vector< T >& r_vf )
{
// convert from Farenheit to Centigrade
std::cout << "Centigrade temperatures:\n";
std::vector< DailyTemp > vcenti(r_vf.size());
for ( size_t i = 0; i < vcenti.size(); ++i )
{
vcenti = (r_vf.get() - 32.0) * 5.0 / 9.0;
std::cout << "vcentigrade[" << i << "] = ";
std::cout << vcenti.get() << std::endl;
}
}

int main()
{
std::vector< DailyTemp > vfarenheit(7);

// seed the rand() generator with computer clock
srand(static_cast<unsigned>(time(0)));

std::cout << "Farenheit temperatures:\n";
for ( size_t i = 0; i < vfarenheit.size(); ++i )
{
vfarenheit = DailyTemp( 60 + rand() % 30 );
std::cout << "vfarenheit[" << i << "] = ";
std::cout << vfarenheit.get() << std::endl;
}

//Finding an entry in the vector
const DailyTemp dummy( 78 );
typedef std::vector< DailyTemp >::const_iterator VIter;
VIter found = std::find( vfarenheit.begin(),
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
convertFarhCenti( 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
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top