How do I add to an array from a static method?

C++ Passing struct array to function. Linker error undefined reference...?

  • Hi, I am new to C++ programming and I have a Class and array of structures. struct kontakt { string jmeno; string adresa; string telefon; }; class CPhoneBook { public: CPhoneBook ( void ); ~CPhoneBook ( void ); bool Add ( const string & name, const string & address, const string & phone ); bool Del ( const string & name, const string & address ); bool Search ( const string & name, const string & address, string & phone ) ; void quicksort ( struct kontakt array[], int min, int max); void swap(struct kontakt array[],int min, int max); void posun(int stred,const string & name,const string & address,const string & phone); void print(); private: int pocet; //pocet kontaktu v poli int size; //velikost pole int z; struct kontakt *ukkontakt;}; When calling for quicksort(ukkontakt,min,pocet); in method Add. It returns C:\Users\Bulwa\AppData\Local\Temp\ccIFba... In function `ZN10CPhoneBook3AddERKSsS1_S1_': [Linker error] undefined reference to `CPhoneBook::quicksort(kontakt*, int, int)' C:\Users\Bulwa\AppData\Local\Temp\ccIFba... ld returned 1 exit status The methods look like this void CPhoneBook::quicksort (struct kontakt array[], int min, int max) { if(min<max) { int b=min; for(int i=min+1;i<max;i++) { if array[i].jmeno>array[min].jmeno {swap(array,i,++b;) } } swap(array,min,b); quicksort(array,min,b); quicksort(array,b+1,max); } void CPhoneBook::swap(struct kontakt array[],int min, int max) { int x=array[max].jmeno; array[max].jmeno=array[min].jmeno; array[min].jmeno=x; } What am i doing wrong ? Thanks. This is the Add Class. I didnt copy the whole code, it is too long and irrelevant =P bool CPhoneBook::Add ( const string & name,const string & address,const string & phone ) { int x,y; for(y=pocet;y size) { size=size*2; kontakt *uk2=ukkontakt; ukkontakt=new kontakt[size]; for(x=0;x bool CPhoneBook::Add ( const string & name,const string & address,const string & phone ) { int x,y; for(y=pocet;y size) { size=size*2; kontakt *uk2=ukkontakt; ukkontakt=new kontakt[size]; for(x=0;x

  • Answer:

    I can't see the Add method here, but I'll guess that it might be calling quicksort as a static method, but the class defines it as an instance method. It really ought to be static, since it doesn't use any fields or methods of a CPhoneBook object. Same for swap(). So, if you're calling CPhoneBbook::quicksort(somearray, min, max) in the Add() method, go to the class declaration and add "static" in front of the declarations for quicksort() and swap(). Don't change the implementation.

husoski at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

I can't see the Add method here, but I'll guess that it might be calling quicksort as a static method, but the class defines it as an instance method. It really ought to be static, since it doesn't use any fields or methods of a CPhoneBook object. Same for swap(). So, if you're calling CPhoneBbook::quicksort(somearray, min, max) in the Add() method, go to the class declaration and add "static" in front of the declarations for quicksort() and swap(). Don't change the implementation.

husoski

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.