Frage

In these days I thought I would try to work on some C++ Object Oriented Programming by converting one of my old programs to an object which methods I could use in other programs. The program is able to get an array of strings and print them to make a selection screen like this:

"> Stuff"
" Stuff2"
" Stuff3"

And the cursor could then be moved with the keyboard arrows and one entry could be selected by pressing Enter.
Unfortunately, after having removed most errors the only left is a "Undefined reference to class::method()" for each method that I wrote.
The code is the following:

Selection.cpp

#include "selection.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <cstring>
#include <string>
#include <vector>
std::vector<entry> selectedArray;
int posizione = 1, tastoPremuto;    
void setCurrentArray(std::string selectionEntries[])
{
    //Copies a string array to a vector of struct entry
}

void resetSelection()
{
    //Blanks out every entry.segno
}

void selectionUpdate(int stato)
{
    //Moves the cursor
}

int getPosition()
{
    return posizione;  //Returns the position of the cursor
}

void setPosition(int nuovaPosizione)  //Sposta il puntatore alla nuovaPosizione
{
    posizione = nuovaPosizione;  //Sets the posiion of the cursor
}

entry getCurrentEntry(int posizioneCorrente)  //Returna l'entry della posizione attuale
{
    //Gets the name of the entry at which the cursor is
}

void printEntries()  //Stampa tutte le entries con la selezione
{
    //Prints all the entries with the cursor before them
}

int getSelection(bool needConfirm)
{
    //gets the selection from keyboard
}

Selection.h

#ifndef SELECTION_H
#define SELECTION_H
#include <string>
#include <vector>

struct entry
{
    std::string entryName;
    char sign;
};

class selection
{
   public:
    selection();
    virtual ~selection();
    void setCurrentArray(std::string selectionEntries[]);
    void resetSelection();
    int getPosition();
    void setPosition(int nuovaPosizione);
    entry getCurrentEntry(int posizioneCorrente);
    void printEntries(int argumentsNumber);
    int getSelection(bool needConfirm);
protected:
private:
    int tastoPremuto;
    int posizione;
    void selectionUpdate(bool stato);
    std::vector<entry> selectedArray;
};

#endif // SELECTION_H

main.cpp

#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <selection.h>
#include <vector>
#include <array>

std::string selezioneTestArray[3] = {"Prima selezione", "Seconda selezione", "Terza seleazione"};

int main()
{
    selection sel;
    sel.setCurrentArray(selezioneTestArray);
    sel.setPosition(0);
    sel.resetSelection();
    sel.printEntries(3);
    sel.getSelection(true);
    return 0;
}

If you need the whole code, it's in my Github: https://github.com/LiceoFederici2H/Vita-Da-Lavoratore
Sorry if it's in Italian but I meant to use this also as a school project.
Thanks in advance.

War es hilfreich?

Lösung

In source file (Selection.cpp), you have to qualify the method names with class name:

void Selection::setCurrentArray(std::string selectionEntries[])
{
  //Copies a string array to a vector of struct entry
}

Otherwise these methods will be free function, and not definition of the class methods. As a result, your class methods remain undefined, and when linker tries to link with the methods, it can not find them, and shows undefined reference of the method error.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top