0

这是我的代码,我想使用 c++ 读取配置文件,我的代码在这里:

//myutils.h

#include <string>
#include <map>
using namespace std;

void read_login_data(char *login_data,map<string,string> &data_map); 

这是 myutils.cpp

//myutils.cpp
#include <fstream>
#include <string>
#include <map>
#include "myutils.h"

using namespace std;

void read_login_data(char *login_data,map<string,string> &data_map)
{
    ifstream infile;
    string config_line;
    infile.open(login_data);
    if (!infile.is_open())
    {
        cout << "can not open login_data";
        return false;

    }
    stringstream sem;
    sem << infile.rdbuf();
    while(true)
    {
        sem >> config_line;
        while(config_line)
        {
            size_t pos = config_line.find('=');
            if(pos == npos) continue;
            string key = config_line.substr(0,pos);
            string value = config_line.substr(pos+1);
            data_map[key]=value;

        }
    }


}

和我的 test.cpp 代码:

#include <iostream>
#include <map>
#include "myutils.h"

using namespace std;

int main()
{
    char login[] = "login.ini";
    map <string,string> data_map;

    read_login_data(login,data_map);
    cout<< data_map["BROKER_ID"]<<endl;

}

配置文件是:

BROKER_ID=66666
INVESTOR_ID=00017001033

当我使用 :g++ -o test test.cpp 编译它时,输出为:

  /tmp/ccOGrPpx.o: In function `main':                                    │[master 6a44d8a] change onrspsettlementinfo
    test.cpp:(.text+0x67): undefined reference to `read_login_data(char*, st│spsettlementinfo,I have forget to input Qry
    d::map<std::basic_string<char, std::char_traits<char>, std::allocator<ch│ 2 files changed, 2 insertions(+), 2 deleti
    ar> >, std::basic_string<char, std::char_traits<char>, std::allocator<ch│young001@server6:~/ctp/ctp_github$ git push
    ar> >, std::less<std::basic_string<char, std::char_traits<char>, std::al│Username for 'https://github.com': young001
    locator<char> > >, std::allocator<std::pair<std::basic_string<char, std:│Password for 'https://young001@github.com':
    :char_traits<char>, std::allocator<char> > const, std::basic_string<char│To https://github.com/young001/ctp_trade.gi
    , std::char_traits<char>, std::allocator<char> > > > >&)'               │   838ed90..6a44d8a  master -> master
    collect2: ld returned 1 exit status     

我不能锻炼,thx

4

2 回答 2

2

你也应该编译myutils.cpp

g++ -o test myutils.cpp test.cpp

或者,更好的是,单独编译它,然后链接:

g++ -c -o myutils.o    myutils.cpp
g++ -c -o test.o       test.cpp
g++    -o test         myutils.o test.o
于 2013-05-22T12:55:52.757 回答
1

您的代码中有很多问题首先,正如@soon 所说,使用编译

g++ -o test myutils.cpp test.cpp

除此之外,还有许多其他错误不会让您的代码编译

以下是更正后的 myutils.cpp。因为我在几个地方做了一些假设,所以要好好地过一遍。

//myutils.cpp
#include <fstream>
#include <string>
// You need to include sstream and iostream
#include <sstream>
#include <iostream>
#include <map>
#include "myutils.h"

using namespace std;

void read_login_data(char *login_data,map<string,string> &data_map)
{
    ifstream infile;
    string config_line;
    infile.open(login_data);
    if (!infile.is_open())
    {
        cout << "can not open login_data";
    // void doesn't return anything, you were returning false!
        return ;

    }
    stringstream sem;
    sem << infile.rdbuf();
    while(true)
    {
        // while(string) doesn't mean anything
        // the loop below will run till EOF of sem
        while(sem>>config_line)
        {
            size_t pos = config_line.find('=');
            if(pos == string::npos) continue;
            string key = config_line.substr(0,pos);
            string value = config_line.substr(pos+1);
            data_map[key]=value;

        }
    }


}
于 2013-05-22T13:00:47.770 回答