0

I'm trying to execute this code, but I'm getting the symbol error of the title:

configfile.cpp:

#include "configFile.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <stdexcept>
#include <map>

configFile* configFile::getInstance(){
    pinstance = new configFile();
    return pinstance;
}

configFile::configFile(){
    filename = "/Users/myfolder/NetBeansProjects/Oier_2/config.cfg";
}

void configFile::setConfigFileName(std::string s){
    filename = s;
}

float* getConfiguration(std::string type, int size) {
    std::string data[size];
    std::string line;
    std::ifstream myfile("/Users/myfolder/NetBeansProjects/Oier_2/config.cfg");

    while (std::getline(myfile, line)) {
            std::istringstream is_line(line);
            std::string key;
            if (std::getline(is_line, key, '=')) {
                if(key.compare(type) == 0){
                    for(int i=0; i<size;i++){
                        std::getline(is_line,data[i],',');
                    }
                }
            }
        }

    float *fdata;
    for(int i=0;i<size;i++){
        fdata[i] = (float)atof(data[i].c_str());
    }
    return fdata;
}

And configFile.h:

#include <string>

#ifndef CONFIGFILE_H
#define CONFIGFILE_H

class configFile {

private:
    static configFile* pinstance;
    static std::string filename;    
public:
    static configFile* getInstance();
    void setConfigFileName(std::string s);
    float* getConfiguration(std::string type, int size);
protected:
    configFile();
    configFile(const configFile& orig);

};

#endif  /* CONFIGFILE_H */

The symbols error I'm having:

Undefined symbols: "configFile::filename", referenced from: configFile::configFile()in configFile.o configFile::configFile()in configFile.o configFile::setConfigFileName(std::basic_string, std::allocator >)in configFile.o
"configFile::pinstance", referenced from: configFile::getInstance() in configFile.o
configFile::getInstance() in configFile.o "configFile::getConfiguration(std::basic_string std::char_traits, std::allocator >, int)", referenced from: _main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

If it's necessary: main.cpp:

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <algorithm>
#include "configFile.h"

using namespace std;

int main(int argc, char** argv) {
    configFile* cfg = configFile::getInstance();    

    string type = "tiempo";
    float* tiem = cfg->getConfiguration(type,3);

    for(int i=0; i< 3;i++){
        printf( " %f ", tiem[i]);
    }
}

I'm running a MaxOSX 10.6.8. Thanks in adavance

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
Avión
  • 7,963
  • 11
  • 64
  • 105

1 Answers1

1

These two areas are the issue:

configFile::configFile(){
    filename = "/Users/myfolder/NetBeansProjects/Oier_2/config.cfg";
}

void configFile::setConfigFileName(std::string s){
    filename = s;
}

You've declared filename as a static data member, so its name must always be configFile::filename. If you meant to use a data member, you can simply remove the static definition.

The difference between static and non-static data members is that with static, only one variable exists, whereas with a non-static data member there is an instance of this variable per-class. So in this case, the consequence is that with static, each instance of configFile would reference the same file path, whereas removing static would make each configFile own its own filename and so have its own file path.

  • I've removed the statics from the configFile.h file `configFile* pinstance;` and `configFile* getInstance();` But I'm getting the following error on (in main.cpp) the line `configFile* cfg = configFile::getInstance();`. This is the error: `main.cpp:47: error: cannot call member function 'configFile* configFile::getInstance()' without object` – Avión Jun 11 '13 at 15:29
  • 1
    @Borja the same logic applies of static functions too. Static variables and methods do not belong to a given instance of a class. You'll need to remove that too. –  Jun 11 '13 at 15:34
  • Can you please be more specific? I've removed all the `static` from the `configFile.cpp`and `configFile.h` files but I'm still having the same error in `main.cpp` – Avión Jun 11 '13 at 15:37
  • 1
    @Borja Ok, I've read the code a little more in depth. `getInstance` is currently doing two things: assigning a static member variable AND allocating a new pointer to it. If you do want that single static instance to exist, that's fine and allowed, but in code you need to refer to it as `configFile::pinstance`. If you want to completely do away with `static`, then the callee of `getInstance` simply needs to have a local variable of `configFile*`. The pattern I think you are after here is the [singleton pattern](http://stackoverflow.com/a/1008289/257111) ... –  Jun 11 '13 at 15:48
  • The answer I linked in above provides some good reading on implementing singletons in C++. –  Jun 11 '13 at 15:48
  • I've left the `static` and change `pinstance` to `configFile::pinstance` on the `configFile.cpp`file and still getting the same symbol error, argh! – Avión Jun 11 '13 at 15:58