User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,097 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,328 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 697 | Replies: 13 | Solved
Reply
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Ambiguous Overload Resolution

  #1  
Jul 20th, 2008
Yes folks me again, but if I don't struggle through, I don't learn it. I'm constructing my main (no interactivity at this point), and when I start to declare/create my first object, it doesn't like the string portion of the input and tells me this:

error C2664: 'Contributor::Contributor(std::string,double,gender,int)' : cannot convert parameter 1 from 'Contributor *' to 'std::string'
1> No constructor could take the source type, or constructor overload resolution was ambiguous

Now I realize this points to a problem with one of my constructors (either the overload or the copy) but not sure which, nor what the problem is. Code follows:

HEADER
#pragma once
#include <string>
#include <iostream>
using namespace std;

enum gender {none,male,female};

class Contributor
{
	public:
		Contributor();
		Contributor (string Name, double Donation =0.0, gender sex=none, int IDKey=0);
		Contributor (const Contributor& CCContributor);
		~Contributor();
		friend ostream& operator << (ostream& out, const Contributor& InContributor);
		friend istream& operator >> (istream& in, Contributor& InContributor);
		Contributor& operator = (const Contributor& rhs);
		bool operator <  (const Contributor& rhs)const;
		bool operator >  (const Contributor& rhs)const;
		bool operator == (const Contributor& rhs)const;
		bool operator != (const Contributor& rhs)const;

	private:
		string Name;
		double Donation;
		int IDKey;
		gender Sex;
};

CLASS DEF CPP
#include "Contributor class.h"


Contributor::Contributor()
{
	Donation=0.0;
	Sex=none;
	IDKey=0;
}

Contributor::Contributor (std::string Name, double Donation, gender Sex, int IDKey)
{
	this->Name=Name;
	this->Donation=Donation;
	this->Sex=Sex;
	this->IDKey=IDKey;
}

		
Contributor::Contributor (const Contributor& CCContributor)
{
	Name=CCContributor.Name;
	Donation=CCContributor.Donation;
	Sex=CCContributor.Sex;
	IDKey=CCContributor.IDKey;
}

Contributor::~Contributor()
{
	cout<< "I have cleaned up yer mess" << endl;
}

ostream& operator << (ostream& out, const Contributor& InContributor)
{
	out << "The Contributor's information is: " << endl << endl;
	out << " Name: " << InContributor.Name << endl;
	out << " Donation: " << InContributor.Donation << endl;
	out << " Sex: ";
		switch (InContributor.Sex)
		{
			case male: out << " Male "; break;
			case female: out << " Female "; break;
			default: out << " No information available ";
		}
		out << endl;
		return out;

	out << " IDKey: " << InContributor.IDKey << endl;
}

istream& operator >> (istream& in, Contributor& InContributor)
{
	int sel=0;
	in.clear();
	in.ignore(in.rdbuf()->in_avail(),'\n');
	
	cout << "Please enter the name of the contributor: "<<endl;
	getline (in, InContributor.Name);

	cout << "Please enter the amount of the donation from the contributor: "<<endl;
	in >> InContributor.Donation;

	cout << "Please enter number of the sex of the contributor: "<<endl << endl;
		cout << "1 = The Contributor is male"<<endl;
		cout << "2 = The Contributor is female "<<endl;
	in >> sel;

	switch (sel)
	{
		case 1: InContributor.Sex = male; break;
		case 2: InContributor.Sex = female; break;
		default: InContributor.Sex = none; break;
	}

	cout << "Please enter the ID Key of the contributor: "<<endl;
	in >> InContributor.IDKey;

	return in;
}

Contributor& Contributor::operator = (const Contributor& rhs)
{
	if (&rhs != this)
	{
		this->Name=Name;
		this->Donation=Donation;
		this->Sex=Sex;
		this->IDKey=IDKey;
	}
	return *this;
}
		
bool Contributor::operator <  (const Contributor& rhs)const
{
	if (Name < rhs.Name)
		return true;
	else
		return false;
}

