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
Related
I have been attempting to write this program where I am required to utilize dynamically allocated arrays to print out a 2d matrix. I am only to write the cpp files and not allowed to modify anything in the header files.
I keep getting an exception
0 [main] review2_cis17c_objectarray 4018 cygwin_exception::open_stackdumpfile: Dumping stack trace to review2_cis17c_objectarray.exe.stackdump
I am relatively new to learning c++; after contemplating, I think something is wrong in my PlusTab.cpp, where I am trying to assign an allocated address to a constructor-defined array in a class. Can someone please help and let me know here I did wrong in the project? Thank you very much!
AbsRow.h:
class AbsRow {
protected:
int size;
int *rowData;
public:
virtual int getSize()const = 0;
virtual int getData(int)const = 0;
};
AbsTabl.h:
class AbsTabl {
protected:
int szRow;
int szCol;
RowAray **columns;
public:
virtual int getSzRow()const = 0;
virtual int getSzCol()const = 0;
virtual int getData(int,int)const = 0; };
PlusTab.h
class PlusTab:public Table {
public:
PlusTab(unsigned int r,unsigned int c):Table(r,c){};
PlusTab operator+(const PlusTab &);
};
RowAray.h
class RowAray:public AbsRow {
public:
RowAray(unsigned int);
virtual ~RowAray();
int getSize()const{return size;}
int getData(int i)const{
if(i>=0&&i<size)return rowData[i];
else return 0;}
void setData(int,int);
};
Table.h
#include "AbsTabl.h"
class Table:public AbsTabl {
public:
Table(unsigned int,unsigned int);
Table(const Table &);
virtual ~Table();
int getSzRow()const {return szRow;}
int getSzCol()const {return szCol;}
int getData(int,int)const;
void setData(int,int,int);
};
PlusTab.cpp:
#include "PlusTab.h"
PlusTab PlusTab::operator+(const PlusTab &t) {
PlusTab tab(this->getSzRow(), this->getSzCol());
for(int i = 0; i < tab.getSzRow(); i++) {
for (int j = 0; j <tab.getSzCol(); j++) {
(tab.columns[i])->setData(j, this->getData(i,j) + t.getData(i,j));
}
}
return tab;
}
RowAray.cpp:
#include "RowAray.h"
RowAray::RowAray(unsigned int c) {
size = c;
rowData = new int[c];
}
RowAray::~RowAray() {
delete []rowData;
}
void RowAray::setData(int i, int value) {
rowData[i] = value;
}
Table.cpp:
#include "Table.h"
#include <cstdlib>
Table::Table(unsigned int r, unsigned int c) {
szRow = r;
szCol = c;
columns = new RowAray*[r];
for (int i = 0; i < r; i++) {
columns[i] = new RowAray(c);
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
columns[i]->setData(j, (rand()%90 + 10));
}
}
}
Table::~Table() {
for (int i = 0; i < szRow; i++) {
delete []columns[i];
}
delete []columns;
}
Table::Table(const Table &t) {
szRow = t.szRow;
szCol = t.szCol;
columns = t.columns;
};
int Table::getData(int r ,int c) const {
return columns[r]->getData(c);
};
void Table::setData(int r, int c, int value) {
columns[r]->setData(c,value);
}
and finally my main.cpp, which I am not allowed to modify either.
#include <ctime>
#include <iostream>
#include <iomanip>
using namespace std;
//User Libraries
#include "PlusTab.h"
//Global Constants
//Function Prototype
void prntTab(const Table &);
//Execution Begins Here!
int main(int argc, char** argv) {
//Initialize the random seed
srand(static_cast<unsigned int>(time(0)));
//Declare Variables
int rows=3,cols=4;
//Test out the Tables
PlusTab tab1(rows,cols);
PlusTab tab2(tab1);
PlusTab tab3=tab1+tab2;
// Print the tables
cout<<"Abstracted and Polymorphic Print Table 1 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab1);
cout<<"Copy Constructed Table 2 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab2);
cout<<"Operator Overloaded Table 3 size is [row,col] = ["
<<rows<<","<<cols<<"]";
prntTab(tab3);
//Exit Stage Right
return 0;
}
void prntTab(const Table &a){
cout<<endl;
for(int row=0;row<a.getSzRow();row++){
for(int col=0;col<a.getSzCol();col++){
cout<<setw(4)<<a.getData(row,col);
}
cout<<endl;
}
cout<<endl;
}
I apologize for this massive amount of code. This is my first time posting, will learn to use the website! I appreciate your help:)
I'm having issues with passing an array of structures to a function that searches them. I delcare an array of structs outside of main then copy it to a new array of structs inside of main (so I have access to them inside main and can pass them easier). Not sure why it is failing though. Can anyone help me?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 2000000;
const string DFile = "DFile.dms";
const string EFile = "EFile.dms";
const string VFile = "VFile.dms";
struct dogs
{
int did;
int age;
} DFBuffer[MAX];
struct examine
{
int vid;
int did;
int fee;
} EFBuffer[MAX];
struct vet
{
int vid;
int eLevel;
} VFBuffer[MAX];
void readDF(ifstream&);
void readEF(ifstream&);
void readVF(ifstream&);
int getLineCount(ifstream&);
bool dogCompare(dogs lhs, dogs rhs) {return lhs.did < rhs.did;}
bool vetCompare(vet lhs, vet rhs) {return lhs.vid < rhs.vid;}
bool examCompare(examine lhs, examine rhs) {return lhs.vid < rhs.vid;}
void vetExamSeach(struct vet newVetArray[], struct examine newExamArray[],
int, int);
int main()
{
dogs * newDogArray = new dogs[MAX];
examine * newExamArray = new examine[MAX];
vet * newVetArray = new vet[MAX];
ifstream DF, EF, VF;
int dogCount = 0, examCount = 0, vetCount = 0;
DF.open(DFile);
readDF(DF);
EF.open(EFile);
readEF(EF);
VF.open(VFile);
readVF(VF);
DF.open(DFile);
dogCount = getLineCount(DF);
EF.open(EFile);
examCount = getLineCount(EF);
VF.open(VFile);
vetCount = getLineCount(VF);
for(int i = 0; i < dogCount; i++)
newDogArray[i] = DFBuffer[i];
for(int i = 0; i < vetCount; i++)
newVetArray[i] = VFBuffer[i];
for(int i = 0; i < examCount; i++)
newExamArray[i] = EFBuffer[i];
cout << "Sorting...\n";
sort(newDogArray, newDogArray + dogCount, dogCompare);
sort(newExamArray, newExamArray + examCount, examCompare);
sort(newVetArray, newVetArray + vetCount, vetCompare);
cout << "Sorting complete!\n";
vetExamSeach(newVetArray, newExamArray, vetCount, examCount);
return 0;
}
here is the search function. for the sake of this question, im just trying to print what i pass it.
void search(vet newVetArray[], examine newExamArray[], int vCount, int eCount)
{
for(int i = 1; i < vCount; i++)
cout << "in search: " << newVetArray[i].vid << ' ' << newVetArray[i].eLevel << endl;
}
here is the error I'm getting
Here is my files. Not asking you to do my HW just help me solve my issue
When, I run your code, I get the same compilation error of undefined reference for readDf, readEF, readVF, getLineCount and vetExamSeach.
The error is because there is no definition of these functions. There are only just decalarations. When I define them (something random) the errors are gone.
So, define the function(s) and the error(s) would be gone.
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.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
Hi guyys i need your help!
When i compile run time error is :
/tmp/ccSOgpjn.o: In function Collection::evaluate()':
fitnessTest.cpp:(.text._ZN10Collection8evaluateEv[_ZN10Collection8evaluateEv]+0x45): undefined reference tofitnessFunction::doEvaluation(std::__cxx11::basic_string, std::allocator >)'
/tmp/ccSOgpjn.o: In function Collection::writeIndividual(char**, int)':
fitnessTest.cpp:(.text._ZN10Collection15writeIndividualEPPci[_ZN10Collection15writeIndividualEPPci]+0x3a): undefined reference toreadIndividual::read[abi:cxx11](char**, int)'
collect2: error: ld returned 1 exit status
I can not understand why?
my code is :
class fitnessFunction
{
public:
virtual int doEvaluation(string x);
};
class OneMax: public fitnessFunction
{
public:
virtual int doEvaluation(string x) {
int count = 0;
for (int i = 0; i < x.size(); i++)
if (x[i] == '1') count = count + 1;
return count;
}
};
class readIndividual
{
public:
string read(char * argv[], int i);
};
class OneMaxIndividual: public readIndividual
{
public:
virtual string read(char * argv[], int i) {
string inputFile = argv[i+1];
ifstream input(inputFile.c_str());
string x;
input >> x;
input.close();
return x;
}
};
class Collection
{
public:
fitnessFunction* m_function;
readIndividual* m_individual;
string individual;
public:
Collection(){}
void set_function(fitnessFunction* s){
m_function = s;
}
void set_individual(readIndividual* s){
m_individual = s;
}
int evaluate() {
m_function->doEvaluation(individual);
}
void writeIndividual(char* argv[], int i) {
individual = m_individual->read(argv,i);
}
};
int main(int argc, char* argv[])
{
int result = 0;
string outputFile = "fitness.out";
ofstream output( outputFile.c_str() );
OneMax fitnessFunction;
OneMaxIndividual individualObj;
Collection collection;
collection.set_function(&fitnessFunction);
collection.set_individual(&individualObj);
for(int i = 0; i < argc/2; i++){
//lettura individo
collection.writeIndividual(argv,i);
result = collection.evaluate();
output << result << endl;
}
for(int i = argc/2; i < argc-1; i++){
collection.writeIndividual(argv,i);
result = collection.evaluate();
output << result << endl;
}
output.close();
return 0;
}
I tried a few more questions like mine but have not found answers that I have solved the problem. The above code in all in a one page.
your doEvaluation(string ) function in fitnessFunction class has no implementation.
you have two options:
- make it pure virtual by appending =0 at the end or
- create dummy implementation
I sugest to make it pure virtual so that you are forced by the compiler to create implementation in derivated classes.
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
}