Tag Archives: c++

Error c3861: identifier not found | identifier not found

Just began to learn C++, today encountered more depressed problems, maybe beginners will encounter this problem, take it out to share.
Define a function, and then call in the main function, compile a run, prompt me “: identifier cannot be found “… , using VS2010.
I looked at it so many times, couldn’t find the solution, and finally found the answer on StackOverflow,
http://stackoverflow.com/questions/16290834/error-c3861-rolldice-identifier-not-found
The compiler compiles the contents of the file from top to bottom, and the function A is called in the main function, but because the main function is defined before the function A, the function A is not defined when it is used, so the “identifier cannot be found” situation occurs.
So, you just have to move the function forward.

Visual Studio Tips: error LNK2005: … already defined in LIBCMTD.lib(new.obj)

Problems encountered

A very old C++ project, before the compilation was all right, just after I introduced a few new.h and.cpp files, the compilation failed to pass, reported the following error:
(On the premise, of course, that there is no problem with the new files I introduced being double-checked, otherwise I would have started with those files and there would have been no article.)

Error	2	error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)	
C:\Work\Demo\DemoApplication\nafxcwd.lib(afxmem.obj)	Sentinel-XP



1>------ Build started: Project: DemoApplication, Configuration: Debug Win32 ------
1>  text.cpp
1>gbk.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
1>Debug\DemoApplication.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Problem analysis

There is no such thing as a gratuitous compilation error. After an extensive search, there is an explanation on MSDN that I think is very good. Here is an excerpt:

The CRT libraries use weak external linkage for the new, delete, and DllMain functions. The MFC libraries also contain new, delete, and DllMain functions. These functions require the MFC libraries to be linked before the CRT library is linked. 

I will not translate it into Chinese, I am lazy, and I believe everyone can understand this sentence.

This way, we give the MSDN link: https://support.microsoft.com/en-us/kb/148652

To solve the problem
In this article on the MSDN: https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx
The following sentence was given:

To fix, add /FORCE:MULTIPLE to the linker command line options, and make sure that ... is the first library referenced.

is just like this:

Then it was compiled. Although there were some warnings, the compilation passed:

1>------ Build started: Project: DemoApplication, Configuration: Debug Win32 ------
1>  ...
1>  ...
1>  Compiling...
1>  ...
1>  Generating Code...
1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/FORCE' specification
1>gbk.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:LBR' specification
1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj); second definition ignored
1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj); second definition ignored
1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj); second definition ignored
1>Debug\DemoApplication.exe : warning LNK4088: image being generated due to /FORCE option; image may not run
1>  DemoApplication.vcxproj -> C:\Work\Demo\DemoApplication\Debug\DemoApplication.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

works fine.

Of course, this is not the root of the problem, but given the old project document, I can’t easily change it too much, so let’s leave it as an imperfect solution.

reference

A LNK2005 error occurs when the CRT library and the MFC libraries are linked in the wrong order in Visual c + + https://support.microsoft.com/en-us/kb/148652
would Tools error LNK2005 https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx
selectany https://msdn.microsoft.com/en-us/library/5tkz6s71 (v = versus 80). Aspx
LNK2005, “Already defined error” would error in MSVC2010 http://stackoverflow.com/questions/8343303/lnk2005-already-defined-error-linker-error-in-msvc2010

syntax error on PKG_CHECK_MODULES

From: http://blog.csdn.net/coolper/article/details/12037101

eg.
./configure: line 14956: syntax error near unexpected token `PROTOCOL,'
./configure: line 14956: `PKG_CHECK_MODULES(PROTOCOL, spice-protocol >= 0.6.0)'

answer:
to install pkg-config

Error c2143: syntax error: missing ‘;’ before ‘type’

When using VC6.0 to compile. C file, if you define variables in any place in the program, the above error will occur;
Reason: Generally in ANSI C or C++, it is allowed to define variables at any time in executable code, but in K& It’s not allowed in R and C, and that’s why we have this error. VC6.0, VS2008 are used K& R C to implement the C language, so the compilation process will report errors.
In pure C, variable declarations for a block of code are usually at the front end.
Solution: 1. Put all variable declarations at the beginning.
2. Save the file as. CPP file.
 
 

error: expected unqualified-id before ‘dynamic_cast’

Error code:

Deride * q= std::dynamic_cast<Deride*>(p);

Dynamic_cast is not part of STD library, but C++ keyword, so it should be removed.

Deride * q= dynamic_cast<Deride*>(p);

A word of caution:
STD :: Dynamic_pointer_cast for dynamic conversion shared_PTR is the part in STD. This is also obvious because shared_PTR is a part in STD

Windows Visual C + + 2005 redistributable error 1935 solution

The reason: in the control panel, the wrong operation has deleted the vcredist_x86 2005
phenomenon: QQ2010 cannot be used, Nero 10 cannot be used, installation Arcgis10 failed!
solution, downloaded the vcredist_x86 sp1 installation on the Internet, the problem then appeared, the installation process is also very slow ~~~ finally appeared “Error 1935. PublicKeytoken =”1fc8b3b9a1e18e3b”,processirArchitecture=”x86″

, it turned out that the Windows service “Windows Installer service” did not start properly.
solve “Windows Installer service” launch exception, “error 1450: system resources insufficient, unable to complete the requested service”,
After analysis and search, it was found that it was caused by the residual installation of Arcgis9.3. The main reason was that the registration space was limited
error modification method after installation of Arcgis9.3:
1, in the run enter regedit to open registry editor
2, find HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control
3, change the RegistrySizeLimit (REG_DWORD type) value to FFFFFFFF (base 10 is 4294967295)
4, restart the computer
Start the “Windows Installer Service” again, OK
Run vcredist_x86 2005 SP1 as administrator again (be careful not to put it in the Chinese directory and clear the contents of the TEMP folder), OK
QQ, Nero, Arcgis and other software, due to the VCR caused by abnormal start, need to delete clean (use makeup removal software to clear the cache), and then install it is OK

“Error: ` cout ‘was not declared in this scope”

code is:
#include <iostream>
int main(int argc, char *argv[])
{
    cout << “hello world” << endl;
}
when compile the erro info is below:
test@HuiT43 ~/sutdy $ g++ scat. cpp  -o scat
scat. cpp: In function `int main(int, char**)’:
scat. cpp:12: error: `cout’ was not declared in this scope
scat. cpp:12: error: `endl’ was not declared in this scope
the reason is :
This is becuase C++ 1998 requires cout and endl be called ‘std::cout’ and ‘std::endl’, or that a proper using directives such as ‘using namespace std;’ be used.
#include <iostream>
int main(int argc, char *argv[])
{
    std::cout << “hello world” << std::endl;
}
or
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
     cout << “hello world” << endl;
}

fopen,fopen_ S, wfopen_ S and_ fsopen, _ The distinction of WFS open

In the process of C++ project, a function of opening and saving files needs to be implemented. After I write to the file tmp.dat, I want to write to the file for the second time. At this time, an error returning int error = 13 (Permission denied) occurs in both fopen_s and _wfopen_s.
Since the project is Unicode encoded, there is no way to use Fopen for file operations (fopen is available and Shared reads and writes are available as long as _CRT_SECURE_NO_WARNINGS is added to the precompile, which I don’t want to do). So all kinds of search, and look up MSDN. Here’s what it says on MSDN:
Files opened by fopen_s and _wfopen_s are not sharable. If you require that a file be sharable, use _fsopen, _wfsopen with the appropriate sharing mode constant (for example, _SH_DENYNO for read/write sharing).
Link: https://msdn.microsoft.com/zh-cn/library/z5hh6ee9 (v = versus 90). Aspx
That is, fopen_s and _Wfopen_s do not support file sharing. When you use fopen_s or _wfopen_s for read or write operations, you can only do one operation (i.e., you can only do one read or write operation). Note: If you use fopen_s or _wfopen_s once for a file in a different place, the file becomes unshared. If you read or write a file again, you may return an int error = 13 (EACCES (Permission denied). I just changed one place in the project, but it still doesn’t work the second time. I need to change all the places that involve reading and writing to _fsopen, _wfsopen.
FILE * FP = _wfsopen(tmpFile, _T(” WB “), _SH_DENYNO);

C + +: error in X utility file

In the middle of writing the program and testing it, there were a bunch of syntax errors that came from xUtility files.
opens the xutility file, which defines many constructs and function templates. Find the source of the error is not found, because it is similar to the lack of; “And so on. So back before writing the code, and finally found the problem: in the global defines a short function, then the short function and a function template name repetition, combined with the incoming parameters when calling type does not conform to the global defines the type of function, to the template, but does not define the type of template function, hence lead to errors.
correction method:
1. Change function name, error parameter type will report the correct position
2. Use scope to avoid ambiguity caused by improper parameter types

ISO C++ forbids comparison between pointer and integer [-fpermissive]

Today, when using C++ to brush the problem, encountered this problem, blame their own carelessness.

When determining whether a character pointer points to the end of a string, the character ‘\0’ is written as a comparison error raised by ‘\0’, which in comparison represents the address of the string ‘\0’.
One thing to note from this is that string comparisons in C++ are best done with STRCMP.