bool Contributor::operator >  (const Contributor& rhs)const
{
	if (Name < rhs.Name)
		return true;
	else
		return false;
}

bool Contributor::operator == (const Contributor& rhs)const
{
	if ((Name,Donation,Sex,IDKey)==(rhs.Name,rhs.Donation,rhs.Sex,rhs.IDKey))
		return true;
	else
		return false;
}

bool Contributor::operator != (const Contributor& rhs)const
{
	return ! (*this == rhs);
}

MAIN CPP (so far)
#include "Contributor Class.h"

int main()
{
	Contributor *Jerry;
	Jerry = new Contributor(Jerry,500.00,male,1);

	
	return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 932
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Ambiguous Overload Resolution

  #2  
Jul 20th, 2008
>>Contributor (string Name, double Donation =0.0, gender sex=none, int IDKey=0);

>>Jerry = new Contributor(Jerry,500.00,male,1);

What you are passing in main() is a pointer to a Conmtributor object (named Jerry). What it wants is a std::string that contains the name of a person, for example put quotes around Jerry in the first parameter

Jerry = new Contributor("Jerry",500.00,male,1);
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Ambiguous Overload Resolution

  #3  
Jul 20th, 2008
yep, I was just coming back to say I caught that...I'm an idiot...but while we're here...how do I call the ostream function for the interactivity portion? That part boggles me a tad also.

I ask because what I'm trying to do is provide at least two "premade" contributors, and then using the friend ostream function, ask the user for a couple more.
Last edited by henpecked1 : Jul 20th, 2008 at 3:00 pm. Reason: additional info
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 932
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Ambiguous Overload Resolution

  #4  
Jul 20th, 2008
>>if ((Name,Donation,Sex,IDKey)==(rhs.Name,rhs.Donation,rhs.Sex,rhs.IDKey))

In operator ==. You don't construct if statements like that

if( (Name == rhs.Name) && (Donation == rhs.Donation) && // you get the idea?
Last edited by Ancient Dragon : Jul 20th, 2008 at 3:01 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Ambiguous Overload Resolution

  #5  
Jul 20th, 2008
Okay, understand that one, much clearer, and looks much cleaner too.

okay, now to the istream and ostream functions, how do I call those in main for the interactivity portion?
Last edited by henpecked1 : Jul 20th, 2008 at 3:35 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 932
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Ambiguous Overload Resolution

  #6  
Jul 20th, 2008
I'm not certain which functions you mean. The two friend functions?

With the below you have to write the two friend functions so that they read/write the class's data objects from/to data file. That's called serializing (don't ask me why it has such a wierd name because I don't know).

int main()
{
     Contributor Jerry;

     ifstream in("infile.txt");
     infile >> Jerry;

     ofstream out("infile.txt");
     outfile << Jerry;
}
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Ambiguous Overload Resolution

  #7  
Jul 20th, 2008
I tried it like this and it seems to work, is it essentially the same thing?

#include "Contributor Class.h"

int main()
{
	Contributor Cont1;
	cin >> Cont1;
	cout << Cont1 << endl;
		
	return 0;
}

except this way I don't think it actually writes to a text file or really anything but memory does it?
Last edited by henpecked1 : Jul 20th, 2008 at 4:52 pm. Reason: added comments
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 932
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Ambiguous Overload Resolution

  #8  
Jul 20th, 2008
I think that will work.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: Ambiguous Overload Resolution

  #9  
Jul 20th, 2008
Could the function definition I wrote for "<" (and others) be used to construct an ordered linked list or should it be rewritten somehow to accomodate that use (looking ahead to my next lab)?

It seems to me it's fine for one or two contributors, but what about when the list expands to say 5 or more?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 932
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Ambiguous Overload Resolution

  #10  
Jul 20th, 2008
use vectors instead of rolling your own linked list, unless of course your instructor says you must code your own. But yes, the overload < operator could be used to construct a linked list in sorted order.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC