Following is my code...
#include"PointerToArray.h"
#include <iostream>
using namespace std;
void display(const int (*displayM)[10],int resultRow,int resultColumn)
{
for(int i=0;i<resultRow;i++)
{
for(int j=0;j<resultColumn;j++)
{
cout<<displayM[i][j];
}
}
}
void read()
{
int (*matrixA)[10],(*matrixB)[10];
int row1,col1,row2,col2;
cout<<"Enter the number of rows and colums for Matrix1";
cin>>row1>>col1;
matrixA=new int[row1][10];
cout<<"Enter elements"<<endl;
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
cin>>matrixA[i][j];
}
}
cout<<"Enter the number of rows and colums for Matrix2";
cin>>row2>>col2;
matrixB=new int[row2][10];
cout<<"Enter elements"<<endl;
for(int i=0;i<row2;i++)
{
for(int j=0;j<col2;j++)
{
cin>>matrixB[i][j];
}
}
display(matrixA,row1,col1);
}
I am getting error as
1:1>C:\Progs\PointerToArray\Debug\PointerToArray.exe : fatal error
LNK1120: 1 unresolved externals 2:1>PointerToArray.obj : error
LNK2019: unresolved external symbol "void __cdecl display(int
(*)[10],int,int)" (?display##YAXPAY09HHH#Z) referenced in function
"void __cdecl read(void)" (?read##YAXXZ)
Can any one suggest the way?
The display function signature in your .c file is different from your error message in that there is no 'const' there. Can you check that in PointerToArray.h the function declaration (prototype) does not have
void display(int (*displayM)[10],int resultRow,int resultColumn)
but
void display(const int (*displayM)[10],int resultRow,int resultColumn)
instead?
If it does you can resolve it by changing either one to match.
Related
#include <iostream>
using namespace std;
void PrintN(int i, int N)
{
for (i = 1; i <= N; i++)
printf("%d\n", i);
return;
}
when I compiled it, it worked well, but it gave two errors in line 1 when I built it. One is
"Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)" The other is "unresolved externals". But after I add "int main()", it can work well,but not export the integers from 1 to N on the screen. I think the void function can work independently even without main function. It really made me confused.
You need to define main, without it the OS has no way to execute your application. Furthermore, who would execute PrintN?
#include <iostream>
void PrintN( int i, int n )
{
for (; i < n; i++ )
std::cout << i << '\n';
}
int main( )
{
PrintN( 10, 20 );
}
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 start learning C++ in school and this error appear.
1>Bettle_Dice.obj : error LNK2019: unresolved external symbol "public: int __thiscall Beetle::checkcom(void)" (?checkcom#Beetle##QAEHXZ) referenced in function _main
I have include other header files and cpp files, I don understand why only this file have problem please help
Below is my code
main.cpp
#include "stdafx.h"
#include "beetle.h"
#include "dice.h"
#include "player.h"
#include <iostream>
using namespace std;
int main()
{
Player p; //declare Class to a variable
Dice d;
Beetle btle;
int temp;
cout << "Number of players?" << endl;
cin >> temp;
p.Num(temp); //store the number of player into class
//cout << p.getNumPlayers() <<endl;
cout << "Start game!!" <<endl;
temp = btle.checkcom();
while(temp != 1)
{
for(int i=0;i<p.getNumPlayers();i++)
{
temp = d.roll();
cout <<"Your roll number:"<< temp;
}
}
return 0;
}
beetle.h
class Beetle
{
private:
int body,head,ante,leg,eye,tail;
public:
int completion();
int checkcom();
int getBody() { return body;};
int getHead() { return head;};
int getAnte() { return ante;};
int getLeg() { return leg;};
int getEye() { return eye;};
int getTail() { return tail;};
};
Beetle.cpp
#include "stdafx.h"
#include "beetle.h"
int completion()
{
return 0;
}
int checkcom()
{
Beetle btle;
int flag = 0;
if(btle.getBody() == 1 && btle.getHead() == 1 && btle.getAnte() == 2 && btle.getEye() == 2 && btle.getLeg() ==6 && btle.getTail() == 1)
flag = 1;
return flag;
}
I checked some solution on the internet, some are saying is the library problem, but this file is not a built-in function. I guess it is not the problem of the library. I tried to include the beetle.obj file to it and the debugger said it is included already and duplicate definition.
In the other file, i do not have "bettle" this word. It should not be the problem of double declaration or duplicate class.
I have no idea what the problem is. Please help.
You need to prefix the signature of your class functions with the class name Beetle::
Otherwise the compiler just thinks those functions are global functions, not member functions.
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 create this file over and over and cant seem to see why I'm getting this error. I tried going to the line where the code is but the format seem correct I may just need another set of eyes .
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void readString(char*, int);
void changeToUppercase(char*, int);
void displayStringInUppercase(char*, int);
int main()
{
int arraySize;
char* characterArray;
cout << "Enter the size of dynamic array: ";
cin >> arraySize;
characterArray = new char[arraySize];
readString(characterArray, arraySize);
changeToUppercase(characterArray, arraySize);
displayStringInUppercase(characterArray, arraySize);
delete [] characterArray;
system ("pause");
return 0;
}
void changeToUppercase(char* characterArray, int arraySize)
{
for(int i = 0; i < arraySize; i++)
characterArray[i] = toupper(characterArray[i]);
}
void displayStringInUppercase(char* characterArray, int arraySize)
{
cout << "\nThestring inupper case letters: ";
for(int i = 0; i < arraySize; i++)
characterArray[i] = toupper(characterArray[i]);
}
This is the error codes that keep popping up:
error LNK2019: unresolved external symbol "void __cdecl readString(char *,int)" (?readString##YAXPADH#Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals
You use a forward declaration: void readString(char*, int); but then never actually define this function.
Define your readString function later in your code like...
void readString(char* str, int a)
{
// do stuff
}
You are missing the readString function. You have a forward declaration that satisfies the compiler here
void readString(char*, int);
But no actual implementation of the function to satisfy the linker when it tries to put your program together. You need something along the lines of
void readString(char* characterArray, int arraySize)
{
// do stuff here
}
I'm new to c++ and was just trying to do a question that involves 3 files.
I got a header file with a namespace that declares a structure and some prototypes. In definitions.cpp file, I have all the function definitions. Then the main.cpp file, just creates a few structures, calls some functions to fill the structures, then calls another function to display the structures.
The problem I have is it gives me an unresolved external error. From what I can gather I think the compiler is complaining that it cant find the prototypes for the functions being called in main.cpp - the overloaded setStruct() and the showStruct().
But I thought when I included the header.h file and declared the functions with the using declaration, in main.cpp, it gave the compiler access to the prototypes stored in the header file?
Header.h
#ifndef header_h
#define header_h
namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales & s, const double ar[], int n);
void setSales(Sales & s);
void showSales(const Sales & s);
}
#endif
Definitions.cpp
#include<iostream>
#include"header.h"
using SALES::Sales;
using SALES::QUARTERS;
double max(Sales & s) //find max sale value in sales array
{
double maxVal = s.sales[0];
for(int i = 1; i<4;i++)
{
if(s.sales[i]>maxVal)
{
maxVal = s.sales[i];
}
}
return maxVal;
}
double min(Sales & s) //find min sale value in sales array
{
double minVal = s.sales[0];
for(int i = 1; i<4;i++)
{
if(s.sales[i]<minVal)
{
minVal = s.sales[i];
}
}
return minVal;
}
void setSales(Sales & s) // fill sales structure interactivly
{
std::cout<< "Please enter the sales for the yearly quarters.\n";
for(int i = 0;i<QUARTERS;i++)
{
std::cout<< "Quater "<<i+1<<": ";
while(!(std::cin>>s.sales[i]))
{
std::cout<<"Please enter valid input\n";
std::cout<< "Quater "<<i+1<<": ";
std::cin.clear();
std::cin.ignore();
}
}
s.average = ((s.sales[0]+s.sales[1]+s.sales[2]+s.sales[3])/4);
s.max = max(s);
s.min = min(s);
}
void setSales(Sales & s, const double ar[], int n) // fill sales structure non interactivly
{
for(int i = 0;i<n;i++)
{
s.sales[i] = ar[i];
}
for(int i = n;i<QUARTERS;i++)
{
s.sales[i] = 0;
}
s.average = ((s.sales[0]+s.sales[1]+s.sales[2]+s.sales[3])/4);
s.max = max(s);
s.min = min(s);
}
void showSales(const Sales & s) // display structure
{
std::cout<< "\nSales for the year\n";
for(int i = 0;i<QUARTERS;i++)
{
std::cout<<"Quarter "<<i+1<<": $"<<s.sales[i];
}
std::cout<<"Max Sale: "<<s.max<<std::endl;
std::cout<<"Min Sale: "<<s.min<<std::endl;
std::cout<<"Average of sales: "<<s.average<<std::endl;
}
Main.cpp
#include<iostream>
#include"header.h"
using SALES::Sales;
using SALES::setSales;
using SALES::showSales;
int main()
{
double Sales1[4] = {453.50, 654.60, 340.20, 500.30};
Sales Year1;
Sales Year2;
setSales(Year1, Sales1, 3);
setSales(Year2);
showSales(Year1);
showSales(Year2);
return 0;
}
Error
1>------ Build started: Project: Myfirst, Configuration: Debug Win32 ------
1> Main.cpp
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl SALES::setSales(struct SALES::Sales &,double const * const,int)" (?setSales#SALES##YAXAAUSales#1#QBNH#Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl SALES::setSales(struct SALES::Sales &)" (?setSales#SALES##YAXAAUSales#1##Z) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "void __cdecl SALES::showSales(struct SALES::Sales const &)" (?showSales#SALES##YAXABUSales#1##Z) referenced in function _main
1>E:\Documents\Uni\Programming\C++ starter projects\Myfirst\Debug\Myfirst.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any help would be great, thanks.
When you define the setSales etc functions, you need to tell the compiler that they are in the SALES namespace. Putting
namespace SALES
{
// functions here
}
will do it.
You are currently defining your functions outside of any namespace, and that is the reason for the linker not finding the definitions within the namespace where those functions are declared and recognized.
The fix would be to wrap the functions in your implementation file - Definitions.cpp -, with the namespace similarly how you are doing it in the header file.
This is what I would write if I were you:
namespace SALES
{
...
void setSales(Sales & s) // fill sales structure interactivly
...
std::cout<<"Average of sales: "<<s.average<<std::endl;
}
}
If you do not put the rest (your min and max) into the namespace, use the :: scope specifier just in case, although I would suggest to put everything into your own namespace.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void countx(double, int[]);
void initializeArray(int[]);
void printResult(double,double,int,int[]);
void main()
{
int i, count,distF[8];
double x,sum1,sum2;
/* Calculation for X~ (x,0,1) */
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
for(i=1;i<10000;i++)
{
x=(double)(rand())/RAND_MAX;
if(x>.9 && x<= 1.8)
count++;
countx(x,distF);
sum1 += x;
sum2 += x*x;
}
cout<<"Results for uniform Distribution:\n";
printResult(sum1,sum2,count,distF);
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
for(i=1;i<=10000;i++)
{
x= -(log(1-(double) (rand())/RAND_MAX))/2;
if(x>.9 && x<=1.8)
countx(x,distF);
sum1+=x;
sum2+=x*x;
}
cout<<"Reuslts for exponential Distribution 2:\n";
printResult(sum1,sum2,count,distF);
/*** Calculations for P(x=1/2)=1 ***/
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
for(i=1;i<=1000;i++)
{
x=.5;
count++;
countx(x,distF);
sum1+=x;
sum2+=x*x;
}
cout<<"Reuslts for exponential Distribution 3:\n";
printResult(sum1,sum2,count,distF);
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
double temp;
for(i=1;i<=10000;i++)
{
temp=(double)(rand())/RAND_MAX;
if(temp<=.5)
x=.25;
else
x=1.50;
if(x>.9 && x<=1.8)
count++;
countx(x,distF);
sum1 += x;
sum2 +=x*x;
}
cout<<"Reuslts for Distribution #4:\n";
printResult(sum1,sum2,count,distF);
/*Function*/
void countx(double x, int distF[]);
{
int j=0;
double t= -0.25;
do
{
if(x<=t)
{
for(int k=j;k<8;k++)
distF[k]++;
break;
}
t += .25;
j++;
}
while(t<=1.50);
//return;
}
void intializeArray(int distF[]);
{
for(int j=0;j<8;j++)
distF[j]=0;
//return;
}
void printResult(double sum1, double sum2, int count, int distF[]);
{
double ev_x,var_x,pr_x;
ev_x=sum1/10000;
var_x=sum2/10000-(ev_x * ev_x);
pr_x=(double)(count)/10000;
cout<<"Expected value: " <<ev_x<<endl;
cout<<"Variance: "<<var_x<<endl;
cout<<"P(0.9<x<=1.8):" <<pr_x<<endl;
cout<<"t=";
for(int k=-1; k<7;k++)
cout<<.25 *k<<"\t";
cout<<"lnFx(t)=";
for(int j=0;j<8;j++)
cout<<(double)(distF[j])/10000<<"\t";
cout<<endl;
//return;
}
}
Need help! Using Microsoft Visual Studio 2012
>kikii.obj : error LNK2019: unresolved external symbol "void __cdecl printResult(double,double,int,int * const)" (?printResult##YAXNNHQAH#Z) referenced in function _main
1>kikii.obj : error LNK2019: unresolved external symbol "void __cdecl countx(double,int * const)" (?countx##YAXNQAH#Z) referenced in function _main
1>kikii.obj : error LNK2019: unresolved external symbol "void __cdecl initializeArray(int * const)" (?initializeArray##YAXQAH#Z) referenced in function _main
You have two overall problems:
1) your functions are 'definedinside ofmain()` - you need to move them outside (functions can't be nested)
2) your function definitions have a semi-colon in an inappropriate place, turning them into merely prototypes followed by an unrelated block of code (which is why problem #1 isn't a syntax error right off the bat). For example:
void intializeArray(int distF[]); // <-- this semi-colon needs to be removed
{
for(int j=0;j<8;j++)
distF[j]=0;
//return;
}
Oh, a third problem I didn't notice at first: you've misspelled initializeArray() as intializeArray() in your attempt to define the function.