My problem is, I can’t seem to send an object by referance to another class. I have had little luck looking this up on the ‘net. If you could please examine my source and let me know if you have any ideas. TYIA -Roland
I also get these errors
error: field 'PgmClass' has incomplete type
error: 'PgmClass' does not name a type
error: expected ')' before 'thesource'
error: 'm_hereitis' was not declared in this scope
#include <iostream>
#include "pgmclass.h"
#include "inclobj.h"
int main()
{
char catchcin[256];
PgmClass wilko;
wilko.addToSet( 7 );
wilko.addToSet( 8 );
wilko.addToSet( 9 );
InclObj alpha( wilko );
wilko.addToSet( 10 );
wilko.addToSet( 11 );
// This doesn't work
alpha.eraseOne( 10 );
// How can I get this to work using referances?
std::cout << "Program Running." << std::endl;
std::cin >> catchcin;
return 0;
}
----------
#include <set>
class PgmClass {
public:
int addToSet( int );
bool eraseSet( int );
std::set<int> m_userset;
};
int PgmClass::addToSet( int theint ) {
m_userset.insert( theint );
}
bool PgmClass::eraseSet( int eraseint ) {
m_userset.erase( eraseint );
}
----------
class InclObj {
public:
InclObj( PgmClass );
void eraseOne( int );
PgmClass m_hereitis;
};
InclObj::InclObj( PgmClass thesource ) {
m_hereitis = thesource;
}
void InclObj::eraseOne( int findint ) {
m_hereitis.eraseSet( findint );
}
You need to place your main at the end of the file. (Normaly you add the class in a separate .h file -with the implementation in another .cpp file-, with you include before use the class).
Define, the member as reference:
Doing this you assume some responsibilities. For example, don’t use
eraseOne()after the original object has been deleted. Don’t try to add a function likeInclObj::use_now_this_other_object(PgmClass& other_source), etc. But I assumed you know about…