error LNK2019: unresolved external and LNK1120: 1 unresolved externals - c++

When compiling my code I receive this error.
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>C:\Users\Gabe Phelan\Documents\Visual Studio 2013\Projects\PA3 test\Debug\PA3 test.exe : fatal error LNK1120: 1 unresolved externals
#include <iostream>
#include <vector>
using namespace std;
class Heap{
private:
vector<int> heap;
int size;
public:
Heap(bool x);
};
#include "Header.h"
Heap::Heap(bool order){
int dummy = 0;
heap.push_back(dummy);
size = heap.size() - 1;
}

You have to have have an entry point, which is a function called main. So you need this,
int main(int argv, char* argc[])
{
//your code here
}
You dont declare classes in this, but what you want the program to execute.

Related

Can't create a runtime class

I'm using Visual Studio 2022 17.4.0 Preview 2.1, WinUI 3 with the WindowsAppSDK 1.1.5 and C++/WinRT 2.0.220929.3, and want to create a ListView using a template. In this goal I need to create a DataType which will be used by the template but can't compile it. I created the 3 following files:
Contact.idl
namespace Pine
{
[default_interface]
runtimeclass Contact
{
Contact();
}
}
Contact.h
#pragma once
#include "Contact.g.h"
namespace winrt::Pine::implementation
{
struct Contact : ContactT<Contact>
{
Contact();
};
}
namespace winrt::Pine::factory_implementation
{
struct Contact : ContactT<Contact, implementation::Contact>
{
};
}
Contact.cpp
#include "pch.h"
#include "Contact.h"
#if __has_include("Contact.g.cpp")
#include "Contact.g.cpp"
#endif
using namespace winrt;
using namespace Microsoft::UI::Xaml;
namespace winrt::Pine::implementation
{
Contact::Contact()
{
}
}
These files seem to correspond to every other runtimeclass I have seen, however it generates two linking errors:
1>Contact.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl winrt::Pine::implementation::ContactT<struct winrt::Pine::implementation::Contact>::Connect(int,struct winrt::Windows::Foundation::IInspectable const &)" (?Connect#?$ContactT#UContact#implementation#Pine#winrt##$$V#implementation#Pine#winrt##UEAAXHAEBUIInspectable#Foundation#Windows#4##Z)
1>Contact.obj : error LNK2001: unresolved external symbol "public: virtual struct winrt::Microsoft::UI::Xaml::Markup::IComponentConnector __cdecl winrt::Pine::implementation::ContactT<struct winrt::Pine::implementation::Contact>::GetBindingConnector(int,struct winrt::Windows::Foundation::IInspectable const &)" (?GetBindingConnector#?$ContactT#UContact#implementation#Pine#winrt##$$V#implementation#Pine#winrt##UEAA?AUIComponentConnector#Markup#Xaml#UI#Microsoft#4#HAEBUIInspectable#Foundation#Windows#4##Z)
1>C:\Users\user\source\repos\Pine\x64\Debug\Pine\Pine.exe : fatal error LNK1120: 2 unresolved externals
I want to create a Class which I'll be able to use in the following way in Xaml files:
<ListView>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:Contact">
...
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Thank you for your time.
There is a bug in the generated file Contact.g.h.
For some reasons, defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h") is true (the macro is not defined and the files does not exist) so the wrong code is used at the end of the file. I had to delete these lines below.
//#if defined(WINRT_FORCE_INCLUDE_CONTACT_XAML_G_H) || __has_include("Contact.xaml.g.h")
//
//#include "Contact.xaml.g.h"
//
//#else
namespace winrt::Pine::implementation
{
template <typename D, typename... I>
using ContactT = Contact_base<D, I...>;
}
//#endif

Unresolved external symbol referenced in function error

I've looked at other people's solutions to this problem and none of them, unfortunately, have solved my issue. I was having this error in my dev project, so I started from scratch, followed this tutorial to the letter, including the file names and locations, and I am still getting
1>unittest1.obj : error LNK2019: unresolved external symbol "public:
static int __cdecl HelloWorld::getTwo(void)"
(?getTwo#HelloWorld##SAHXZ) referenced in function "public: void
__thiscall UnitTest1::UnitTest1::TestMethodGetTwo(void)" (?TestMethodGetTwo#UnitTest1#1#QAEXXZ)
1>unittest1.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
HelloWorld::getN(void)const " (?getN#HelloWorld##QBEHXZ) referenced in
function "public: void __thiscall
UnitTest1::UnitTest1::TestMethodGetN(void)"
(?TestMethodGetN#UnitTest1#1#QAEXXZ)
The code is in the tutorial I linked but I will copy it below. I don't understand what I could be doing wrong at this point - my test project depends on my build project, my definitions for these functions are in the class.
HelloWorld.h
#pragma once
class HelloWorld
{
int n = 10;
public:
static int getTwo();
int getN() const;
};
HelloWorld.cpp
#include "stdafx.h"
#include "HelloWorld.h"
int main()
{
return 0;
}
int HelloWorld::getTwo()
{
return 2;
}
int HelloWorld::getN() const
{
return n;
}
unittest1.cpp (in test project)
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../HelloWorld/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethodGetTwo)
{
Assert::AreEqual(2, HelloWorld::getTwo());
}
TEST_METHOD(TestMethodGetN)
{
HelloWorld hw = HelloWorld();
Assert::AreNotEqual(0, hw.getN());
}
};
}
For some reason the linker can't find my definitions, and I'm out of ideas about why that might be.

