What Is wrong with this code?

CDev-8220 470 Reputation points
2025-03-17T16:12:27.2866667+00:00

I getting this error on the function below, which is not already defined, as far as I know.

Error:

Warning: discarding return value of function with [[nodiscard]] attribute Error: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl WCHAR_TO_STRING(wchar_t *,int &)" (?WCHAR_TO_STRING@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEA_WAEAH@Z) already defined in WinApp.obj Error: one or more multiply defined symbols found

Code:

string WCHAR_TO_STRING(WCHAR* wc, int& chars) {
	string ws = "";
	return ws;
}


I don't understand why.

Can someone please explain. Thanks.

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author
Minxin Yu 13,516 Reputation points Microsoft External Staff
2025-03-18T02:04:31.98+00:00

Hi,

The function definition is included in header file. And #include header.h in multiple .cpp files causes duplicate symbol definitions.

inline keywords explicitly allows multiple definitions.

Best regards,

Minxin Yu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.