0

I am coding Quicksort to sort binary files that contains different data type(structs). This is what i wrote so far:

void quicksort(){
    int izbor;
    char naziv_datoteke[20];
    cout << "Izaberite datoteku koju zelite sortirati: "<<endl;
    cout << "1 - sifra.ind "<<endl;
    cout << "2 - ime.ind "<<endl;
    cout << "3 - prezime.ind "<<endl;
    cin >>izbor;
    switch(izbor){
        case 1:strcpy(naziv_datoteke, "sifra.ind");
            typedef tsifra slog;
            break;
        case 2:strcpy(naziv_datoteke, "ime.ind");
            typedef time slog;
            break;
        case 3:strcpy(naziv_datoteke, "prezime.ind");
            typedef tprezime slog;
            break;
    }

    int broj_zapisa;
    dat.open(naziv_datoteke, ios::in|ios::out|ios::binary);
    dat.seekg(0, ios::end);
    broj_zapisa=dat.tellg()/sizeof(slog);
    //  quicksort(0, broj_zapisa-1);
    dat.close();
}

I am getting this error:

conflicting declaration 'typedef struct time slog'

I would like to define slog as data type that is stored in file so I can use it later for getting the size of that struct and few other things.

payloc91
  • 3,724
  • 1
  • 17
  • 45
Kris Bob
  • 11
  • 4
  • 7
    C++ does not work this way. `typedef` defines an alias whose scope starts at the definition, and lasts until the end of its enclosing scope. You don't get to "redefine" typedef, and it's not some kind of an executable statement that takes effect from that point on. There are many different ways of accomplishing the basic idea of what you're trying to do, using template, inheritance, or various other design patterns. I suggest that you continue reading your C++ book to learn all about these topics, in order to decide which is the best one for your situation. – Sam Varshavchik Nov 24 '17 at 16:42
  • ^That, and please show what you want to achieve with `slog` after the `switch`. – Passer By Nov 24 '17 at 18:02

1 Answers1

0

Touching on what Sam mentioned: please have a look at this answer and this page for further information on the topic. Hopefully they help you understand the bigger picture.

To answer your question directly: Defining slog as a data type at runtime is just gonna be messy. More details here.

A quick solution to what you want would be to define a struct slog, which can hold different information for you, like so:

struct slog()
{
    int my_val_int = -1;
    char* my_val_char = "";
}

Once it's time to return the value, you just check which value is actually set by checking if my_val_int is anything different than your default or if char is not empty.

Mind you, this is a very quick and dirty way of doing it. Please use it only as a starting point to develop your own idea of how to structure the program, so it works in a lean and mean manner! :-)

Goodluck!

Sipty
  • 1,159
  • 1
  • 10
  • 18
  • Thank you. Is there any simpler way to make function work with different data types? – Kris Bob Nov 24 '17 at 17:34
  • Yes! I will jot down a quick solution for you in the answer in a second. Again, though, I urge you to take a look at the links mentioned above. – Sipty Nov 24 '17 at 17:37