Solved

Header Errors LNK2019, LNK1120 - Unresolved Externals

Posted on 2016-10-25
4
180 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
ID: 41859508
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
ID: 41859520
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 33

Accepted Solution

by:
sarabande earned 250 total points
ID: 41859922
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
ID: 41860069
Thank you all so much. I followed sarabande's advice and it's working perfectly now.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

789 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