tags:

views:

123

answers:

4

I do not know how to call a function with a reference in it :( It is a silly noob question I know.

The header is let's call it ref.h has a function in it that is:

virtual int funcName (Bike &biker, BikeArray &bikearray, int x, int y .... )

Within ref.h there is another header that is called Bike.h.

How do I call funcName from my main.cpp (obv. I included Bike.h and ref.h

I have called other functions from ref.h in my program, but the other functions did not have references to Bike in them, just variables... How do I declare everything?

What I have so far:

ref *ref;
Bike bike;
BikeArray bikearray;
int x, y;

ref = ref::createinstace(configFile);

ref->funcName(bike, bikearray, x,y);

ref->funcName should return results based on the configFile i passed. I have done this earlier with another function in ref.h, but that function only had int's and doubles... no objects to Bike ...

... and I get the Error "no matching function for call ref::funcName(Bike&, BikeArray&, int&, int&)

Sorry, I know this is a novice question!

A: 

You call the function the same way regardless of whether its arguments are passed by value or by reference.

Bike foo;
BikeArray bar;
funcName(foo, bar);
VoteyDisciple
John Studio
A: 

Pass it values not pointers.

Bike biker;
funcName(biker, ...);
Samuel
A: 

funcName is virtual so must be a class member function. You therefore need to have a reference or pointer to the class that it is a member of first.

It would have been enough for Bike to be forwardly declared but presumably Bike.h contains the class definition for Bike. BikeArray might be a class or a typedef, for example

typedef std::vector<Bike> BikeArray;

To call funcName you would then call it using . or -> on the class reference or pointer you have, then use an instance of Bike and an instance of BikeArray to be written into, plus whatever other parameters it requires.

CashCow
+1  A: 

You pass objects per reference as you would pass them per copy:

someObj.funcName(myBiker, myBikeArray, 42, ...);

Note that, if a function takes arguments per non-const reference, this indicates that the function might change them. (See here for more on that.)

Also, you cannot pass rvalues (temporary objects) as non-const arguments. That means that, if you have a function that returns a biker:

Biker getBikerByNumber(int num);

you can't use it as an argument to funcName:

someObj.funcName(getBikerByNumber(42), myBikeArray, 42, ...); // won't compile

because rvalues to not bind to non-const references. They do bind to const references, though (and also to the rvalue references to be introduced by the next C++ standard and already implemented by some compilers).

sbi
Earlier in my program .... I create an object for ref.h, lets call it ref. Then set that object up to pass a reference file. So when i call ref->funcName it is supposed to pass me the results of that config file in the Bike object... But I get the error "No matching function for call ref::funcName .... "
John Studio
@John: Please edit your question to show the entire function call and the entire resulting error message, including all the "Candidates were:" lines.
Potatoswatter
ok... should be more complete now... sorry!
John Studio
I am am compiling now, but calling the function crashes my program... I think I need to talk to the guy who wrote the headers!
John Studio
I think you need to go back to whatever you're using to learn C++ from and see if you can work out the difference between the classes and functions that make up your programme and the names of the files you're using to store them in. What you've written so far suggests you either don't understand this difference, or you do but your attention to detail in describing your problem is lacking. The first problem will make developing C++ hard; the second problem (lack of attention to detail) will make ANY non-trivial software development impossible.
raimesh
@raimesh: I've seen this mistake made by students who came from Java to C++. @John: A header is a _file_ it might contain any number of _classes_ (which are syntactical entities) - or none. Just as a text file could contain any number of poems - or a shopping list.
sbi