The error verbatim reads
1>yes.obj : error LNK2019: unresolved external symbol "int __cdecl availableMoves(int * const,int (* const)[4],int)" (?availableMoves##YAHQAHQAY03HH#Z) referenced in function "void __cdecl solveGame(int * const,int (* const)[4])" (?solveGame##YAXQAHQAY03H#Z)
I've never seen this error before. Here are the two functions I believe it's referring to though.
int availableMoves(int a[15], int b[36][3],int openSpace){
int count=0;
for(int i=0; i<36;i++){
if(i < 36 && b[i][2] == openSpace && isPeg(b[i][0],a) && isPeg(b[i][1],a) ){
count++;
}
}
return count;
}
and
void solveGame(int a[15], int b[36][4]) {
int empSpace;
int movesLeft;
if(pegCount(a) < 2) {
cout<<"game over"<<endl;
} else {
empSpace = findEmpty(a);
if(movesLeft = availableMoves(a,b,empSpace) < 1 ) {
temp[index] = empSpace;
d--;
c[d][0] = 0;
c[d][1] = 0;
c[d][2] = 0;
c[d][3] = 0;
a[b[c[d][3]][0]] = 1;
a[b[c[d][3]][0]] = 1;
a[b[c[d][3]][0]] = 0;
b[c[d][3]][3] = 0;
index++;
} else if(movesLeft >= 1) {
chooseMove( a, b, empSpace);
index = 0;
for(int i=0; i<4; i++) {
temp[i] = -1;
}
}
d++;
solveGame( a, b);
}
}
Your current declaration doesn't match the definition.
You probably have declared the function availableMoves() before you use it, but then you implement a different function:
int availableMoves(int* const a, int (* const)[4] , int);
//....
//....
//....
//code that uses available moves
int availableMoves(int a[15], int b[36][3],int openSpace)
{
//....
}
Since the compiler sees that declaration first, it will use it to resolve the call in the block of code. However, that function is not exported, as it has a different signature.
in solved game
b[36][4]
in available moves
b[36][3]
that could create a problem.
Nice one: you use incompatible array dimensions! Note that part of the error message reads
availableMoves(int *const,int (*const)[4],int)
While the definition of availableMoves() looks like this:
int availableMoves(int a[15], int b[36][3],int openSpace)
Although the first dimension of the arguments is ignored, all other dimensions have to match exactly. You try to call this function with incompatible dimensions, however:
void solveGame(int a[15], int b[36][4]){
...
... availableMoves(a,b,empSpace) ...
Related
In the code that I have attached here, I have written the function called "show2D" and then I want to run this function inline 21 but it is showing an error there saying "no matching function for call to 'show2D'"
#include <iostream>
using namespace std;
void show2D(int variable[][20]){
int answer[4000];
int n=0;
for (int i=0; i<20;i++){
for(int j=0;j<20;j++){
if (variable[i][j]%2==1){
answer[n]=variable[i][j];
n++;
}
}
}
}
int main() {
int trailarr[2][2];
trailarr[0][0] = 0;
trailarr[0][1] = 1;
trailarr[1][0] = 2;
trailarr[1][1] = 3;
show2D(trailarr);
return 0;
}
Because trailarr is a [2][2] array and show2D expects an array of size variable[][20]. So the linker does not find any function called show2D that can accept a [2][2] array.
I want to pass a pointer to a function which will call a second function that will use realloc.
The issue is that realloc is returning NULL.
I don't know if the mistake is in the numbers of * in the function call or something else.
Could you please help me ?
The code:
int main(){
// some code.
clause_t* ptr; //clause_t is a structure i declared.
//Some work including the initial allocation of ptr (which is working).
assignLonely(matSAT, ic.nbClause, ic.nbVar, ptr); //the issue is here.
//Some other work
}
void assignLonely(int** matSAT, int nbClause, int nbVar, clause_t* ptr)
{
int i = 0, j = 0;
int cpt = 0;
int indice = -1;
for (i = 0; i < nbClause ; ++i)
{
j = 0;
cpt = 0;
while((j < nbVar) && (cpt < 2))
{
if (matSAT[i][j] != 0)
{
cpt++;
}
else
{
indice = j;
}
if (cpt < 2)
{
deleteClause(indice, &ptr);
}
j++;
}
}
}
void deleteClause(int indiceClause, clause_t** ptr)
{
int i = indiceClause;
int nbElt = sizeof((*ptr))/sizeof((*ptr)[0]);
int tailleElt = sizeof((*ptr)[0]);
while(i+1 < nbElt)
{
(*ptr)[i] = (*ptr)[i+1];
i++;
}
*ptr = (clause_t*)realloc(*ptr, (nbElt-1)*tailleElt);
if (*ptr == NULL)
{
fprintf(stderr, "Erreur reallocation\n");
exit(EXIT_FAILURE);
}
}
You have to declarae function assignLonely similarly to function deleteClause like
void assignLonely(int** matSAT, int nbClause, int nbVar, clause_t** ptr);
if you want that changes of ptr in the function would be stored in the original object in main.
Also take into account that this statement
int nbElt = sizeof((*ptr))/sizeof((*ptr)[0]);
is wrong.
Expression sizeof((*ptr)) will return the size of the pointer. Pointers do not keep information about how many elements in arrays they point to.
So expression
(nbElt-1)
can be equal to zero or even be negative.
So here is my header file:
#pragma once
#ifndef HYPERINT_H
#define HYPERINT_H
#include <iostream>
#include <vector>
class Hyperint
{
public:
Hyperint();
Hyperint(long a);
~Hyperint(void);
Hyperint & operator*= (const Hyperint & right);
std::vector<int> hyperintVector;
};
Hyperint operator* (const Hyperint & left, const Hyperint &right);
#endif
here is my cpp file:
#include "stdafx.h"
#include "Hyperint.h"
using namespace std;
Hyperint::Hyperint(long a)
{
//vector<int> hyperint;
int b = a;
while (b != 0){
int h = b % 10;
this->hyperintVector.push_back(h);
b = b / 10;
}
}
Hyperint::~Hyperint()
{
}
Hyperint operator*(const Hyperint & left, const Hyperint & right){
vector<int> leftVec = left.hyperintVector;
vector<int> rightVec = right.hyperintVector;
vector<int> resultVector;
Hyperint result;
int carry = 0;
int counter1 = 0;
for (vector<int>::const_iterator it = leftVec.begin(); it != leftVec.end(); ++it){
int counter2 = 0;
int totalOperand = 0;
for (vector<int>::const_iterator it2 = rightVec.begin(); it2 != rightVec.end(); ++it2){
double pow2 = pow(10, counter2);
totalOperand += ((*it2) * ((int) pow2)) * (*it);
++counter2;
}
totalOperand += carry;
int store = totalOperand % 10;
resultVector.push_back(store);
carry = totalOperand / 10;
++counter1;
}
while (carry != 0){
int putIn = carry % 10;
resultVector.push_back(putIn);
carry /= 10;
}
result.hyperintVector = resultVector;
return result;
}
Hyperint & Hyperint::operator*=(const Hyperint & right){
vector<int> rightVec = right.hyperintVector;
//vector<int> leftVec = this->hyperintVector;
vector<int> resultVector;
Hyperint theResult;
int carry = 0;
int counter1 = 0;
for (vector<int>::const_iterator it = (this->hyperintVector).begin(); it != (this->hyperintVector).end(); ++it){
int counter2 = 0;
int totalOperand = 0;
for (vector<int>::const_iterator it2 = rightVec.begin(); it2 != rightVec.end(); ++it2){
double pow2 = pow(10, counter2);
totalOperand += ((*it2) *((int)pow2)) * (*it);
++counter2;
}
totalOperand += carry;
int store = totalOperand % 10;
resultVector.push_back(store);
carry = totalOperand / 10;
++counter1;
}
while (carry != 0){
int putIn = carry % 10;
resultVector.push_back(putIn);
carry = carry/10;
}
(this->hyperintVector) = resultVector;
return *this;
}
Now the problem arises when I compile it... I get 1 error and I don't know what it is, what it means, or why and how to fix it.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Hyperint::Hyperint(void)" (??0Hyperint##QAE#XZ) referenced in function "public: class Hyperint & __thiscall Hyperint::operator*=(class Hyperint const &)" (??XHyperint##QAEAAV0#ABV0##Z) C:\Users\Drock\documents\visual studio 2013\Projects\A3-Attempt\A3-Attempt\Hyperint.obj A3-Attempt
It means that the linker is trying to look for a definition of Hyperint::Hyperint() but couldn't find it. You need to provide an implementation of it.
Linker errors can be confusing and a lot more so than compiler errors as the names get garbled and you often lose the exact location in your code that generated that error. Let's go through your error message as it contains all the information you need, just presented poorly. I'll bold the important parts.
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Hyperint::Hyperint(void)" (??0Hyperint##QAE#XZ) referenced in function "public: class Hyperint & __thiscall Hyperint::operator*=(class Hyperint const &)" (??XHyperint##QAEAAV0#ABV0##Z) C:\Users\Drock\documents\visual studio 2013\Projects\A3-Attempt\A3-Attempt\Hyperint.obj A3-Attempt
In human terms, Visual Studio is complaining that it's linker encountered an error called LNK2019, which was due to not being able to find the symbol Hyperint::Hyperint(void), whilst it was going through the function Hyperint::operator*=(class Hyperint const &).
First port of call is the error number. This is easily found in a search engine, which gives the following page on MSDN: http://msdn.microsoft.com/en-us/library/799kze2z.aspx
That page describes what the error is, and gives a few examples of what kind of code generates it. This subpage describes the problem that's closer to yours: http://msdn.microsoft.com/en-us/library/x3k07566.aspx
More specifically, it couldn't find an implementation of Hyperint::Hyperint(). In C++ just declaring it (e.g. in a header as Hyperint();) is not enough, you need an implementation (the code in curly braces {}) somewhere, usually in the corresponding cpp file.
Finally, it's saying that it encountered this error whilst processing the Hyperint::operator*=(class Hyperint const &) function. This information is not actually useful in tracking down this error, but it's probably caused by this line:
Hyperint result;
Which creates a Hyperint object and initialises using the no-argument constructor, i.e. Hyperint::Hyperint().
So putting this all together, you have declared and used the no-argument constructor Hyperint::Hyperint() in your header:
class Hyperint
{
public:
Hyperint(); // < this line here
Hyperint(long a);
~Hyperint(void);
// ...
};
But you have not implemented it in your cpp file. You'd probably need something like this:
Hyperint::Hyperint()
{
// some code goes here, if required
}
You have declared a constructor Hyperint::Hyperint() that takes no arguments, but you have not provided a definition. In the .cpp file you need to provide one. You could also use a default parameter for the Hyperint::Hyperint(long) constructor (in the header file) instead if that works for your design.
I'm trying to include a simple hash table class in some files with a header class. But whenever I try to compile I get several errors like this:
LNK2019: unresolved external symbol " public: __thiscall HashTable::~HashTable(void)" (??1HashTable##QAE#XZ) referenced in function _main "
I'm using Visual Studio 2010. I am aware that this means it can't find the function definition in any of the source files. But I have defined them, in a file in the same directory as the file it's called in. Perhaps Visual Studio doesn't look in the current directory unless you set some linker option?
Here is the source code:
//HashTable.h
#ifndef HASH_H
#define HASH_H
class HashTable {
public:
HashTable();
~HashTable();
void AddPair(char* address, int value);
//Self explanatory
int GetValue(char* address);
//Also self-explanatory. If the value doesn't exist it throws "No such address"
};
#endif
//HashTable.cpp
class HashTable {
protected:
int HighValue;
char** AddressTable;
int* Table;
public:
HashTable(){
HighValue = 0;
}
~HashTable(){
delete AddressTable;
delete Table;
}
void AddPair(char* address, int value){
AddressTable[HighValue] = address;
Table[HighValue] = value;
HighValue += 1;
}
int GetValue(char* address){
for (int i = 0; i<HighValue; i++){
if (AddressTable[HighValue] == address) {
return Table[HighValue];
}
}
//If the value doesn't exist throw an exception to the calling program
throw 1;
};
};
No you have not. You created a new class.
The proper way to define the methods is:
//HashTable.cpp
#include "HashTable.h"
HashTable::HashTable(){
HighValue = 0;
}
HashTable::~HashTable(){
delete AddressTable;
delete Table;
}
void HashTable::AddPair(char* address, int value){
AddressTable[HighValue] = address;
Table[HighValue] = value;
HighValue += 1;
}
int HashTable::GetValue(char* address){
for (int i = 0; i<HighValue; i++){
if (AddressTable[HighValue] == address) {
return Table[HighValue];
}
}
//If the value doesn't exist throw an exception to the calling program
throw 1;
};
Please tell me if some more info is needed here:
Global declarations:
typedef struct route
{
int exitPoint;
bool allBranchesTraversed;
} route;
****vector <route> routeVector;****
The culprit func is getting called from:
int main ()
{
....
do
{
****currentExitPoint = returnShortestWeightedBranch (&routeVector);****
if (currentExitPoint != -1)
{
objRoute.exitPoint = currentExitPoint;
routeVector.push_back (objRoute);
}
else
{
break;
}
} while (1);
}
The error is in this func on the line with **:
int returnShortestWeightedBranch (vector <route> *objRouteVector)
{
....
for (unsigned int h = 0; h < objRouteVector->size (); h++)
{
// Locate 'currentExitPoint' in the vector 'exitPointDetailsVector'.
for (i = 0; i < exitPointDetailsVector.size(); i++)
{
// If located
****if (objRouteVector[h].exitPoint == exitPointDetailsVector[i].exitPoint)****
{
// For all the branches of the 'currentExitPoint',
for (j = 0; j < exitPointDetailsVector[i].branchesVector.size(); j++)
{
...............
}
If you use vector <route> *objRouteVector as parameter, you need (*objRouteVector)[h].exitPoint. Better is using reference: vector <route> &objRouteVector.
You took a pointer to objRouteVector, you need to take a reference. Your code indexing objRouteVector isn't indexing the vector at all- it's indexing the pointer.