Solved

Header Errors LNK2019, LNK1120 - Unresolved Externals

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

919 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

17 Experts available now in Live!

Get 1:1 Help Now