Rokusjar
Rokusjar

Reputation: 209

Function compiles correctly, but it doesn't compile as a member function of a class

I am new to c++ and I dont know much yet. I have this strange problem. I have a function which is working correctly, but when I try to run it as a member function of a class without any change, it does not work it says: undefined reference to gsiread::get_rows(char *)

#include <string>
#include <vector>
#include <fstream>

using namespace std;
//vector<string> get_rows ( char filepath[] );  ... it works

class gsiread  {

        public:
        vector<string> get_rows ( char filepath[] ); ... it doesnt work

        private:
        };

vector<string> get_rows ( char filepath[] ) {

   vector<string> data;
   string str;

   ifstream file;
   file.open(filepath);

    if( file.is_open() ) {
        while( getline(file,str) ) {

        if ( str.empty() ) continue;
        data.push_back(str);

        }
    }
    return data;
}

// This part is "like" main i am using Qt creator and i have copied parts of code
   from separate files

gsiread obj;

vector<string> vypis;
vypis = obj.get_rows("ninja.txt"); ....... //This doesnt work

vypis = get_rows("ninja.txt");  .......... //This works if I put declaration of
                                           //function get_rows outside the class and
                                           //and use // on declaration inside the class

for( int i = 0; i < vypis.size(); i++ ) {

    QString n = QString::fromStdString(vypis[i]);

    QString t = "%1 \n";

    ui->plainTextEdit->insertPlainText(t.arg(n));


    // QString is like string but zou have to use it if wanna use widgets
    // of qt (I think )
}

Upvotes: 1

Views: 91

Answers (3)

templatetypedef
templatetypedef

Reputation: 373022

Notice that you've defined the function as

vector<string> get_rows ( char filepath[] ) {
   ...
}

C++ treats this as a free function, not a member function, because you didn't mention that it belongs to the class. It treats your function get_rows as a completely different entity than gsiread::get_rows, and the linker error you're getting arises because the compiler can't find gsi::get_rows.

Try changing this to read

vector<string> gsiread::get_rows ( char filepath[] ) {
    ...
}

More generally, even if a function is defined in the same source file as a class, C++ will not assume that it's part of the class. You need to either

  • define it inside the class body, or
  • explicitly prefix it with the class name

in order to make the function a member function.

Hope this helps!

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227498

When you define the member function, you need to put it in the class' scope:

vector<string> gsiread::get_rows ( char filepath[] ) { .... }
//             ^^^^^^^^^

Otherwise, it is treated as a non-member function, and your member function is declared but not defined, leading to the error.

Upvotes: 1

simonc
simonc

Reputation: 42185

If you want get_rows to be a member of gsiread, its implementation needs to show this

vector<string> gsiread::get_rows( char filepath[] ) {
//             ^^^^^^^^^

Upvotes: 2

Related Questions