This question already has answers here:
Why should I not include cpp files and instead use a header?
(14 answers)
Closed 5 years ago.
Can't understand what doesn't this freak(the Linker).
I tried to find answer in google but usually people do connect several times the same file.
I have the same Player.cpp connects once in the main file, and inside Player.cpp connected Player.h, that's all. What's wrong?
1>------Rebuild All started : Project: ConsoleApplication1, Configuration : Debug Win32------
1>stdafx.cpp
1>Player.cpp
1>ConsoleApplication1.cpp
1>Generating Code...
1>Player.obj : error LNK2005 : "public: __thiscall Player::Player(int,int)" (? ? 0Player##QAE#HH#Z) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: __thiscall Player::~Player(void)" (? ? 1Player##QAE#XZ) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: int __thiscall Player::getX(void)" (? getX#Player##QAEHXZ) already defined in ConsoleApplication1.obj
1>Player.obj : error LNK2005 : "public: int __thiscall Player::getY(void)" (? getY#Player##QAEHXZ) already defined in ConsoleApplication1.obj
1>C:\Users\New\Documents\Visual Studio 2017\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe : fatal error LNK1169 : one or more multiply defined symbols found
ConsoleApplication1.cpp:
#include "stdafx.h"
#include "windows.h"
#include <conio.h>
#include <iostream>
#include "main.h"
using namespace std;
void Render(CELL** vector);
void WaitForAction();
void Move(ACTION a);
int main()
{
CELL** vector = new CELL*[SIZE_MAP_Y];
for (int i = 0; i < SIZE_MAP_Y; i++) {
vector[i] = new CELL[SIZE_MAP_X];
for (int j = 0; j < SIZE_MAP_X; j++) {
vector[i][j] = CELL::EMPTY;
}
}
Player* player = new Player(2, 3);
cout << player->getX() << ", ";
cout << player->getY() << endl;
system("pause");
while (true) {
system("cls");
Render(vector);
WaitForAction();
}
for (int i = 0; i < 10; i++)
delete[] vector[i];
delete[] vector;
system("pause");
return 0;
}
void Render(CELL** vector) {
for (int y = 0; y < SIZE_MAP_Y; y++) {
for (int x = 0; x < SIZE_MAP_X; x++) {
if (vector[y][x] == CELL::EMPTY) cout << "#";
else if (vector[y][x] == CELL::PLAYER) cout << "O";
else cout << "?";
}
cout << endl;
}
}
void WaitForAction() {
char ch;
ch = _getch();
if (ch == 'ф' || ch == 'a') Move(ACTION::LEFT);
else if (ch == 'ы' || ch == 's') Move(ACTION::DOWN);
else if (ch == 'в' || ch == 'd') Move(ACTION::RIGHT);
else if (ch == 'ц' || ch == 'w') Move(ACTION::UP);
}
void Move(ACTION a) {
if (a == ACTION::LEFT) {
}
}
main.h:
#include "Player.cpp"
#define SIZE_MAP 10
#ifdef SIZE_MAP_Y
#undef SIZE_MAP_Y
#endif
#ifdef SIZE_MAP_X
#undef SIZE_MAP_X
#endif
#define SIZE_MAP_Y SIZE_MAP
#define SIZE_MAP_X (SIZE_MAP_Y * 2)
enum CELL { EMPTY, PLAYER };
enum ACTION { LEFT, RIGHT, DOWN, UP };
Player.cpp:
#include "stdafx.h"
#include "Player.h"
Player::Player(int x, int y)
{
this->x = x;
this->y = y;
}
Player::~Player()
{
}
int Player::getX() {
return this->x;
}
int Player::getY() {
return this->y;
}
Player.h:
#pragma once
class Player
{
public:
Player(int x, int y);
~Player();
int getX();
int getY();
private:
int x;
int y;
};
Include the Player.h header in your ConsoleApplication1.cpp source file:
#include "Player.h"
and remove the Player.cpp source file from your main.h header which is the cause of multiple definitions:
#include "Player.cpp" // <- remove this line
Declarations should go into header files, definitions should go inside source files. Source files (*.cpp) should include header files (*.h), not the other way around.
Related
while coding I got the following error: 1>giochino.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall entity::print_Pv(int,int)" (?print_Pv#entity##QAEXHH#Z) non risolto nella funzione _main 1>C:\Users\tomma\source\repos\giochino\Debug\giochino.exe : fatal error LNK1120: 1 esterni non risolti 1>Compilazione progetto "giochino.vcxproj" NON COMPLETATA.
it seems I couldn't find the solution
here's the code, although quite short I can't find the problem
The lib that I've created:
#include <iostream>
using namespace std;
class entity{
public:
int pv_left;
int Max_pv;
//find the percentage of life left and print a bar filled of the same percentage
void print_Pv(int pv_now, int pv_max) {
char pv_bar[10];
int pv_perc = ( pv_now / pv_max) * 10;
for (int i = 0; i < 10; i++) {
if (i <= pv_perc) {
pv_bar[i] = '*';
}
else if (i > pv_perc) {
pv_bar[i] = '°';
}
}
for (int i = 0; i < 10; i++) {
cout << pv_bar[i];
}
}
};
the header of the lib
#pragma once
#include <iostream>
class entity {
public:
int pv_left;
int Max_pv;
void print_Pv(int pv_now, int max_pv);
};
and the main method
#include "game_library.h"
using namespace std;
entity Hero;
int main()
{
Hero.Max_pv = 100;
Hero.pv_left = 10;
Hero.print_Pv(Hero.pv_left, Hero.Max_pv);
}
Your implementation file is wrong, you need
#include "your.h"
void entity::print_Pv(int pv_now, int pv_max) {
char pv_bar[10];
int pv_perc = ( pv_now / pv_max) * 10;
for (int i = 0; i < 10; i++) {
if (i <= pv_perc) {
pv_bar[i] = '*';
}
else if (i > pv_perc) {
pv_bar[i] = '°';
}
}
for (int i = 0; i < 10; i++) {
cout << pv_bar[i];
}
}
you must not declare the class again, just the method bodies that you didnt put in the .h file
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm a visual studio 2015 c++ newby who's trying to write some game code at home.
I'm getting this link error:
LNK2019 unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall display_utils::fit_int_2(int)" (?fit_int_2#display_utils##QAE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##H#Z) referenced in function "public: void __thiscall bat_stats::disp_bat_stats(struct bat_stats::bat_stats_typ)" (?disp_bat_stats#bat_stats##QAEXUbat_stats_typ#1##Z)
It apparently doesn't like the string I'm using to access the returned string from function fit_int_2. I've google searched for a solution, but can't find anything that fixes my problem. Note that the code compiled and linked before i I added the fit_int_2 call. Thanks in advance if you can help me out. The code is below:
bat_stats.h
#pragma once
class bat_stats
{
public:
struct bat_stats_typ
{
int gm;
int ab;
int ht;
int dbl;
int trpl;
int hr;
int rbi;
int sb;
int cs;
int bb;
int ibb;
int sf;
int sac;
int k;
int gidp;
int err;
float ave;
float slg;
float obp;
};
void disp_bat_hdr();
void disp_bat_stats( bat_stats_typ );
private:
int dummy;
};
bat_stats.cpp
#include <iostream>
using std::cout;
std::cin;
#include <string>
using std::string;
#include "bat_stats.h"
#include "display_utils.h"
void bat_stats::disp_bat_hdr()
{
cout << " G AB H 2B 3B HR RBI SB CS BB IW SF SH K GDP E AVE SLG OBP\n";
}
void bat_stats::disp_bat_stats( bat_stats_typ bat )
{
display_utils dut;
string s;
s = dut.fit_int_2( bat.gm ); // <- the problem is here!
cout << s << bat.gm << " ";
cout << bat.ab << "\n\n";
}
display_utils.h
#pragma once
#include <string>
using std::string;
class display_utils
{
public:
void insert_5_lines();
string fit_int_2( int );
private:
int dummy;
};
display_utils.cpp
#include <iostream>
using std::cout;
#include "display_utils.h"
void display_utils::insert_5_lines()
{
cout << "\n\n\n\n\n";
}
string fit_int_2(int i0)
{
string s0 = "";
if (i0 < 10)
{
s0 = " ";
}
return s0;
}
You need to change
string fit_int_2(int i0)
to
string display_utils::fit_int_2(int i0)
(You need to define the member function - currently you're defining an unrelated global function.)
This is my code, the exact error is at the bottom, I've looked throughout stack overflow and can't find an answer to my question. I have no idea why I'm getting this error
//amer_bi.h
#include <iostream>
using namespace std;
class amer_bi
{
public:
int steps,i,j;
double risk_free, price, strike, ttm, u, d, p, vol, disc, dt;
char sw;
double OptionPrice(double, double, double, double, double);
double max(double , double);
};
//amer_bi.cpp
#include "amer_bi.h"
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
#include <string>
//max function create
double max( double d1, double d2 )
{
return ( d1 > d2 ) ? d1 : d2;
}
double OptionPrice(double risk_free, double price, double strike, double ttm, double vol)
{
int steps;
steps = 200;
int i;
int j;
const int rows = steps+1;
const int cols = steps+1;
double dt = ttm/steps;
double u = exp(vol*sqrt(dt));
double d = exp(-vol*sqrt(dt));
double p = .5 + ((risk_free - .5*vol*vol)/(2*vol))*sqrt(dt);
double disc = exp(-risk_free*dt);
//pointer code for multidimensional dynamic array
double **price_array;
double **disc_array;
double **call_array;
price_array=new double*[rows];
disc_array=new double*[rows];
call_array=new double*[rows];
for(int i=0; i<rows; ++i)
{
price_array[i]=new double[cols];
disc_array[i]=new double[cols];
call_array[i]=new double[cols];
}
/*
//test data for book example
u = 1.1;
d = .9091;
disc = .9802;
p = .5820;
*/
char sw = 'c';
disc_array[steps][steps] = price*pow(d,steps);
for (i=steps; i > 0; i--)
{
disc_array[i-1][steps] = disc_array[i][steps]*u/d;
}
for (i=steps; i>=0; i--)
{
for (j=steps-1; j>=0; j--)
{
disc_array[i][j] = disc_array[i][j+1]*d;
}
}
for (i=steps; i >= 0; i--)
{
if (sw == 'c')
call_array[i][steps] = max(disc_array[i][steps] - strike, 0);
else
call_array[i][steps] = max(strike - disc_array[i][steps], 0);
}
price_array[0][steps] = price*pow(d,steps);
for (i=steps-1; i >=0; i--)
{
for (j=steps-1; j>=0; j--)
{
if (sw == 'c')
call_array[i][j] = max(disc*(p*call_array[i][j+1] + (1-p)*call_array[i+1][j+1]), disc_array[i][j] - strike);
else
call_array[i][j] = max(disc*(p*call_array[i][j+1] + (1-p)*call_array[i+1][j+1]), strike - disc_array[i][j]);
}
}
//std::cout << call_array[0][0] << endl;
return call_array[0][0];
}
//top.cpp
#include <iostream>
#include "amer_bi.h"
using namespace std;
int main ()
{
amer_bi exa;
exa.OptionPrice(.06, 100.0, 120.0, 1.0, .2);
system("pause");
return 0;
}
error LNK2019: unresolved external symbol "public: double __thiscall amer_bi::OptionPrice(double,double,double,double,double)" (?OptionPrice#amer_bi##QAENNNNNN#Z) referenced in function _main 1>C:\Users\Class2017\Documents\Visual Studio 2010\Projects\QF 465\Debug\amer_bi.exe : fatal error LNK1120:
The Error Your Getting is Because you are not implementing the method amer_bi::Option_Price. You Are Implementing the function Option_Price but not the method. As A result the linker is failing to find the methods implementation and raising a lnk2019 error. to implement a method of a class you either need to put the implementation in the class(marking the method as inline) or when implementing it out side the class refer to the name as amer_bi::Option_Price so that it knows what method to implement.
e.g.
//.hpp file
class foo{
public:
//method prototype
void method();
};
//.cpp file
//what you need to be doing
void foo::method(){
//method implementation
}
//what you are actually doing
void method(){
//function implementation
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm trying to test a program and every time I go to compile it, I get the error LNK2019: unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main. I was wondering what is causing this and how I can fix it. I've tried messing around with the code, but can't figure what is causing it.
Prog3Graph.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "Prog3Graph.h"
#include "GraphNode.h"
using namespace std;
int main()
{
Prog3Graph *test;
test = new Prog3Graph();
test->buildGraph("Graph.txt");
test->printGraph();
return 0;
}
bool Prog3Graph::buildGraph(char *fileName)
{
int i,j,index,numlinks, link;
char line[24];
ifstream inFile;
inFile.open(fileName, ifstream::in);
if(!inFile.is_open())
{
cout << "Unable to open file " << fileName << ". \nProgram terminating...\n";
return 0;
}
for(i=0;i<10;i++)
{
getNextLine(line,24);
index = atoi(line);
Nodes[i].setNodeID(index);
getNextLine(line,24);
Nodes[i].setNodeData(line);
getNextLine(line,24);
numlinks = atoi(line);
for(j=0;j<numlinks;j++)
{
getNextLine(line,24);
link = atoi(line);
AdjMatrix[i][link]=1;
}
inFile.close();
}
return true;
}
void Prog3Graph::printGraph()
{
int i,j;
cout << "------------------------------------------------------------\n\n";
cout << " Adjacency Matrix:\n\n";
cout << " 0 1 2 3 4 5 6 7 8 9\n";
cout << " +---------------+\n";
for(i=0; i<10; i++)
{
cout << i << "|";
for(j=0; j<10; j++)
{
cout << AdjMatrix[i][j] << "|";
}
cout << "\n +---------------+\n";
}
}
bool Prog3Graph::getNextLine(char *line, int lineLen)
{
int done = false;
ifstream inFile;
while(!done)
{
inFile.getline(line, lineLen);
if(inFile.good())
{
if(strlen(line) == 0)
continue;
else if(line[0] == '#')
continue;
else done = true;
}
else
{
strcpy(line, "");
return false;
}
}
return true;
}
Prog3Graph.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
class Prog3Graph
{
private:
ifstream inFile; // File stream to read from
int AdjMatrix[10][10];
GraphNode Nodes[10];
public:
Prog3Graph(); // Class constructor
~Prog3Graph(); // Class destructor
bool buildGraph(char *filename); // Read graph file, build graph
void printGraph(); // Print all data in graph
void depthFirstTraversal(); // Perform a depth first traversal
private:
bool getNextLine(char *line, int lineLen); // Read next line from graph file
};
GraphNode.cpp:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
#include "GraphNode.h"
using namespace std;
void GraphNode::setNodeID(int ID)
{
m_iNodeID = ID;
}
int GraphNode::getNodeID()
{
return m_iNodeID;
}
void GraphNode::setNodeData(char *data)
{
int i;
for(i=0;i<24;i++)
{
m_sNodeData[i] = data[i];
}
}
char *GraphNode::getNodeData()
{
return &m_sNodeData[24];
}
void GraphNode::setVisited(bool visited)
{
m_bVisited = visited;
}
bool GraphNode::hasBeenVisited()
{
return m_bVisited;
}
GraphNode.h:
#pragma once
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class GraphNode
{
private:
int m_iNodeID;
char m_sNodeData[24];
bool m_bVisited;
public:
GraphNode();
~GraphNode();
void setNodeID(int ID);
int getNodeID();
void setNodeData(char *data);
char *getNodeData();
void setVisited(bool visited);
bool hasBeenVisited();
};
Read the message carefully:
unresolved external symbol "public: __thiscall Prog3Graph::Prog3Graph(void)" (??0Prog3Graph##QAE#XZ) referenced in function _main.
You've declared a constructor (and a destructor) for your class, but you've never actually defined them.
It doesn't compile in Visual Studio, it says
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>position.exe : fatal error LNK1120: 1 unresolved externals
#include <iostream>
#include <cstdlib>
using namespace std;
class Position
{
private:
int row;
int column;
public:
Position(); //constructor
~Position(); //destructor
void setPos(int, int); //set the position
int getRow(); //return the current row
int getColumn(); //return the current column
void getPos(); //print the pos
bool compare(int, int); //compare a row and column with the one in the class
};
Position::Position()
{}
Position::~Position()
{}
void Position::setPos(int x, int y)
{
row = x;
column = y;
}
int Position::getRow()
{
return row;
}
int Position::getColumn()
{
return column;
}
void Position::getPos()
{
cout << "Row: " << row << "Column: " << column;
}
bool Position::compare(int x, int y)
{
if(x == row && y == column)
return true;
else
return false;
}
int main()
{
return 0;
}
It compiled for me under Visual Studio Professional 2008.
Try creating a new project.
Select File->New and specify Project Type of Visual C++ -> Win32 -> Win32 Console Application.
Then click Ok.
Then click on Application Settings and uncheck "Precompiled header".
Then paste in your code and compile it successfully.
Make sure you're creating an empty project or a Win32 console application. If you make a Win32 Windows application then you will get this error.