Simple program gets LNK2019: unresolved external symbol Visual Studio 2013

I've been struggling with this error on a more complex solution for days, so I simplified a test project to see if I can't figure it out and it still gets the same error:
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> Test.cpp
1> MainThing.cpp
1> main.cpp
1> Generating Code...
1>MainThing.obj : error LNK2019: unresolved external symbol "public: static int __cdecl Test::add(int)" (?add#Test##SAHH#Z) referenced in function "public: void __thiscall MainThing::run(void)" (?run#MainThing##QAEXXZ)
1>P:\Leif's Documents\Visual Studio 2013\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
MainThing.h
#pragma once
class MainThing
{
public:
MainThing();
~MainThing();
void run();
private:
int result;
};
MainThing.cpp
#include "MainThing.h"
#include "Test.h"
#include <cstdlib>
#include <iostream>
MainThing::MainThing()
{
}
MainThing::~MainThing()
{
}
void MainThing::run() {
int result = Test::add(5);
std::cout << result;
}
Test.h
#pragma once
class Test
{
public:
static int add(int a);
};
Test.cpp
#include "Test.h"
int add(int a){
int b = a;
int c = 5;
int d = b + c;
return d;
}
main.cpp
#include <iostream>
#include "MainThing.h"
int main(int argc, char** argv){
MainThing mainGame;
mainGame.run();
return 0;
}
This has been super frustrating and the fact that it even effects a fairly simple recreation test without any external dependencies makes it that much more so.
I think you just have a typo. Test.cpp should be
#include "Test.h"
int Test::add(int a){
int b = a;
int c = 5;
int d = b + c;
return d;
}
The way you have, you've written a free function that other things in Test.cpp could use, but no one else knows about. The compiler is looking for Test's method called add and isn't finding one.

C++ WinSock2 Errors

Yesterday I've tried to make a socket server in C++, but I get errors upon compiling.
The errors:
Error 6 error LNK2019: unresolved external symbol _imp_socket#12 referenced in function "public: static unsigned long __cdecl Env::GetSocket(void)" (?GetSocket#Env##SAKXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 5 error LNK2019: unresolved external symbol _imp_listen#8 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 4 error LNK2019: unresolved external symbol _imp_htons#4 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 3 error LNK2019: unresolved external symbol _imp_bind#12 referenced in function "public: void __thiscall Network::Start(void)" (?Start#Network##QAEXXZ) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\Network.obj HabboV5
Error 2 error LNK2001: unresolved external symbol "public: static class Network * Env::Network" (?Network#Env##2PAV0#A) C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\HabboV5\HabboV5.obj HabboV5
Error 7 error LNK1120: 5 unresolved externals C:\Users\JoshuaTha\Documents\Visual Studio 2010\Projects\HabboV5\Debug\HabboV5.exe HabboV5
My main .cpp class:
// HabboV5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "Env.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout.write("hi", 2);
cout << "Hello World!" << endl;
Env::Network = new Network();
Env::Network->Start();
while (1)
{
char input[256];
cin.getline(input, 256);
}
}
Network.h:
#pragma once
#include <WinSock2.h>
class Network
{
private:
SOCKET socket;
public:
Network(void);
void Start();
};
Network.cpp:
#include "StdAfx.h"
#include "Network.h"
#include <WinSock2.h>
#include "Env.h"
Network::Network(void)
{
}
void Network::Start()
{
this->socket = Env::GetSocket();
SOCKADDR_IN sInformation;
sInformation.sin_family = AF_INET;
sInformation.sin_addr.s_addr = INADDR_ANY;
sInformation.sin_port = htons(30000);
bind(this->socket, (SOCKADDR*) (&sInformation), sizeof(sInformation));
listen(this->socket, 10);
}
Env.h:
#include "stdafx.h"
#include "Network.h"
#include <WinSock2.h>
class Env
{
public:
static Network* Network;
static DWORD GetSocket()
{
return socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
};
In the linker options (on the project right-click, linker, input) you need add wsock32.lib or ws2_32.lib to the list of input files.

unresolved external errors

I have the following .h and .cpp files
If i have to I will include the full codes of the function definitions
When i compile my program i get the errors shown at the end
hash.h
#define BUCKETS 64
#define B_ENTRIES 50000
int curr_tanker;
typedef unsigned long int ulong;
typedef struct bucket
{
int bucket_id;
ulong bucket_entries;
}bucket;
typedef struct tanker_record
{
ulong tanker_id;
ulong tanker_size;
ulong num_of_entries;
ulong bucket_entry_count;
}tanker_record;
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
struct fpinfo* InitHTable(fpinfo *);
int CreateTanker(tanker_record tr[]);
int Hash_CreateEntry(struct fpinfo *,struct fpinfo he,tanker_record tr);
ht.cpp
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include <iostream>
#include "ht.h"
struct fpinfo* InitHTable(struct fpinfo ht[][B_ENTRIES])
{
}
int CreateTanker(tanker_record tr[])
{
}
int
Hash_CreateEntry(struct fpinfo *t[][B_ENTRIES],struct fpinfo he,tanker_record tr[])
{
}
static void
WriteHTtoFile(struct fpinfo *t[][B_ENTRIES],int this_tanker)
{
}
main.cpp
#include<iostream>
#include"ht.cpp"
#include<conio.h>
#include<stdlib.h>
void main(int argc, char **argv)
{
static fpinfo hash_table[BUCKETS][B_ENTRIES];
static tanker_record tr[100];
InitHTable(&hash_table[0][0]);
CreateTanker(tr);
struct fpinfo fp;
...
ar = Hash_CreateEntry(&hash_table[0][0], fp,tr[0]);
i get the following errors when i try to compile it using vc2010
1>main.obj : error LNK2005: "struct fpinfo * __cdecl InitHTable(struct fpinfo (* const)[50000])" (?InitHTable##YAPAUfpinfo##QAY0MDFA#U1##Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl CreateTanker(struct tanker_record * const)"
(?CreateTanker##YAHQAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int __cdecl Hash_CreateEntry(struct fpinfo * (* const)[50000],struct fpinfo,struct tanker_record * const)" (?Hash_CreateEntry##YAHQAY0MDFA#PAUfpinfo##U1#QAUtanker_record###Z) already defined in ht.obj
1>main.obj : error LNK2005: "int curr_tanker" (?curr_tanker##3HA) already defined in ht.obj
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl Hash_CreateEntry(struct fpinfo *,struct fpinfo,struct tanker_record)"
(?Hash_CreateEntry##YAHPAUfpinfo##U1#Utanker_record###Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "struct fpinfo * __cdecl InitHTable(struct fpinfo *)" (?InitHTable##YAPAUfpinfo##PAU1##Z) referenced in function _main
THANKS FOR YOUR HELP!!
You're including ht.cpp from main.cpp, which will include all the definitions of functions already defined in ht.cpp itself.
You want to include ht.h instead.
It won't help in this situation, but you should also protect the header file with include guards:
#ifndef HT_H
#define HT_H
// contents of ht.h
#endif
You also need the arguments of the function declarations to match those of the definitions:
struct fpinfo* InitHTable(struct fpinfo[][B_ENTRIES]);
// Missing: ^^^^^^^^^^^
int CreateTanker(tanker_record tr[]); // OK
int Hash_CreateEntry(struct fpinfo*[][B_ENTRIES],struct fpinfo,tanker_record[]);
// Missing ^^^^^^^^^^^^^ ^^
Add an "include guard" in your header, so that it its contents aren't "seen" twice after preprocessing. For Microsoft, #pragma once at the beginning of the .h file. In general, add:
#ifndef __YOUR_HEADER_H
#define __YOUR_HEADER_H
// all the stuff from the header here
#endif
Make sure to adopt a consistent "unique" naming scheme for each of your headers. __YOUR_HEADER_H would do, for example customio.h into __CUSTOM_IO_H.