Thread: Simplest of questions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Simplest of questions

    Doing literally my 2nd walk-through in a book. I'm experienced in PHP, but brand new to C++, and don't quite understand the errors.

    This should be super simple:

    Code:
    #include <iostream>
    
    int main()
    {
    	using namespace std;
    	int carrots;
    	cout << "How many carrots?" << endl;
    	cin >> carrots;
    	cout << "Here are two more. ";
    	carrots = carrots + 2;
    	cout << carrots;
    	return 0;
    }
    Getting error:

    1>c:\users\chase\documents\visual studio 2010\Projects\getinfo\Debug\getinfo.exe : fatal error LNK1120: 1 unresolved externals
    Thanks guys, sorry for the newbie

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    using namespace std; Should be above main not in it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Like this?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	
    	int carrots;
    	cout << "How many carrots?" << endl;
    	cin >> carrots;
    	cout << "Here are two more. ";
    	carrots = carrots + 2;
    	cout << carrots;
    	return 0;
    }
    I tried that and same error. The book has it in the int main() {, so it is weird that it is wrong in the book!

    But still no go unfortunately

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by PlzHelp View Post
    1>c:\users\chase\documents\visual studio 2010\Projects\getinfo\Debug\getinfo.exe : fatal error LNK1120: 1 unresolved externals
    The code is fine. You have a linker error. Post the details of how you are doing the link step and any other relevant info.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ehh... That is the linker complaining that it can't link to the implementation of your iostream library, from what I can tell. Is your environment setup correctly?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    I used Win32 application instead of Win32 console (empty project) in visual studio. Is that it? I just went afk so will check when I get home

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by PlzHelp View Post
    I used Win32 application instead of Win32 console (empty project) in visual studio. Is that it? I just went afk so will check when I get home
    Probably - in that case, the compiler would expect 'WinMain' instead of 'main', hence the unresolved external.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What is your file suffix? Is it by chance ".c"?

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Hey guys,

    Thanks for the help, I just needed to use the New Project --> Windows Console instead of Win32 App.

    While I'm here:

    Why is the code written to exit right away instead of keep the console up? Is it something I'm doing wrong or just the book doing it(you can barely see the text come up before exiting in most instances!) I know I'll learn more about the void and such so don't want to bother, just making sure I wasn't screwing that part up too

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    The code isn't written to exit right away. Rather, it isn't written to stay open. Console programs are meant to be run from the command-line; however Windows lets you try to run them like any other windows program. That is, they can be "double-clicked and then show up only for a second". If you run the program in console, the output will remain visible.

    This isn't in any way an error or flaw. The instant-close is understandable if you consider how Windows and console programs exit. A window will stay open until you click the X, at which point it sends a signal that the program has finished, and the window closes. Console programs, on the other hand, do not stay in a loop waiting for user input. They execute essentially top to bottom, and when the bottom is reached, it sends the "finished" signal. This also causes the window to close. So: the window closes when the program is done, in both cases.

    Your carrot program just isn't meant to be run in a non-console environment. Run things in console from now on!

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by claudiu View Post
    using namespace std; Should be above main not in it.
    Nope, it's a linker error. You can also put a using in a function just fine. In fact, sometimes it's good practice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 10:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM