| | |
Adding memberfunctions into a class?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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:
The error messages that I get are these ones:
Can any of you please explain what I'm doing wrong
Thank you.
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:
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
#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:
C++ Syntax (Toggle Plain Text)
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.
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;
}•
•
•
•
Originally Posted by JoBe
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
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
![]() |
Similar Threads
- java.lang.NoSuchMethodError: main ?? (Java)
- ArrayList Help (Java)
- Double-Sided (Java)
- accessing private data members (C++)
- "missing storage-class or type specifiers" error (C++)
Other Threads in the C++ Forum
- Previous Thread: overloading <<
- Next Thread: using your own namespace?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





