我正在尝试执行此代码,但出现标题的符号错误:
配置文件.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;
}
和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 */
我遇到的符号错误:
未定义的符号:"configFile::filename",引用自:configFile::configFile()in configFile.o configFile::configFile()in configFile.o configFile::setConfigFileName(std::basic_string, std::allocator >)in configFile.o
"configFile::pinstance",引用自:configFile.o 中的 configFile::getInstance() configFile.o 中的
configFile::getInstance() "configFile::getConfiguration(std::basic_string std::char_traits, std: :allocator >, int)",引用自:main.o 中的 _main
ld:未找到符号
collect2:ld 返回 1 个退出状态
如果有必要: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]);
}
}
我正在运行 MaxOSX 10.6.8。提前感谢