Solved

Header Errors LNK2019, LNK1120 - Unresolved Externals

Posted on 2016-10-25
4
57 Views
Last Modified: 2016-10-26
Hello all.

I'm trying to create a simple header and implement it in a cpp file, but I am experiencing the following error messages.

LNK2019 unresolved external symbol _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
LNK1120 1 unresolved externals

Here is the code for my header.

#include <iostream>

class Cat
{
public:
	Cat(int initialAge);
	~Cat();
	int GetAge() const { return itsAge; }
	void SetAge (int age) { itsAge = age; }
	void Meow() const { std::cout << "Meow.\n"; }
private:
	int itsAge;
};

Open in new window


Here is the code for the cpp file I am trying to compile.

#include "Cat.h"


Cat::Cat(int initialAge)
{
	itsAge = initialAge;
}

Cat::~Cat()
{
}

int main()
{
	Cat Frisky(5);
	Frisky.Meow();
	std::cout << "Frisky is a cat who is " << Frisky.GetAge() << " years old.\n";
	Frisky.Meow();
	Frisky.SetAge(7);
	std::cout << "Now Frisky is " << Frisky.GetAge() << " years old.\n";
	return 0;
}

Open in new window


Both have been copied directly from a book, and try as I might I can't see any mistakes in my transcription.

Many thanks in advance!
0
Comment
Question by:blossompark
4 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 125 total points
Comment Utility
Since I didn't see anything wrong with your code either, I created an empty project in VS 2015 Community edition. Using your code unaltered, I built/ran it and got this output:
Meow.
Frisky is a cat who is 5 years old.
Meow.
Now Frisky is 7 years old.

Open in new window

If you create a new  empty project in Visual Studio, you should be able to build with no problems. Just avoid creating a Win32 Project.
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 125 total points
Comment Utility
The problem is that you've probably created a Windows project rather than a Command line project. The entry point for Windows projects is WinMain and not main. The main function already exists inside the Windows code framework and this then calls WinMain as the application entry point (hence, it's complaining about WinMain not existing when called from main).

https://msdn.microsoft.com/en-us/library/windows/desktop/ff381406(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/ms633559%28v=vs.85%29.aspx
0
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
Comment Utility
you could try to change the project properties at menu

Projects - Properties - [All Configurations] - Configuration Properties - Linker - System - SubSystem

and choose Subsystem 'Console'.

Sara
0
 

Author Closing Comment

by:blossompark
Comment Utility
Thank you all so much. I followed sarabande's advice and it's working perfectly now.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now