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.
Related
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(){}
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.
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.
I'm getting these error messages
2>main.obj : error LNK2019: unresolved external symbol "public: __thiscall CEngine::CEngine(void)" (??0CEngine##QAE#XZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::SetWindowSize(int,int,char const *,int)" (?SetWindowSize#CEngine##QAEXHHPBDH#Z) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall CEngine::Begin(void)" (?Begin#CEngine##QAEXXZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayWidth(void)" (?GetDisplayWidth#CEngine##QAEHXZ) referenced in function _WinMain#16
2>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall CEngine::GetDisplayHeight(void)" (?GetDisplayHeight#CEngine##QAEHXZ) referenced in function _WinMain#16
2>C:\Users\ethan\Desktop\C++ Projects\delveenginetest\Debug\delveenginetest.exe : fatal error LNK1120: 5 unresolved externals
This is my solution:
Solution 'delveenginetest' (2 projects)
DelveEngine
Include
delve.h
Engine.h
SetupSDL.h
stdafx.h
Engine.cpp
Main.cpp
SetupSDL.cpp
This is the code for Engine.h
#pragma once
#include "SetupSDL.h"
class CEngine
{
public:
CEngine(void);
~CEngine(void);
void SetWindowSize(int winW, int winH, const char* GameName, int windowMode);
void Begin(void);
int GetDisplayWidth(void);
int GetDisplayHeight(void);
private:
int deskW;
int deskH;
bool playing;
CSetupSDL* sdl_setup;
};
Code for Engine.cpp
#include "Include/stdafx.h"
#include "Include/Engine.h"
CEngine::CEngine(void)
{
playing = true;
deskW = GetSystemMetrics(SM_CXSCREEN);
deskH = GetSystemMetrics(SM_CYSCREEN);
}
CEngine::~CEngine(void)
{
}
void CEngine::SetWindowSize(int winW, int winH, const char* GameName, int windowMode)
{
// set up SDL for use
sdl_setup = new CSetupSDL(winW, winH, GameName, windowMode);
}
void CEngine::Begin(void)
{
while (playing && sdl_setup->GetMainEvent()->type != SDL_QUIT)
{
sdl_setup->Begin();
sdl_setup->End();
}
playing = false;
}
int CEngine::GetDisplayWidth(void){ return deskW; }
int CEngine::GetDisplayHeight(void){ return deskH; }
The DelveEngine project builds successfully, whereas the delveenginetest project fails.
What's wrong? I've looked everywhere for a reason, can't find one that suits me.
Despite the fact you're not providing all the sufficient information for a correct diagnosis of your problems, I'll try to share what I could imagine that might be the reasons for the linker errors:
I suppose the project delveenginetest you mention is meant to set up unit tests for the classes from the DelveEngine project.
Since you have a Main.cpp in your DelveEngine project, I'd guess it's simply build as an executable (successfully).
Your delveenginetest needs to link to the classes provided from the DelveEngine project, but that's actually not possible, since the .exe from DelveEngine can't be used for linking, you'll need a library to import it to another executable (the unit testing framework).
I'd recommend to separate out your classes/source files from DelveEngine project to make up a static or shared library, that can be linked from an application and the test framework simultaneously from within a single VS solution:
Solution 'DelveEngine' (3 projects)
DelveEngineLib (project [.lib/.dll])
Include
delve.h
Engine.h
SetupSDL.h
Engine.cpp
SetupSDL.cpp
DelveEngine (project [.exe])
Main.cpp
delveenginetest (project [.exe])
Main.cpp (TestFramework main definition)
Since I'm not very versed with it I don't know actually, if VS 2013 supports to setup projects consuming virtual resources (think of links to sources in the actual build environment), but this could be an alternative how to setup application and unit tests without need of an extra library.
I have a visual c++ console project named "C_test".
Another project is called "dll_project" which is dll project.
in "C_test" project, I have set the location of additional include directory for "dll_project".
I have these simple codes.
in "C_test" project's main.cpp.
#include "SampleClass.h"
int main()
{
SampleClass *_parser = new SampleClass();
return 0;
}
And in "dll_project" SampleClass.h and SampleClass.cpp.
SampleClass.h
#pragma once
class SampleClass
{
public:
SampleClass(void);
~SampleClass(void);
};
SampleClass.cpp
#include "SampleClass.h"
SampleClass::SampleClass(void)
{
}
SampleClass::~SampleClass(void)
{
}
When I try to debug or build, I get this error.
error LNK2019: unresolved external symbol "public: __thiscall SampleClass::SampleClass(void)" (??0SampleClass##QAE#XZ) referenced in function _main C:\C_test\main.obj C_test
What have I done wrong?