473,661 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assignment error with user defined vector container

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; }
DailyTemp(doubl e x) { temp = x; }

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

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

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

int i;
int search = 70;

DailyTemp dummy(78);
DailyTemp* dummy1;

for(i=0; i<7; i++)
v->push_back(Dail yTemp(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<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++ >>

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;
}

Nov 13 '06 #1
1 3955

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
*/

Nov 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12734
by: Earl Anderson | last post by:
In A97 on WinXP, I'm trying to use a KB function to provide the next sequential number in a custom counter (a table with two fields-- and ). When I run it, I get the "User-defined type not defined" error on the third line of code (Dim rs As ADODB.Recordset) I'm using below: " Function Next_Custom_Counter() On Error GoTo Next_Custom_Counter_Err Dim rs As ADODB.Recordset
3
2955
by: Regnab | last post by:
Despite having at least 2 days VBA programming experience, this one has got me... I'm trying to export multiple tables into one worksheet in Excel. I've adapted code that I've got from this forum, but I keep getting the "User-Defined type not defined" error on 'Dim objXL As Excel.Application'. Again, from my limited time as a veteran copier, paster and editor, I've had this problem before when using a browse window - which was fixed by...
2
1352
by: Soofy | last post by:
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.
3
12057
by: blakerrr | last post by:
Hi everyone, I am trying to export a table to an excel file using vba on a form's button click event. I am getting the error: Compile error: User-defined type not defined. And it highlights my first line: Dim appExcel As Excel.Application Any ideas? I am using Access 2003, but I am using Access 2000 file format. Does this have to with DAO and ADO something-or-others? I'm a beginner with all of this database stuff so please...
13
9060
by: forrestgump | last post by:
I am currently trying to use the below VBA to import information into excel from access. This VBA is in the excle sheet:- Public Sub getrs() Dim adoconn As ADODB.Connection Dim adors As ADODB.Recordset Dim sql As String Dim filenm As String sql = "Select * from Table1" filenm = "R:\HR\HR_System_Reports_Folder\Databases\HeadCount.mdb" Call GetCn(adoconn, adors, sql, filenm, "", "")
4
5662
by: hung52 | last post by:
Hi experts, I try to send a mail with attachment using cdosys.dll. Dim iCfg As CDO.Configuration Dim iMsg As CDO.Message Set iCfg = New CDO.Configuration ... the compile got error user-defined type not defined on New CDO.Configuration
4
1850
by: nani2717 | last post by:
HI ALL, I m currenly working with a visual basic 6 project.i m new to VB.wen i tried to comile a file i got the error "user defined type not defined".i don know wat appropriate reference to add.i m enclosing the code here. Private Sub Form_Load() On Error Resume Next tries = 1 Load frmDialog Set FRM = frmDialog AutoRedraw = LoadResString(107)
6
19928
by: travjbad1 | last post by:
I am new to the forum and new to Access, so please be simple and descriptive if possible. I am having a problem with a button on a form that saves, opens a report in pdf, and emails the report to the client. I believe the email is what is failing. I get the Error message "Compile Error: User-defined type not defined" and this is the section of code that pops up: Public Function SendClientEmail(emailto As Variant, attachmentpath As Variant,...
0
1379
by: usharani K | last post by:
Urgent Help !!!!!!!!!!!while compiling the code I am getting the compile error: Compile error: User-defined type not defined in form_load. ---------------------------- Private Sub Form_Load() Dim mypanel As Panel StatusBar1.Panels.Clear ----------------------------
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8341
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2760
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.