•
•
•
•
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
![]() |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
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
CLASS DEF CPP
MAIN CPP (so far)
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;
}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation:
Rep Power: 38
Solved Threads: 932
>>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);
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
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.
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation:
Rep Power: 38
Solved Threads: 932
>>if ((Name,Donation,Sex,IDKey)==(rhs.Name,rhs.Donation,rhs.Sex,rhs.IDKey))
In operator ==. You don't construct if statements like that
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation:
Rep Power: 38
Solved Threads: 932
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).
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
I tried it like this and it seems to work, is it essentially the same thing?
except this way I don't think it actually writes to a text file or really anything but memory does it?
#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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation:
Rep Power: 38
Solved Threads: 932
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Rep Power: 1
Solved Threads: 1
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?
It seems to me it's fine for one or two contributors, but what about when the list expands to say 5 or more?
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,197
Reputation:
Rep Power: 38
Solved Threads: 932
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- static array in a class (C++)
- what this error mean ? Any 1 pleeese (C++)
- VC++ 6.0 errors (C++)
- Vector hell (C)
- Inserting objects into an array (C++)
- please fix the error (C++)
- Problem with object declaration (C++)
- Visual Studio 6: Initializing static class member (C++)
- A little Help (C++)
Other Threads in the C++ Forum
- Previous Thread: converting 'class here' to 'type here'
- Next Thread: Problem In Comparing.Urgent!



Linear Mode