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.
undefined reference to function in main()
-
19-06-2023 - |
Frage
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.
Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow