LNK2019 Error when compiling .cpp - c++

First, this is hw and I'm supposed to change only one function. But I'm not here for help on that function, I'm here for an LNK error.
Here's the relevant output on the Developer Command Prompt after I type in cl shortest.cpp (with spaces between errors for easier reading):
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::readFile(char *)" (?readFile#Graph##QAEXPAD#Z) referenced in function _main
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::dijkstra(int)" (?dijkstra#Graph##QAEXH#Z) referenced in function _main
shortest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Graph::printPath(int)" (?printPath#Graph##QAEXH#Z) referenced in function _main
shortest.exe : fatal error LNK1120: 3 unresolved externals
I understand this is supposed to mean I haven't defined those symbols, but they should have been predefined for me in one of two files: graph.cpp or graph.h. Graph.h seems more likely, as it contains the declarations of each of these "unresolved" symbols.
graph.h:
#include <iostream>
#include <fstream>
#include <vector>
#include <limits.h>
#include <cstdlib>
#define INFINITY INT_MAX
using namespace std;
class Graph
{
struct Edge
{
int dest; // destination vertex number
int cost; // edge weight
Edge *next;
};
struct Vertex
{
Edge *adj;
bool known;
int dist;
int path;
};
vector<Vertex *> vertices;
vector<int> PQ;
int PQsize;
void PQinsert (int v);
int PQdeleteMin ();
public:
Graph () { PQsize=0; }
int numVertices () { return vertices.size(); }
int dist (int dest) { return vertices[dest]->dist; }
void readFile (char *name);
void dijkstra (int start);
void printPath (int dest);
};
I won't post graph.cpp or shortest.cpp (which only contains the main), unless requested, as it doesn't seem to me that the content of the related functions is an issue. If I do, I will only post the related methods to keep it shorter. But graph.cpp, graph.h, and shortest.cpp are all in the same folder. And shortest.cpp does include graph.h. I'm only supposed to change the dijkstra method, but if I can add something that will make this compile without changing way the methods or class themselves work (which it shouldn't have to) I don't see a problem.

You have to use make file to build with all the source files. Otherwise you can first compile the sources and then link manually. Search google for individual commands for compiling/linking.

Related

LNK2019 Error: unresolved external symbol

