cant get sample program to compile

Just starting out and trying to compile the sample programs as I read along.

in the below code I keep getting
Severity Code Description Project File Line Suppression State
Error LNK1120 1 unresolved externals LoidTest C:\Users\Lloyd\source\repos\LoidTest\Debug\LoidTest.exe 1

I would appreciate showing me the error of my ways
Thank you


#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
string mystr;
float price = 0;
int quantity = 0;

cout << "Enter price: ";
getline(cin, mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline(cin, mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price * quantity << endl;
return 0;
}



Well, your program is correct, and builds for me with Visual Studio 2017.
I'm not sure which part of it wouldn't be linking properly, since you're only using the standard library.

Disch wrote:
99 times out of 100, "unresolved external symbol" means you are trying to call a function that doesn't have a body. The name of the function should be in the error message.
Do you have the text "LoidTest" somewhere in your actual code that you aren't showing? (not just the filename)

What IDE are you using? Visual Studio 2017? Visual Studio 2019?
Trying making a new, empty C++ project and pasting the code there.

If that doesn't work, then replace the contents of main with just cout << "hello\n";.
Does that build?
Last edited on
¿unresolved reference to what?
paste the full error message

my guess is that you used a C (not C++) linker
Thanks for the quick response

I am using Visual Studio 2019

I made new project - same error

the code below is also erroring - I wonder if there is some visual studio option that I did not set up?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
cout << "hello\n";
}
Please post the full error message, not an edited version. The full error message will be telling you which reference is undefined, which is pretty crucial to solving your issue. Why did you think withholding that information would be a helpful thing to do?

What type of project are you creating?
Are you sure you're posting the full error message?

Possibly read this:
https://stackoverflow.com/questions/7410798/c-fatal-error-lnk1120-1-unresolved-externals
When you created the project, you made the wrong choice of application type. When asked whether your project was a console application or a windows application or a DLL or a static library, you made the wrong chose windows application (wrong choice).

Go back, start over again, go to File -> New -> Project -> Win32 Console Application -> name your app -> click next -> click application settings.

For the application type, make sure Console Application is selected (this step is the vital step).
For the application type, make sure Console Application is selected (this step is the vital step).

wow thank you... I was using the wrong option

and when I sent the error earlier I had coped the entire line...

anyway Thanks so much for your help... my bad
The error message of interest would have contained something like "unresolved external symbol WinMain@16 referenced in function [name of function]".

When I reproduce your issue on my machine, under the Error List tab, you should actually see two errors.

The second error is the one you posted:
Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK1120	1 unresolved externals	cplusplus26300_bad	C:\TestProjects\cplusplus263000_bad\Debug\cplusplus26300_bad.exe	1	


But the first error message gives the more important information:
Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)	cplusplus26300_bad	C:\TestProjects\cplusplus263000_bad\cplusplus26300_bad\MSVCRTD.lib(exe_winmain.obj)	1	


Also, if you look at the Output tab, you will see consolidated information:
1>------ Build started: Project: cplusplus26300_bad, Configuration: Debug Win32 ------
1>stdafx.cpp
1>cplusplus26300_bad.cpp
1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
1>C:\TestProjects\cplusplus263000_bad\Debug\cplusplus26300_bad.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "cplusplus26300_bad.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


The output of the "Output" tab is probably the best to post on a forum, for next time.
Last edited on
Topic archived. No new replies allowed.