undefined reference to function in main() - c++

i tried to make a program on seperate files. Unfortunatelty i had erros while trying to build the code. It was pointing on undefined references to constuctors,destructos and function CzynnikiPierwsze. So i decied to put the whole code in one code. Still there is a problem in main() function: undefined reference to 'CzynnikiPierwsze(int)' Any ideas whats wrong? Here is the whole code:
#include <iostream>
#include <cctype>
#include <vector>
using namespace std;
vector<int> CzynnikiPierwsze(int);
class NieprawidlowaDana //wyjatki
{};
class SpozaZakresu
{};
class RozkladLiczby{
private:
int *tab;
public:
RozkladLiczby(int); //konstruktor
vector<int> CzynnikiPierwsze(int); //metoda
~RozkladLiczby();
};
/////////////////BODY of the CLASS/////////////////////////////////////
RozkladLiczby::~RozkladLiczby() //destruktor
{}
RozkladLiczby::RozkladLiczby(int n){
int* tab = new int[n+1];
int i,j;
for( i=0;i<=n;i++)
tab[i]=0; //zerujemy tablice
for( i=2;i<=n;i+=2)
tab[i]=2; //zajmujemy sie liczbami parzystymi
for(i=3; i<=n;i+=2)
for(j=i;j<=n;j+=i) //sito erastotesa
if(tab[j]==0)
tab[j]=i;
}
vector<int> RozkladLiczby::CzynnikiPierwsze(int m){
vector<int> tablica;
while(m!=1){
tablica.push_back(tab[m]);
m=m/tab[m];
}
return tablica;
}
////////////////////////END OF THE BODY//////////////////////////////
int parsuj(char* argz){
int i=0;
while(argz[i] != '\0'){ //funckja ktora konwertuje na int i sprawdza czy wprowadzaony zostal string
if( !isdigit(argz[i]))
throw NieprawidlowaDana();
i=i+1;
}
int x = stoi(argz);
if (x >= 2)
return x;
else
throw SpozaZakresu();
}
//////////////////GLOWNY BLOK///////////////////////////////////////
int main(int argc,char* argv[]){
vector<int> tablica,p;
int i,n;
int max;
for( i=1;i<=argc-1;i++){
n = parsuj(argv[i]);
tablica.push_back(n);
}
max=tablica[0];
for(i=1; i<=argc-1;i++){
if(tablica[i]>max)
max=tablica[i]; } // sprawdzamy max
RozkladLiczby odp = RozkladLiczby(max); //utwoorzylismy obiekt z maxa
for(unsigned int i=0;i<=tablica.size()-1;i++){
p=CzynnikiPierwsze(tablica[i]);
cout<<tablica[i]<<" = ";
int x= p[0];
int licznik = 1;
for(unsigned int j=1;j<=p.size()-1;j++){
if(x==p[j])
licznik++;
else if(licznik!=1)
cout<<x<<"^"<<licznik<<"*";
else
cout<<x<<"*";
}
cout<<endl;
}
return 0;
}
I would be grateful if u could solve this.

You have declared global function vector<int> CzynnikiPierwsze(int); but you have not defined it anywhere in your program. In your main you are calling global function and not the one which is your class member.

Related

Unable to pass a function pointer to a member function of the class as an argument to another member function of the same class

I need help... appropriate questions have been asked in the comments. The programs has zero compiler errors and warnings!! I have concerns with calling a member function from another member function using function pointers. (To be precise, setMatrixto() is trying to call setElement() function using function pointer)
Plus somehow the "hello there" is not being printed to the console. I was expecting it to show up as output.Maybe the setMatrixto() is not getting called at all!!
Header File definition
#ifndef MATRIXOPERATIONS_H
#define MATRIXOPERATIONS_H
class MatrixOperations;
typedef int (MatrixOperations::*INTFUNC)(int,int);
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void displayMatrixOf(INTFUNC f);
void setMatrixTo(VOIDFUNC f);
int getElement(INTFUNC from, int i, int j);
void setElement(VOIDFUNC to,int i ,int j, int value);
int fromDiagonalArray(int i, int j);
void toDiagonalArray(int i, int j, int value);
protected:
private:
int size;
int* a;
};
#endif // MATRIXOPERATIONS_H
CPP Implementation File
#include "MatrixOperations.h"
#include <iostream>
using namespace std;
MatrixOperations::MatrixOperations()
{
//ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{
//ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
///////////////////FUCNTION POINTER SECTION///////////////////////////////////
int MatrixOperations::getElement(INTFUNC from, int i, int j)
{
return (this->*from)(i,j);
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
int MatrixOperations::fromDiagonalArray(int i, int j)
{
if(i==j)
{
return a[i];
}
else
{
return 0;
}
}
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::displayMatrixOf(INTFUNC f)
{
for(int i{0}; i < size; i++)
{
for(int j{0}; j < size; j++)
{
cout << getElement(f,i,j) << "\t"; //is this the correct way to send the function pointer?
}
cout << endl;
}
}
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!"; //not getting this output.. whats wrong??
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value); //is this the correct way to send the function pointer?
}
}
///////////////////////////////////////////////////////////////////////////////
Main File
#include <iostream>
#include "MatrixOperations.h"
typedef MatrixOperations MATRIX;
using namespace std;
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray); //was expecting a "Hello there!" but i am not getting that output either
return 0;
}
EDIT2: I added all the class definitions and main function in one single file. SURPRISINGLY!! this works . I am confused??!!!
#include <iostream>
using namespace std;
class MatrixOperations;
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
typedef MatrixOperations MATRIX;
class MatrixOperations
{
public:
MatrixOperations();
MatrixOperations(int size);
~MatrixOperations();
//diagonal matrix funtions
void setMatrixTo(VOIDFUNC f);
void setElement(VOIDFUNC to,int i ,int j, int value);
void toDiagonalArray(int i, int j, int value);
private:
int size;
int* a;
};
MatrixOperations::MatrixOperations()
{ //ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{ //ctor
this->size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this->*to)(i,j,value);
}
/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
a[i] = value;
}
///////////////////////////////////////////////////////////////////
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout << "Hello there!!" << endl;
for(int i{0}; i < size; i++)
{
int value {};
cout << "Enter value diagonal element " << i << " : ";
cin >> value;
setElement(f,i,i,value);
}
}
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray);
return 0;
}
There is nothing wrong with the code in both cases. Its just my debugger was not running in admin mode. I got error code 740. So I launched my IDE in admin mode and voila it worked.

c++: vector of class instances, search by class member values failed

I have the following program where I defined a vector of class Point. I pushed into this vector five Point instances, with their Ids. Then I tried to search by Id but didn't get the expected result. The following program didn't return anything.
#include<iostream>
#include<vector>
using namespace std;
class Point {
private:
int id;
public:
Point(){}
void setId(int k){ id=k; }
int GetId() { return id; }
};
int main()
{
vector<Point> datasets;
for(int i=0; i< 5; ++i){
Point temp;
temp.setId(i);
datasets.push_back(temp);
}
for(int i=0;i<5;i++){
if (datasets[i].GetId() ==4){
return i;
}
}
}
Your program is probably working just fine, I think you're mixing up return with cout to actually print it out to the console, currently you're not printing anything and you're just returning i to the OS because you use return in main, making it a status code.
To see the output, use cout:
#include<iostream>
#include<vector>
using namespace std;
class Point{
private:
int id;
public:
Point(){}
void setId(int k){id=k;}
int GetId(){return id;}
};
int main()
{
vector<Point> datasets;
for(int i=0; i< 5; ++i){
Point temp;
temp.setId(i);
datasets.push_back(temp);
}
for(int i=0;i<5;i++){
if (datasets[i].GetId() ==4){
cout << "i is : " << i << endl;
break;
}
}
}
Edit to answer OP's comment:
use : vector<Point> datasets(5);
you arent printing any thing because you put return i after for loop
its certain that you wont got any result
#include<iostream>
#include<vector>
using namespace std;
class Point{
private:
int id;
public:
Point(){}
void setId(int k){id=k;}
int GetId(){return id;}
};
int main()
{
vector<Point> datasets;
for(int i=0; i< 5; ++i){
Point temp;
temp.setId(i);
datasets.push_back(temp);
}
for(int i=0;i<5;i++){
if (datasets[i].GetId() ==4){
cout<<i<<endl;
}
}
system("pause");
return 0;
}
you can use : datasets.insert(datasets.begin()+i,temp);
instead of datasets.push_back(temp);
for more flexibility in adding elements at the index i in vector class

coding error or compiler's fault

I was working on this code for a project on school and when I wanted to try debugging my code just got segmentation fault before even running the first line in main() so i was wondering if i miss something on my code or is the compiler's fault.
#include <iostream>
using namespace std;
class poly
{
public: int a[1000000];
private:
int forx(int x);
public:
poly(){cout<<"add";}
~poly(){cout<<"kill";}
void add();
void sum(int *x,int *y);
void dif(int *x,int *y);
void mult(int *x,int *y);
void renew();
};
void poly::add()
{
int i,n;
cin>>n;
a[0]=n;
for (i=1; i<=n; i++)
{
cin>>a[i];
}
}
int poly::forx(int x)
{
int s,i,p;
p=1;
for (i=1; i<=a[0]; i++)
{
s+=p*a[i];
p*=x;
}
return s;
}
void poly::sum(int *x,int *y)
{
int i,m=x[0]>y[0]?x[0]:y[0];
a[0]=m;
for (i=1; i<=a[0]; i++)
{
a[i]=x[i]+y[i];
}
}
void poly::dif(int *x,int *y)
{
int i,m=x[0]>y[0]?x[0]:y[0];
a[0]=m;
for (i=1; i<=a[0]; i++)
{
a[i]=x[i]-y[i];
}
for (i=a[0]; i>0; i--)
{
if (a[i]!=0) break;
a[0]--;
}
}
void poly::mult(int *x,int *y)
{
int i,j,k;
for (i=1; i<=(x[0]+y[0]-2); i++)
{
j=0;
k=y[0]-1;
while (j+k!=i)
{
if (j+k>i) k--;
if (j+k<i) j++;
}
while (j<x[0] && k>=0)
{
a[i]+=x[j]*y[k];
k--;
j++;
}
}
}
void poly::renew () {
int i;
for (i=1; i<=a[0]; i++)
{
cout<<a[i];
}
}
int main()
{
cout<<"starting";
poly w;
w.add();
poly h;
h.add();
poly o;
o.sum(w.a,h.a);
o.renew();
o.dif(w.a,h.a);
o.renew();
o.mult(w.a,h.a);
o.renew();
}
Becase of int a[1000000];, size of poly class is very large. Making a (actually you are making 3) local variable(s) of this class (on stack) would give you segmentation fault.
You can try making them static or move them to global scope or alloc them dynamically.
...
static poly w;
w.add();
static poly h;
h.add();
static poly o;
...
Another solution is to replace arrays with std::vector
change public: int a[1000000]; to
...
public: std::vector<int> a;
...
poly() : a(1000000) {cout<<"add";}
...
Now you can create local objects of this class.
Another related question Segmentation fault on large array sizes