I am getting an error in Visual Studio when compiling my program.
Error LNK2019 unresolved external symbol "public: __cdecl
Grid::Grid(void)" (??0Grid##QEAA#XZ) referenced in function
main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall
Grid::~Grid(void)" (??1Grid##QAE#XZ) referenced in function
_main Grid C:\Users\Ryan\Desktop\Dev\Grid\Grid\main.obj 1
This project works fine at my university but not on my own computer and I am not sure what is wrong.
My main.cpp:
#include <iostream>
#include "Grid.h"
using namespace std;
int main(int args, char **argv)
{
Grid grid;
// grid.LoadGrid("Grid1.txt");
// grid.SaveGrid("OutGrid.txt");
system("pause");
}
And my header file:
#pragma once
class Grid
{
public:
Grid();
~Grid();
void LoadGrid(const char filename[]);
void SaveGrid(const char filename[]);
private:
int m_grid[9][9];
};
Any help at all is appreciated, thanks.
Issue resolved from advise given on [error LNK2019: unresolved external symbol "public: __thiscall : constructor issue
"First in the library project do rightclick->properties, then under the tab General, Configuration Type should be Static library (.lib)."
Thanks everyone for your answers.
As per my understanding your grid class constructor and destractor implementation are missing. You should check your .cpp file, implemention like this
Grid(){}
~ Grid(){}

Getting "error LNK2019: unresolved external symbol ... "

I'm trying to get started with C++ but I keep getting this error. I know which parts of my code is generating it, but I think that at least one these parts shouldn't generate them.
I am creating a class called Text that is functioning in a way similar to the std::string class, just to experiment and get a better understanding of value semantics.
Anyhow, these are my files:
Text.h:
#ifndef TEXT
#define TEXT
class Text {
public:
Text(const char *str);
Text(const Text& other);
void operator=(const Text& other);
~Text();
private:
int size;
char* cptr;
};
#endif
Text.cpp:
#include "Text.h"
#include <cstring>
#include <iostream>
using namespace std;
Text::Text(const char* str) {
size = strlen(str) + 1;
cptr = new char[size];
strcpy(cptr, str);
}
Text::Text(const Text& other) {
size = other.size;
cptr = new char[size];
strcpy(cptr, str);
}
void Text::operator=(const Text& other){
delete [] cptr;
size = other.size;
cptr = new char[size];
strcpy(cptr, other.ctpr);
}
Text::~Text() {
delete [] cptr;
}
Main.cpp:
#include <iostream>
#include "Text.h"
using namespace std;
Text funk(Text t) {
// ...
return t;
}
int main() {
Text name("Mark");
Text name2("Knopfler");
name = funk(name);
name = name2;
return 0;
}
So what's causing the error is the function funk, and the first two lines in the main function. I get why it's complaining on the first two lines in the main function, because there are no function called "name" or "name2". But what I'm trying to do is declaring and initialize an object in one line (I'm and old Java guy :p), is this even possible in C++? I can't find anything online indicating this.
The funny thing is that this code is more or less copied from some code my lecturer executes just fine during a lecture. And he has certainly not declared any functions named "name" and "name2" either. Any reasonable explanation for this?
But why is the function funk generating this error as well? All I am doing is returning a copy of the object that I'm sending in.
Thanks in advance!
Edit: Here comes the full error messages. There are five of them. "SecondApplication" is the name of my project.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Text::Text(char const *)" (??0Text##QAE#PBD#Z) referenced in function _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Text::Text(class Text const &)" (??0Text##QAE#ABV0##Z) referenced in function "class Text __cdecl funk(class Text)" (?funk##YA?AVText##V1##Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall Text::operator=(class Text const &)" (??4Text##QAEXABV0##Z) referenced in function _main C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Text::~Text(void)" (??1Text##QAE#XZ) referenced in function "class Text __cdecl funk(class Text)" (?funk##YA?AVText##V1##Z) C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\SecondApplication.obj SecondApplication
Error 5 error LNK1120: 4 unresolved externals C:\Users\XXX\Documents\Visual Studio 2013\Projects\SecondApplication\Debug\SecondApplication.exe 1 1 SecondApplication
You'll get the link errors (not compilation errors) that you see if you, for instance, forget to add "Text.cpp" to your project so it doesn't get compiled and linked.
There are two errors in the code - one is in the copy constructor, and one is in the assignment operator.
Since the compiler didn't complain about the two errors, I suspect you forgot to add the file to the project.

Unresolved Externals when using polymorphism

Hi i'm getting the following errors:
Error 9 error LNK1120: 2 unresolved externals
Error 8 error LNK2019: unresolved external symbol "public: virtual __thiscall physics::~physics(void)" (??1physics##UAE#XZ) referenced in function "public: virtual void * __thiscall physics::`scalar deleting destructor'(unsigned int)" (??_Gphysics##UAEPAXI#Z)
Error 7 error LNK2019: unresolved external symbol "public: virtual __thiscall student::~student(void)" (??1student##UAE#XZ) referenced in function __unwindfunclet$??0physics##QAE#XZ$0
which occur using the following code:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class student{
protected:
string fName,sName;
int id;
vector<string> cname;
vector<int> cmark;
public:
virtual ~student();
virtual void addCourse(string name, int mark)=0;
};
class physics : public student{
public:
physics(){//Initialize default values
fName="blank";
sName="blank";
id=0;
cname.push_back("none");
cmark.push_back(0);
};
~physics();
void addCourse(string name, int mark){
if(cname.size()==1){//override default value for a size of 1
cname[0]=name;
cmark[0]=mark;
}
else{
cname.push_back(name);
cmark.push_back(mark);
}
}
};
The above compiles fine but when i try to initialize an object in main() by using:
int main(){
//Database Storage
vector<student*> DB;
DB.push_back(new physics);
}
That's when i get the errors (removing the push_back line fixes it but i need this for my program). What am i doing wrong?
Turns out adding braces to the end of the destructors fixed it. What difference does that make? (from the comments)
The difference is that in one case you have a declaration which lacks a definition; in the second case you provide a (empty) definition inline.
Trying to invoke a function that is declared but not defined (as in the first case) result in an unresolved reference error raised by the linker - after all, what should it do when a function invocation is found for a function whose implementation is not available?

LNK2019: unresolved external symbol but I have coded the function

Can somebody help me with this? I'm using Visual Studio 2010
I'm getting this message and I don't know how to solve this.
1> Generating Code...
1>dct.obj : error LNK2019: unresolved external symbol "public:
__thiscall Amostras::Amostras(class std::basic_string,class std::allocator >)"
(??0Amostras##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
referenced in function _main
1>C:\Users\redneck\documents\visual studio
2010\Projects\dct\Debug\dct.exe : fatal error LNK1120: 1 unresolved
externals
Here is some of the *.cpp file:
class Amostras {
public:
int original[10][257];
int idct[10][257];
float dct[10][257];
int grupos;
Amostras::Amostras(void)
{
for (int i=0;i<10;i++)
{
this->original[i][0]=0;
this->dct[i][0]=0.0;
this->idct[i][0]=0;
}
this->grupos=0;
}
Amostras::Amostras(string arquivo)
{
int n_samples=0,linha=0,coluna=0;
int cont;
..
and here is the *.h
class Amostras {
public:
int original[10][257];
int idct[10][257];
float dct[10][257];
int grupos;
Amostras::Amostras();
Amostras::Amostras(string arquivo);
void Amostras::mostra(void);
};
main
int main(void)
{
Amostras *amostra = new Amostras("in.txt");
dct(amostra,0);
show(amostra,0);
amostra->mostra();
return 0;
}
hope it helps, i'm running out of options here :(
Solution:
So what I did was just putting the class just in *.h and then including the *.h in the class *.cpp that only has the methods and functions of that class. It worked!
This linker error usually means that you have prototyped a function but forgotten to define it. Make sure that you have implemented the function
Amostras::Amostras(string arg);
somewhere, and that when you were linking your code that the object file containing that implementation was linked in.
Hope this helps!
you forgot to define the Amostras::Amostras(string arg);
though declared in your *.h file
Amostras::Amostras(string arg)
{
}
copy the above code in your *.cpp file
OR
you can also do this by commenting the line from your *.h file.
//Amostras::Amostras(string arg);
whoa! do you have a *.h file ? if you are working in only *.cpp then lemme know.

error LNK2019: unresolved external symbol - what am I doing wrong?

When I try and build my project I get the following errors:
1>Assignment1CoreTest.obj : error LNK2019: unresolved external symbol "struct point * __cdecl findLongPaths(struct point *,double)" (?findLongPaths##YAPAUpoint##PAU1#N#Z) referenced in function "public: void __thiscall Geometry_CoreUnitTest::test_method(void)" (?test_method#Geometry_CoreUnitTest##QAEXXZ)
1>Assignment1CoreTest.obj : error LNK2019: unresolved external symbol "double __cdecl calculateLineLength(struct point *)" (?calculateLineLength##YANPAUpoint###Z) referenced in function "public: void __thiscall Geometry_CoreUnitTest::test_method(void)" (?test_method#Geometry_CoreUnitTest##QAEXXZ)
1>C:\Users\user\documents\visual studio 2010\Projects\Assignment1\Debug\Assignment1.exe : fatal error LNK1120: 2 unresolved externals
I've been trying to work out why for the last hour or so and have made absolutely no progress so I was wondering if anyone might be able to point me in the right direction. Obviously I'm doing something stupid but I can't work out what.
This is my AssignmentOneCoreTest.cpp:
#define BOOST_TEST_MODULE Test_Assignment1
#include <boost/test/unit_test.hpp>
#include "geometry.h"
BOOST_AUTO_TEST_CASE(Geometry_CoreUnitTest) {
point p[3] = {{0,0}, {0,3}, {0,1, true}};
point longest[2] = {{0,1}, {0,3,true}};
BOOST_CHECK_EQUAL(calculateLineLength(p), 5);
point *longest_calculated = findLongPaths(p, 1.1);
BOOST_CHECK_EQUAL(longest_calculated[1].y, longest[1].y);
delete longest_calculated;
}
Geometry.cpp:
#include "geometry.h"
#include <iostream>
using namespace std;
double calculateLineLength(point *points)
{
...
}
point *findLongPaths(point *points, double threshold_distance)
{
...
}
and Geometry.h:
#ifndef GEOMETRY_H
#define GEOMETRY_H
typedef struct {
int x;
int y;
bool end;
} point;
double calculateLineLength(point *points);
point *findLongPaths(point *points, double threshold_distance);
#endif
I'm totally stumped and starting to get kinda frustrated, what am I overlooking?
you are getting linker error.
Most probably you are not generating the object code for Geometry.cpp
this would work for now:
create an empty project;
copy the header files in the headerfiles folder
copy the cpp files in the cpp files folder
then build the project;
this will build your Geometry.cpp program as well.