Solved

Header Errors LNK2019, LNK1120 - Unresolved Externals

Posted on 2016-10-25
4
141 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

831 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