Is this code valid or is it a compiler?

int cpu = 0; int player = 0; char * getPoints() { using namespace std; string str = "You: "; str += player; str += " CPU: "; str += cpu; char c [100]; strcpy(c, str.c_str()); return c; } 

This code does not compile. Is the code wrong or is there something wrong with my compiler?

I am using Microsoft Visual Studio with DarkGDK .

If it's me, can someone improve it?


This is the conclusion:

 1>------ Build started: Project: Pong, Configuration: Debug Win32 ------ 1>Compiling... 1>Main.cpp 1>c:\users\martijn\documents\visual studio 2008\projects\pong\pong\main.cpp(42) : warning C4172: returning address of local variable or temporary 1>Linking... 1>libcpmtd.lib(xdebug.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library 1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z) 1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z) 1>libcpmtd.lib(stdthrow.obj) : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "void __cdecl std::_Debug_message(wchar_t const *,wchar_t const *,unsigned int)" (?_Debug_message@std@@YAXPB_W0I@Z) 1>Debug\Pong.exe : fatal error LNK1120: 3 unresolved externals 1>Build log was saved at "file://c:\Users\Martijn\Documents\Visual Studio 2008\Projects\Pong\Pong\Debug\BuildLog.htm" 1>Pong - 4 error(s), 2 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+1
c ++
source share
4 answers

The errors seem to be related to problems with the compiler. Are you sure that the project is configured correctly?

The warning about returning a local variable is serious. You push the array c onto the stack in your function. When the function returns, the array will disappear from the stack, and the pointer you returned indicates garbage. Instead, return string or allocate c to the heap.

+4
source share

The LNK4098 error indicates that your assembly will mix code that was compiled with DEBUG settings (thus using libcmtd.lib) and code that was compiled with settings without DEBUG (thus using libcmt.lib).

OR: This can be code using static libs executables (libcmt.lib) mixed with code using dynamic libs executables (msvcrt.lib).

The linker then finds two different versions of the common methods / classes of the runtime library and cannot figure out what to use. You cannot mix multiple executables in one assembly.

In any case, this is a problem with the solution / project. We came across this when we tried to link to the libs / dll provided by other people, where they built them with different settings than we used.

The / NODEFAULTLIB argument (available in the VS Linker options as "Ignore libraries" or similar) allows you to forcibly ignore one or more of these sets of runtime libraries, leaving only one runtime library.

+6
source share

The concatenation that you do does not do what you think; the + = std :: string operator only accepts std :: string, char * or char, so the ints you are trying to add are converted to characters, so each of them is considered a single ASCII character that must be attached to the string. If you want to build strings from many variables of different types, you should consider using string streams (sstream header) or acceleration.

In addition, you return a pointer to a local object, which will disappear when the function returns, so the caller will have a pointer to garbage; it can work if it was used immediately after the function returns, since this part of the stack has not yet been overwritten, but you are in the undefined behavior country and it will fail if you try to use this pointer after another function call.

You need to return std :: string so that the caller gets its own copy of the string. Then, if he needs C (ASCIIZ), she can get it from std :: string, which calls the c_str () method.

The errors you posted are related to the linker; such errors usually occur when you link to a static library compiled with another version of CRT; recompile your project and library with the same CRT version to solve the problem.

+4
source share

Martjin,

There are some errors in your code that may cause the program to crash after starting. Nevertheless, none of them should prevent you from compiling / linking it. As mentioned, the problem is with the linker, not the compiler, which means that your code generates the object correctly, but when VS tries to create an executable file, it does not find the necessary characters.

You should look at your SDK documentation to find out how to properly link your objects with your libraries. Once this is done, all external undefined elements must be allowed, which allows you to create an executable file.

+2
source share

All Articles