Hello ladies and gents,

I was reading about how "Passing by References" for efficiency and there was a little program added wich shows what the difference is between passing by value and passing by reference is. The program is this one:

#include <iostream>

using namespace std;

class SimpleCat
{
public:
	SimpleCat();
	SimpleCat (SimpleCat&);
	~SimpleCat();
};

SimpleCat::SimpleCat()
{
	cout<<"Simple Cat Constructor...\n";
}

SimpleCat::SimpleCat (SimpleCat&)
{
	cout<<"Simple Cat Copy Constructor...\n";
}

SimpleCat::~SimpleCat()
{
	cout<<"Simple Cat Destructor...\n";
}

SimpleCat FunctionOne (SimpleCat theCat);
SimpleCat *FunctionTwo (SimpleCat *theCat);

int main()
{
	cout<<"Making a cat...\n";
	SimpleCat Frisky;
	cout<<"Calling FunctionOne...\n";
	FunctionOne (Frisky);
	cout<<"Calling FunctionTwo...\n";
	FunctionTwo (&Frisky);
	
	return 0;
}

SimpleCat FunctionOne (SimpleCat theCat)
{
	cout<<"FunctionOne returning...\n";
	
	return theCat;
}

SimpleCat *FunctionTwo (SimpleCat *theCat)
{
	cout<<"FunctionTwo returning...\n";

	return theCat;
}

No problem there, but, I decided to see wether I could change the program so "FunctionOne" and "FunctionTwo" where memberfunctions from SimpleCat, but, to no avail.

I thought that the following changes would be correct and lett me use this in the same way as the above example, but, that is not the case:

#include <iostream>

using namespace std;

class SimpleCat
{
public:
	SimpleCat();
	SimpleCat (SimpleCat&);
	~SimpleCat();

	FunctionOne (SimpleCat);
	*FunctionTwo (SimpleCat*);
};

SimpleCat::SimpleCat()
{
	cout<<"Simple Cat Constructor...\n";
}

SimpleCat::SimpleCat (SimpleCat&)
{
	cout<<"Simple Cat Copy Constructor...\n";
}

SimpleCat::~SimpleCat()
{
	cout<<"Simple Cat Destructor...\n";
}

int main()
{
	cout<<"Making a cat...\n";
	SimpleCat Frisky;
	cout<<"Calling FunctionOne...\n";
	Frisky.FunctionOne (Frisky);
	cout<<"Calling FunctionTwo...\n";
	Frisky.FunctionTwo (&Frisky);
	
	return 0;
}

SimpleCat::FunctionOne( SimpleCat theCat)
{
	cout<<"FunctionOne returning...\n";
	
	return theCat;
}

SimpleCat::*FunctionTwo (SimpleCat *theCat)
{
	cout<<"FunctionTwo returning...\n";

	return theCat;
}

The error messages that I get are these ones:

error C2501: 'FunctionTwo' : missing storage-class or type specifiers
error C2440: 'return' : cannot convert from 'class SimpleCat' to 'int'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be callederror C2501: 'FunctionTwo' : missing storage-class or type specifierserror C2440: 'return' : cannot convert from 'class SimpleCat *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2617: 'FunctionTwo' : inconsistent return statement

Can any of you please explain what I'm doing wrong :?:

Thank you.

Recommended Answers

All 4 Replies

Here Is the corrected Code...Code In Red Was Missing

#include <iostream>

using namespace std;

class SimpleCat
{
public:
	SimpleCat();
	SimpleCat(SimpleCat&);
	~SimpleCat();

	SimpleCat FunctionOne(SimpleCat);
	SimpleCat* FunctionTwo(SimpleCat*);
};

int main()
{
	cout<<"Making a cat...\n";
	SimpleCat Frisky;
	cout<<"Calling FunctionOne...\n";
	Frisky.FunctionOne(Frisky);
	cout<<"Calling FunctionTwo...\n";
	Frisky.FunctionTwo (&Frisky);
	
	return 0;
}
SimpleCat::SimpleCat()
{
	cout<<"Simple Cat Constructor...\n";
}

SimpleCat::SimpleCat (SimpleCat&)
{
	cout<<"Simple Cat Copy Constructor...\n";
}

SimpleCat::~SimpleCat()
{
	cout<<"Simple Cat Destructor...\n";
}

SimpleCat SimpleCat::FunctionOne( SimpleCat theCat)
{
	cout<<"FunctionOne returning...\n";
	
	return theCat;
}


SimpleCat* SimpleCat::FunctionTwo(SimpleCat *theCat)
{
	cout<<"FunctionTwo returning...\n";

	return theCat;
}
commented: Very helpfull person! +2

Thank you sunnypalsingh for your help :!:

Could you however explain why it is necessary to write the name of the class infront of the definition inside the class and twice infront of the declaration outside the class :?:

Could you however explain why it is necessary to write the name of the class infront of the definition inside the class and twice infront of the declaration outside the class :?:

Well its the syntax of language.....
syntax
returntype classname ::functionname(arguments)

returntype-tells what the function is returning
classname-tells the scope of the function(which class does it belong to)
:: scope resolution operator

rest i guess is clear.......read your books properly...this is given in any c++ book..........

In function decleration you need to specify return type of the function(you were missing that).....only constructors and destructors don't have return type....rest all the member function should have a return type

Doh, how stupid I have been :o

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.