linker error in c++ and returns null value while geeting the new val function

I am getting the inconsistent error while i am trying to execute the following program.can anyone tell me where i am doing wrong...
int a,b=0;
int getvalue(int c)
{
int n=0;
a=c;
if(n<c)
n=a+b;
return n;
}
int newvalue(int c)
{
int n=0;
int a=c;
if(n<getvalue(c))
n=a+b;
return n;
}
voidmain()
{
int j=1;
int b=newvalue(j);
cout<<a+b+j<<end1;
return 0;
}
Try this (end1 should be endl) and I fixed the main signature.
#include <iostream>
int a,b=0;
int getvalue(int c)
{
int n=0;
a=c;
if(n<c)
n=a+b;
return n;
}
int newvalue(int c)
{
int n=0;
int a=c;
if(n<getvalue(c))
n=a+b;
return n;
}
int main()
{
int j=1;
int b=newvalue(j);
std::cout<<a+b+j<<std::endl;
return 0;
}
Without testing I suspect that a space between void and main could help you. voidmain() -> void main(). However if you already can compile the code my advice won't help.

This code shows error "stu undeclared"?? what should i do

I know this error is because i have declared stu inside the for loop scope but its the necessity of the program.I want to declare an array for each test case (test case should all be input at once).Suggest me a way to achieve this.Is dynamic memory an alternative.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int t;
cin>>t;
int n[t],g[t];
int m =0;
for(int w=0;w<t;t++)
{
cin>>n[w]>>g[w];
int stu[n[w]];
for(int i=0;i<n[w];i++)
{
cin>>stu[i];
}
}
while(m<t)
{
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]=1)
{
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]=n[m])
{
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++)
{
for(int k=(j+1);k<n[m];k++)
{
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
You are declaring stu inside of a for loop, so it is limited to the scope of the loop. You then try to use it outside of the loop, where it is undeclared.
for(int w=0;w<t;t++)
{
...
int stu[n[w]]; // Beware: stu is a VLA. Non-standard C++.
// OK to use stu here
...
}
// stu doesn't exist here
Also note that standard C++ does not support variable length arrays (VLAs), which is what you are attempting to use in the declaration of stu, as well as here:
int t;
cin>>t;
int n[t],g[t];
You can replace these arrays by std::vector<int>:
#include <iostream>
#include <vector>
int main()
{
int t=0;
cin>>t;
std::vector<int> n(t);
std::vector<int> g(t);
std::vector<int> stu ...;
}
The line
int stu[n[w]];
is inside a block and outside that block it won't be seen. You should move it out of the block, but doing so of course you can't use n[w], being w the looping var. You coudl put a limit to the max value n[w] can have, e.g.
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXV = 100;
int main()
{
int t;
cin>>t;
int n[t],g[t]; // <- supported by some compiler, but not std
int m =0;
int stu[MAXV];
for(int w=0;w<t;t++) {
cin>>n[w]>>g[w];
for(int i=0;i<n[w] && i < MAXV;i++) {
cin>>stu[i];
}
}
while(m<t) {
int a,b;
int e;
e = (n[m]*(n[m]-1))/2;
int diff[e];
if (g[m]==1) {
cout<<0<<endl;
return 0;
}
b=*(min_element(stu,stu+n[m]-1));
a=*(max_element(stu,stu+n[m]-1));
if (g[m]==n[m]) {
cout<<a-b<<endl;
return 0;
}
int z = 0;
for(int j=0;j<(n[m]-1);j++) {
for(int k=(j+1);k<n[m];k++) {
diff[z]=abs(stu[j]-stu[k]);
++z;
}
}
cout<<*(min_element(diff,diff+e-1))<<endl;
++m;
}
cin.ignore();
cin.get();
return 0;
}
(I've fixed a couple of assignment in conditional when I suppose you meant == and not =, but I've not tested if the code does what you expect: it just compile, with g++ but not with other compiler likely, see comment in code)