Jump to content
  • Advertisement
Sign in to follow this  
LuFe

Function that returns a pointer

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by stylin
Do you have CMonstro defined before your prototype?


thanks, thats is the problem, i think


Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by DadleFish
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.

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

Share this post


Link to post
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.cpp

int myfunction(int, int, int);

int main()
{
myfunction(1);
}






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


//myfunction.cpp

int 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.cpp

int 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.cpp

int 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.cpp
void log(const std::string& Message, const std::string& SourceFile = "a.cpp");

..
log("hi");
...





//b.cpp
void 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!