Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a user-defined literal like this in is OWN .cpp file (declared as a friend function in .h file):
<pre lang="C++">
fraction operator"" _frac(const long double val)
{
	return fraction(static_cast<float>(val));
}


But in main it produces this error:
Error (active)E2486 user-defined literal operator not found

But when I write the SAME code (definitely the same as I copy it and also do some words comparison) in main or .h(outside of the class scope) file, it works fine? Is it because it must be written as a global function? I'm using Visual Studio 2019 and C++ 20. GitHub

What I have tried:

I created other classes and use an older version of C++, I still get the same result (definition in the same file as main works fine but if written in another file doesn't work). I can accept to write it in the same file, but then what's the purpose of orientated programming anymore.
Posted
Updated 27-Aug-21 20:53pm
Comments
Richard MacCutchan 28-Aug-21 3:46am    
You need to show how it is defined in the header file, and how it is referenced in the main .cpp file.

1 solution

Do you have a prototype for the function defined? Something like :
C++
extern fraction operator _frac(const long double val);
 
Share this answer
 
Comments
Khiêm Nguyễn 2021 28-Aug-21 6:34am    
Yes, I tried to include the extern in the .cpp file. Still not work
Khiêm Nguyễn 2021 28-Aug-21 9:16am    
OH, it worked. But my compiler says: Redundant declaration

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900