passing objects as arguments

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2004
Posts: 113
Reputation: liliafan is on a distinguished road 
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

passing objects as arguments

 
0
  #1
Jun 18th, 2004
Hi

I am attempting to pass a reference to an object from one function to another, is this even possible, if so what is the syntax this is an example of what I want:

  1. class SomeClass {
  2. public:
  3. int a = 100;
  4. };
  5.  
  6. void second_function(SomeClass & ref_to_classobj)
  7. {
  8. std::cout << ref_to_classobj.a << endl;
  9. }
  10.  
  11. int main()
  12. {
  13. SomeClass classobj;
  14. second_function(classobj);
  15. return 0;
  16. }

Obviously the functions and classes I am using are a lot more complicated the class itself in the program I am writing takes about 5 seconds to construct due to it dealing and containing a huge amount of data. I don't want to have to reinitialise this class everytime I use it I want to just be able to point a reference to it. Does anyone know how to do this?

I have tried the method above, also:

void foo(SomeClass &reference)
void foo(SomeClass *reference)

and about 10 other variations on this, the usual error message I get is:

no matching function for call to `keyword_cache::find_keyword(const char[6])'

Any help will be greatly appreciated.

Thanks

Ben
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 10
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: passing objects as arguments

 
0
  #2
Jun 18th, 2004
What you have written as an example looks correct. Your error message suggests a different issue at work? Perhaps the function find_keyword does not take a const string?

find_keyword( char* keyword )

if you said:

find_keyword( "123456" );

you would likely get the error you mention, because "123456" is a six char const array. (heck, maybe its 7 with a trailing null, but you get the idea)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 113
Reputation: liliafan is on a distinguished road 
Solved Threads: 2
liliafan's Avatar
liliafan liliafan is offline Offline
Junior Poster

Re: passing objects as arguments

 
0
  #3
Jun 18th, 2004
Originally Posted by Chainsaw
What you have written as an example looks correct. Your error message suggests a different issue at work? Perhaps the function find_keyword does not take a const string?
You are right :o)

The actual call I was making was

  1. void test_class(keyword_cache &test)
  2. {
  3. test.find_keyword("black");
  4. }

It was translating the "black" at a char array not a string.

Thanks for the help.

Ben
Application development, webhosting, and much more: www.webcentric-hosting.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC