Search

0.8 — A few common C++ problems

In this section, we’ll address some of the common issues that new programmers seem to run across with fairly high probability. This is not meant to be a comprehensive list of compilation or execution problems, but rather a pragmatic list of solutions to very basic issues. If you have any suggestions for other issues that might be added to this list, post them in the comments section below.

General run-time issues

Q: When executing a program from the IDE, the console window blinks and then closes immediately.

First, add or ensure the following lines are near the top of your program (Visual Studio users, make sure these appear after #include “pch.h” or #include “stdafx.h”, if those exist):

Second, add the following code at the end of your main() function (right before the return statement):

This will cause your program to wait for the user to press a key before continuing, which will give you time to examine your program’s output before your operating system closes the console window.

Other solutions, such as the commonly suggested system(“pause”) solution may only work on certain operating systems and should be avoided.

Older versions of Visual Studio may not pause when the program is run in Start With Debugging (F5) mode. Try running in Start Without Debugging (ctrl-F5) mode.

Q: I ran my program and get a window but no output.

Your virus scanner or anti-malware may be blocking execution. Try disabling it temporarily and see if that’s the issue.

Q: My program compiles but it isn't working correctly. What do I do?

Debug it! There are tips on how to diagnose and debug your programs later in chapter 1 or 2.

General compile-time issues

Q: When I compile my program, I get an error about unresolved external symbol _main or _WinMain@16

This means your compiler can’t find your main() function. All programs must include a main() function.

There are a few things to check:
a) Does your code include a function named main?
b) Is main spelled correctly?
c) When you compile your program, do you see the file that contains function main() get compiled? If not, either move the main() function to one that is, or add the file to your project (see lesson 1.8 -- Programs with multiple files for more information about how to do this).
d) Did you create a console project? Try creating a new console project.

Q: I'm trying to use C++11/14/17/XX functionality and it doesn't work

If your compiler is old, it may not support these more recent additions to the language. In that case, upgrade your compiler.

Alternatively, you can try seeing if your compiler has support for these but has it turned off by default. For GCC/G++, you can try passing compiler flags -std=c++11, -std=c++14, or -std=c++17. For Code::Blocks, make sure the appropriate global compiler setting is set. We show how to do that in lesson 0.6 -- Installing an Integrated Development Environment (IDE).

Q: When trying to use cin, cout, or endl, the compiler says cin, cout, or endl is an 'undeclared identifier'

First, make sure you have included the following line near the top of your file:

Second, make sure each use of cin, cout, and endl are prefixed by “std::”. For example:

If this doesn’t fix your issue, then it may be that your compiler is out of date, or the install is corrupted. Try reinstalling and/or upgrading to the latest version of your compiler.

Q: When trying to use endl to end a printed line, the compiler says end1 is an 'undeclared identifier'

Make sure you do not mistake the letter l (lower case L) in endl for the number 1. endl is all letters. Make sure your editor is using a font that makes clear the differences between the letter lower case L, upper case i, and the number 1. Also the letter capital o and the number zero can easily be confused in many non-programming fonts.

Visual Studio issues

Q: When compiling with Microsoft Visual C++, you get a C1010 fatal error, with an error message like "c:\vcprojects\test.cpp(263) :fatal error C1010: unexpected end of file while looking for precompiled header directive"

This error occurs when the Microsoft Visual C++ compiler is set to use precompiled headers but one (or more) of your C++ code files does not #include “stdafx.h” or #include “pch.h” as the first line of the code file.

Our suggested fix is to turn off precompiled headers, which we show how to do in lesson 0.7 -- Compiling your first program.

If you would like to keep precompiled headers turned on, to fix this problem, simply locate the file(s) producing the error (in the above error, test.cpp is the culprit), and add the following line at the very top of the file(s):

Older versions of Visual Studio use “stdafx.h” instead of “pch.h”, so if pch.h doesn’t resolve the issue, try stdafx.h.

Note that for programs with multiple files, every C++ code file needs to start with this line.

Alternatively, you can turn off precompiled headers.

Q: Visual Studio gives the following error: "1MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)"

You’ve likely created a Windows graphical application rather than a console application. Recreate your project, and make sure to create it as a Windows (or Win32) Console project.

Q: When I compile my program, I get a warnings about "Cannot find or open the PDB file"

This is a warning, not an error, so it shouldn’t impact your program. However, it is annoying. To fix it, go into the Debug menu -> Options and Settings -> Symbols, and check “Microsoft Symbol Server”.

Something else

Q: I have some other problem that I can't figure out. How can I get an answer quickly?

As you progress through the material, you’ll undoubtedly have questions or run into unexpected problems. What to do next depends on your problem. But in general, there are a few things you can try.

First, ask Google. Find a good way to phrase your question and do a Google search. If you have received an error message, paste the exact message into google using quotes.
Odds are someone has already asked the same question and there is an answer waiting for you.

If that fails, ask on a Q&A board. There are websites designed for programming questions and answers, like Stack Overflow. Try posting your question there. Remember to be thorough about what your problem is, and include all relevant information like what OS you’re on and what IDE you’re using.


0.9 -- Configuring your compiler: Build configurations
Index
0.7 -- Compiling your first program

211 comments to 0.8 — A few common C++ problems

  • João Pedro

    In the first problem what the window opens and immediately quits has another solution, this solution is for VStudio. Simply to compile press CTRL + F5, you are starting without debugging

    • Alex

      I think this is only an issue in older versions of Visual Studio now. 2017 seems to pause at the end of program execution regardless of whether you're executing with debugging or not.

  • Rapheal

    when trying to build the project "Hello World" in Visual studio 2017
    The error list is showing the following Message
    "error MSB6006: "CL.exe" exited with code -1073741515"

    Please help with it

    • Alex

      Sounds like you're missing a .dll, which suggests a corrupted installation. See https://stackoverflow.com/questions/22038660/visual-studio-2013-cl-exe-exited-with-code-1073741515 for more info.

  • Wulfred

    I just want to say that to be more inclusive to all sides of coding on certain platforms, don't focus solely on Windows side of IDE editors. If you want a more universally supported IDE I would totally recommend Sublime Text 3 application, it's available to both Linux, Windows and Mac, and has readily available additional packages and support for building and running code built-in to the editor itself, which also comes with a Distraction Free mode and is easy on the eyes with varying dark themes instead of staring at a white screen while coding which is bad for your eyesight. I recommend Sublime Text because it's clean and simple in its purest form, and is easy for anyone to learn to use.

    • Alex covered IDEs for Windows/Linus and Mac in lesson 0.6.
      Sublime text isn't an IDE, it's a text editor.

      • Wulfred

        Yes, it's not a an IDE, but it's got the features of an IDE in the form of a text editor.

        • Alex

          This tutorial series is focused on teaching C++, not installing the most productive code editor. To that end, our recommended solutions are optimized for getting people up and running with minimal fuss. Separating out the code editor from the compiler just adds more steps that can go wrong for beginners, who are least equipped to understand how to fix such issues.

          This is part of the same reason we don't recommend Eclipse, VS Code, Atom, Emacs, or Notepad++.

  • Fan

    Hi Alex,

    Two pedantic questions:

    1. In the answer to the first question "...which will give you time to examine your program’s output before your compiler closes the console window." I am wondering is the compiler, or the runtime, or the IDE, that closes the console window?

    2. In the answer to the first Visual Studio question "Our suggested fix is to turn off precompiled headers, which we show how to do in lesson 0.9 -- Configuring your compiler: Build configurations." I can't see where it is shown in Section 0.9.

    Thanks.

    • Alex

      1) I believe that it's the operating system that does so after executing the runtime. I'll update the text, as it's clearly not the compiler, which isn't even used when the program is running.

      2) This recently got moved and I missed updating this lesson reference. I'll fix it. Thanks for pointing these out!

  • Ryan

    Hello! I'm not sure if this is a typo, but I was a little confused by the error given in the first example under the 'Visual Studio issues' section. I was reading through everything, despite not having any issues with my own IDE, just for the sake of getting the knowledge behind common issues, and saw the example error "c:cprojects est.cpp(263) :fatal error C1010: unexpected end of file while looking for precompiled header directive". Was the 't' accidentally omitted from 'est.cpp', or would it be presented in this way for some reason? I apologize if I missed a detail here, but I did want to point it out in case it was a mistake. Thanks!

    • Alex

      It was an encoding error. In many programming languages (including C++ and PHP) backslashes are used inside strings to denote a non-printable character. So \t was being treated as a tab rather than as literal \t characters. I've corrected the issue. Thanks for pointing it out!

  • Bartek

    Some time ago I noticed that every time I want to build a file, I receive a message "error: cannot find 'ld'". I hadn't had any similar problem before (I mean, I did some examples from lessons and didn't notice this message; everything worked perfectly).
    I only found an information that it could be a problem with an 'ld.exe' file. Is that true?
    Thanks in advance.

    • Alex

      Yes, most likely related to ld executable. Ld is the GNU linker that links object files created with gcc/g++. If your programs compiled and ran, I guess I'd ignore the issue for now, because your code must be getting linked somehow...

  • CrashTheRed

    Hello, I'd like to inquire about a certain problem I'm having while using visual studio 2017.

    When I compile, build and run the program for the first time, or any other time with no changes, it runs without any issues. If I try to add or remove code or make any changes and try to compile, it gives me an error saying it cannot open filename.exe at the debug folder of the project so it could write in it. I've found the quickest solution to solving this would be deleting the exe file, but that's not efficient in any way. I read some saying AV might be causing this problem but I don't have any heavily active ones to give me such a problem, or I take a look at the processes and I don't see any CMDs open, so I don't know how to solve this permanently. Help would be greatly appreciated.

    Also, I'd like to say, having taken courses on Java in my university till OOP, I have some insight into basics of programming and such. I want to say, this course is amazing so far, really well done and I'm loving it so far.

    • CrashTheRed

      I was able to solve this issue (about friggin time) by starting the Application Experience service, besides some other solutions some people have, this is the one that worked for me.

  • Hi Alex, Thanks for the information i have learnt a lot from reading this.

  • ello, Alex! I have Windows 10 and CodeBlocks. My CodeBlocks sayed that he dosen't find the GNU GCC Compiler.
    What can i do? Please, help me!

  • yitzi

    Hi Alex.
    Could you please explain what's wrong with this program I've done.

    #include "stdafx.h"

    #include <iostream>

    //#include "multiply.h"

    int mul(int a, int b)
    {
        return a * b;

    }

    int main()
    {
        int x;
        int y;
        std::cout << "enter first number" << std::endl;
        std::cin >> x;
        std::cout << "enter second number" << std::endl;
        std::cin >> y;
        
        std::cout << mul(x, y) << std::endl;
        return 0;
    }
    1>------ Build started: Project: My own try, Configuration: Debug Win32 ------
    1>multiply a.cpp
    1>c:\progaming\my own try\my own try\multiply a.cpp(1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
    1>My own try.cpp
    1>Generating Code...
    1>Done building project "My own try.vcxproj" -- FAILED.
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  • William

    Hello,
    Im coming across a issue while trying to build my program. error C2679. And I cant figure out a solution can someone help me?

  • Mark

    Hi,
    In our textbook it says that we have to use "using namespace std;", but I read somewhere that it can cause name collisions, but I don't know what it means; so is it better to use "using namespace std;" or std::cout, std::cin , std::endl; ,etc ? Can you replace std inside "using namespace std" with other codes?
    Thanks.

    • nascardriver

      Hi Mark!

      Don't use 'using namespace', if your book tells you to use it you might want to consider getting a new book, because there might be more bad advise in there.

      Example naming colission:

      You might not be able to understand this example just yet. Your question should resolve itself in future lessons.

      References
      * Lesson 4.3b - Namespaces

  • Brecht Vandendriessche

    Can you help me?
    I just downloaded visual studio and I typed this code:

    but I get a lot of errors and I don't know why this ar the errors:

    Severity    Code    Description    Project    File    Line    Suppression State
    Error (active)    E0349    no operator ">>" matches these operands    test    c:\Users\Medion\source\repos\test\test\Source.cpp    5    
    Error (active)    E0065    expected a ';'    test    c:\Users\Medion\source\repos\test\test\Source.cpp    6    
    Severity    Code    Description    Project    File    Line    Suppression State
    Error    C2784    'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2784    'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2784    'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char *)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2784    'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,signed char &)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2784    'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char *)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2784    'std::basic_istream<char,_Traits> &std::operator >>(std::basic_istream<char,_Traits> &,unsigned char &)': could not deduce template argument for 'std::basic_istream<char,_Traits> &' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2784    'std::basic_istream<_Elem,_Traits> &std::operator >>(std::basic_istream<_Elem,_Traits> &&,_Ty &&)': could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &&' from 'std::basic_ostream<char,std::char_traits<char>>'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    Error    C2676    binary '>>': 'std::basic_ostream<char,std::char_traits<char>>' does not define this operator or a conversion to a type acceptable to the predefined operator    test    c:\users\medion\source\repos\test\test\source.cpp    5    

    Error    C2143    syntax error: missing ';' before 'return'    test    c:\users\medion\source\repos\test\test\source.cpp    6    

    What is wrong?

    • Alex

      should be

      The compiler was complaining that std::cout doesn't know what to do with the >> operator.

      • Alex did you get this info from any books or websites, or did you learn it yourself then type it out? This is very impressive. I am creating a Word document for all of these lessons. If you give me your email i can give it to you. But for now i want to focus on this question.

        • ChrisPy

          This information was found in the output error list. Specifically the 2nd and 3rd lines.

          "Error (active)    E0349    no operator ">>" matches these operands    test    c:\Users\Medion\source\repos\test\test\Source.cpp    5    
          Error (active)    E0065    expected a ';'    test    c:\Users\Medion\source\repos\test\test\Source.cpp    6 "

          These errors state that:

          ">>" is not an operator in cpp. This is because it is actually supposed to be "<<". The complier was confused with the ">>" command as it didn't know what to do with it. Then the compiler couldn't complete the compilation due to the missing ";", which indicates the end of a statement.

          This is a basic syntax error

          All other errors are caused by those 2 syntax errors.

    • Parvez Alam

      I can help you on this error.

  • winsurfwon

    I am ready to code

Leave a Comment

Put all code inside code tags: [code]your code here[/code]