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

Recommended Answers

All 13 Replies

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

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.

>>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?

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?

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 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?

I think that will work.

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?

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.

yes, he's going to make us "roll our own" linked list. This Contributor class is the base class for all the structures we'll cover. That in particular is why I wanted to make sure this class was solid as written for use with all the other things we'll have to do. (he's also adding that we could make it a doubly linked list for extra credit, and moreso if we can overload the + operator to be able to merge two lists into one ordered list.

So...I suppose this begs the last question...is it solid enough as written to be the base class for the next few structures we're going to cover, knowing that linked and doubly linked are the first ones?

What you have written is a data class, and either your professor will have you extend it or will have you write a templated linked-list class and use Contributor in a linked list.

I've really only barely glanced at it, and most everything seems to be in order but one:

Serialization
Yep. It's that word. :)

The word 'serial' indicates something that is arranged in a series. In meaning it is very like the word 'sequential'. A serial port on your computer, for example, doesn't send all the information at once, but one 'word' at a time (be that a byte or an integer or whatever). A serial killer is someone who murders over and over again. He doesn't kill a lot of people all at once and be done with it. In all cases, we can speak of the objects (or people) by number. The first byte. The second byte. Etc. The last victim. The penultimate victim. Etc.

A file stream is a serial device. It is an ordered collection of data. A text file represents characters that are ordered to something humans can read. An exe (win) or elf (nix) file represents a collection of codes that are ordered to something the computer can read. Etc.

So, to serialize your class means to take your entire object (which exists all at once) and turn it into discrete, ordered elements that can be sent over a serial device (like an I/O port or to a file).

OK, enough of that background stuff. On to your error:
Your iostream operators (the insertion operator << and the extraction operator >>) try to interact with the user.

In other words, they are using a serial device to do the job of writing to a serial device. This assumes something about the I/O devices that you cannot always guarantee to be true.

The only iostream that operator >> should be using is the one given as argument. Likewise for operator <<.

Now, of course it is friendlier to the human sitting at the keyboard for that kind of input. But it does not belong in a data class's structure. You can always write (or provide for the user of your class) additional functions which do interact with the human and manipulate the Contributor. But the Contributor class itself should not know or care anything about who or what it is talking to.

And this leaves us with one last thing: Anything written by << should be readable by >>. This means you will have to decide how to represent your object serially (in a file).

You could make it very human-readable:

Name: Joaquin Ramirez Hernandez
Donation: $24000.00
Sex: Male
IDKey: 417982446

You could make it less human readable, but still structured (for example, CSV):

Joaquin Ramirez Hernandez, $24000.00, Male, 417982446

You could make it machine readable (but I won't recommend it for this). Etc.

By defining a definite serialized representation you can write provably correct output and validate input as correct or incorrect while allowing variations in things like whitespace.

Hope this helps.

You were right D, he wants us to write an ordered linked list. However, he does not want us to use the template (not yet).

So my thought, for everyone here, not just Duoas, is to take out the interactivity, and just implement several Contributor objects that I can insert, in order, into the linked list.

Does that sound like a better approach? I'll start a new thread for doing the linked list...I'm sure there will be...issues..lol

Some linguistic studies in Serialization ;):

From http://www.yourdictionary.com/serialize:

serialize Definition: to put or publish (a story , etc.) in serial form

From http://www.merriam-webster.com/dictionary/serialize:

serialize: to arrange or publish in serial form <serialize a novel>

serial: ... 6: relating to or being a connection in a computer system in which the bits of a byte are transmitted sequentially over a single wire...

From http://en.wikipedia.org/wiki/Serialization:

In computer science, in the context of data storage and transmission, serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link in binary form. When the resulting series of bytes is reread according to the serialization format, it can be used to create an accurate clone of the original object.

Don't give heed to "in binary form" from the prev quote, add unspoken "or textual".

Now let's remember that the process of a complex data object transforming (obviously) runs step by step, member by member, recursively downto the simplest types. Of course, the ready to use term from the data transmission area (and its derivatives) was in accordance with this OOP-related stuff...

A good term without side effects - compare with so common "sin(angle")"...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.