Reputation: 166
I have the following line of code in one of the projects I am working on:
char* i2txt(int);
And I don't exactly understand what it does? With my half-knowledge I was trying to make it work with floats, so I changed the int to a float, but that gives me an error.
char* i2txt(int);
/*vs*/
char* i2txt(float);
Error Message :
Error LNK2019 unresolved external symbol "char * __cdecl i2txt(float)" (?i2txt@@YAPADM@Z) referenced in function "public: char * __thiscall DapChannel::getDurationTxt(void)" (?getDurationTxt@DapChannel@@QAEPADXZ) PhotoZ DapChannel.obj 1
Upvotes: 4
Views: 683
Reputation: 4111
Its just a function declaration that take and int
argument and return char*
:
// Declaration :
char* i2txt(int);
// Definition :
char* i2txt(int n)
{
// do something
}
But the occurred error
is because there's an implementation of i2txt
function that take an int
argument and when you trying to change the declaration (especially if the implementation defined in an static library) it give you the linker error
like the bellow :
error LNK2019: unresolved external symbol "char * __cdecl a(float)" (?a@@YAPADM@Z) referenced in...
In the normal state, if you call i2txt
with a float
value, it may cast float number to int, BUT if you trying to change the declaration of i2txt
the link error will be occur (If the definition is in a static library).
Upvotes: 3
Reputation: 10740
The statement char* i2txt(int);
is forward-declaring a function i2txt
that takes an int
as input, and returns a char*
.
If you have a function used before it's declared, that results in an error:
#include <iostream>
int main() {
foo(); // Error: foo not defined
}
void foo() {
std::cout << "Hello, world!";
}
Forward-declaration basically states "This function isn't defined yet, but I promise I'll define it eventually. In the above case, it'd look like this:
#include <iostream>
void foo(); // Forward declaration
int main() {
foo(); // Now we can use it
}
void foo() {
std::cout << "Hello, world!";
}
i2txt(float);
?This results in an error because suddenly, there's no i2txt(int)
function to call. Because int
s can be implicitly converted to float
, the compiler still allows other functions to call i2txt(float)
, but no definition for i2txt(float)
is ever provided, so there's a linker error:
#include <iostream>
char* i2txt(float);
int main() {
std::cout << i2txt(10); // Tries calling i2txt(float)
}
// This provides a definition for i2txt(int), but the linker is still missing a definition for i2txt(float)
char* i2txt(int) {
// ... stuff
}
Upvotes: 3
Reputation: 28987
It declares a function which takes an int (by value), and returns a point to char. Given the name (integer to text), the pointer is almost certainly to a null terminated sequence of characters which are the text version of the number. The pointer will either be to a static variable in the function (which means it is not thread safe, and the resulting text must be copied if you want to save it), or to a dynamically allocated array (in which case it must be freed). A function returning a std::string
would be much better.
Upvotes: 3
Reputation: 2942
This declares a function accepting an integer and returning a char pointer.
I'm guessing the error you got after changing it was a linker error, telling you that it doesn't find a definition for i2txt
with a float parameter. That's because the definition of this function, which is provided elsewhere, accepts an integer parameter not a float.
Upvotes: 3