• 15
• 15
• 11
• 9
• 10

# Function that returns a pointer

This topic is 4549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts


//This is the function:
CMonstro* NovoMonstro(int MonstroLargura,int MonstroAltura, int initX = 0, int initY = 0)
{
...
}

//This prototype makes compiler error:
CMonstro * NovoMonstro(int, int, int, int);


How can i fix it? Thanx

##### Share on other sites
What is the error it gives, and are you including the prototype before the function?

##### Share on other sites
why are u doing a fwd dec after the function has already been defined?
usually it's the other way around. and in the fwd dec is where you
normally place the default args in the param list, unless the function
is inline. tis odd code.

##### Share on other sites
The prototype is after the function because i want use olny a block of source code, to post be smaller.
In my code, the prototype is ina header file CMonstro.h
And the function is in the CMonstro.cpp

The error is:
9 C:\LuFe3\Desenvolvimento\X-Drift.NEt\novo\teste3\CMonstro.h expected constructor, destructor, or type conversion before '*' token

9 C:\LuFe3\Desenvolvimento\X-Drift.NEt\novo\teste3\CMonstro.h expected ,' or ;' before '*' token

Show how is the right mode

##### Share on other sites
Do you have CMonstro defined before your prototype?

##### Share on other sites
Quote:
 Original post by stylinDo you have CMonstro defined before your prototype?

thanks, thats is the problem, i think

Sure thing.

##### Share on other sites
I thought you had to define the default parameters in the PROTOTYPE and not in the actual implementation of the function. Maybe that's another problem you have there.

##### Share on other sites
Quote:
 Original post by DadleFishI thought you had to define the default parameters in the PROTOTYPE and not in the actual implementation of the function. Maybe that's another problem you have there.

No, it's optional. Many programmers do it for code clarity.

##### Share on other sites
I think you do have to declare the default parameters in the function prototype (if you have a prototype), otherwise how is the compiler to determine if a call to the function is valid if the function definition resides in another translation unit.

Consider the following example of two source files and two functions

//main.cppint myfunction(int, int, int);int main(){  myfunction(1);}

The function myfunction() is defined in myfunction.cpp as

//myfunction.cppint myfunction(int a, int b = 1, int c = 2){return a + b + c;}

Now when the compiler attempts to compile the first translation unit, namely main.cpp, it will get to the line myfunction(1); and attempt to find some meaning out of it. It knows there is a version of myfunction which takes 3 parameters, but it cannot see one that only takes one parameter, so it must issue a compiler error. The compiler knows nothing of the function definition in the myfunction.cpp file, and so knows nothing of the default parameters of the function.

To get the code to compile, you must specify the default arguments in the declaration of the function provided in the translation unit in which the function is called. This is done like so:

//main.cppint myfunction(int, int = 10, int = 10);int main(){  myfunction(1);}

The code now compiles correctly.

An interesting property of this is that you can declare the function to take different default arguments in different translation units. So, for example, you may want to add another source file, b.cpp, which declares the function as

//a.cppint myfunction(int, int = 20, int = 30);void anotherfunction(){  myfunction(1);}

Here you have provided another call to the function with different default parameters than are specified in another translation unit, and so you will get a different result.

I can't think of many uses of this feature, maybe something like a logging system which provides details of the source file for each logging message?

//logger.cpp#include<ostream>#include<string>#include<iostream>void log(const std::string& Message, const std::string& SourceFile){std::clog << Message << " From: " << SourceFile << std::endl;}

You could then change the second parameter to a different default parameter in each source file like so

//a.cppvoid log(const std::string& Message, const std::string& SourceFile = "a.cpp");..log("hi");...

//b.cppvoid log(const std::string& Message, const std::string& SourceFile = "b.cpp");..log("hi");...

You would then get a different logging message for each source file.

You can also override the default arguments in a different scopes within a translation unit. For example, you can declare a function to have a different default argument for the duration of a certain function like so:

void function(){  void log(const std::string& Message, const std::string& SourceFile = "b.cpp function()");  log("hi");}void function2(){  void log(const std::string& Message, const std::string& SourceFile = "b.cpp function2()");  log("hi");}

There you will get a different message for each function call.