C++ cpp关于每种用法

C++ cpp关于每种用法,c++,g++,C++,G++,这是我的代码,我想用C++读取配置文件 我的代码在这里: //myutils.h #include <string> #include <map> using namespace std; void print(pair<string,string> &p); void read_login_data(char *login_data,map<string,string> &data_map); 当我使用:g++-o te

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

//myutils.h

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

void print(pair<string,string> &p);

void read_login_data(char *login_data,map<string,string> &data_map); 
当我使用:g++-o test test.cpp myutils.cpp编译它时,输出是:

young001@server6:~/ctp/ctp_github/trader/src$ g++ -o test test.cpp myutils.cpp
In file included from /usr/include/c++/4.6/algorithm:63:0,
                 from test.cpp:3:
/usr/include/c++/4.6/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >, _Funct = void (*)(std::pair<std::basic_string<char>, std::basic_string<char> >&)]’:
test.cpp:15:48:   instantiated from here
/usr/include/c++/4.6/bits/stl_algo.h:4379:2: error: invalid initialization of reference of type ‘std::pair<std::basic_string<char>, std::basic_string<char> >&’ from expression of type ‘std::pair<const std::basic_string<char>, std::basic_string<char> >’
young001@server6:~/ctp/ctp\u github/trader/src$g++-o test test.cpp myutils.cpp
在/usr/include/c++/4.6/algorithm:63:0中包含的文件中,
来自测试。cpp:3:
/usr/include/c++/4.6/bits/stl_algo.h:在函数“\u Funct std::for_each(\u IIter,\u IIter,\u Funct,\u Funct)[with _IIter=std::\u Rb_tree_迭代器,\u Funct=void(*)(std::pair&)]中:
test.cpp:15:48:从此处实例化
/usr/include/c++/4.6/bits/stl_algo.h:4379:2:错误:从“std::pair”类型的表达式初始化“std::pair&”类型的引用无效
好像是关于对的引用,如何修改才能工作?

我相信这句话:

void print(pair<string,string> &p)
我相信这句话:

void print(pair<string,string> &p)

问得好,我想要的所有信息都在那里!如果我的答案不能解决它,我会复制并尝试自己编译,但我几乎肯定这是这段代码的唯一问题。那
而(config_line)
是愚蠢的。重新考虑你的循环条件。问得好,我想要的所有信息都在那里!如果我的答案不能解决它,我会复制并尝试自己编译,但我几乎肯定这是这段代码的唯一问题。那
而(config_line)
是愚蠢的。重新考虑您的循环条件。当我添加const时,它告诉:/tmp/ccOKyd69.o:In function
main':test.cpp:(.text+0x87):对
print(std::pair&)'collect2:ld的未定义引用返回了1个退出状态,因此我们修复了一个问题,没有,我们还有另一个问题。喜欢模板化的.h文件及其编译器输出。完全是虚构的。我会在我的机器上建立这个,给我一分钟。这是我的错,我没有修改myutils.cpp。现在它编译时没有警告或错误,但它无法打印地图内容。当我运行程序时:./test,没有打印。我不喜欢你在文件中读取循环。你确定这工作正常吗?我修改了它,并放置了一个我更喜欢的版本。但是如果没有输入文件,我真的帮不上忙。您可以尝试在“read”函数之外的映射中填充一个值,以确保这确实是问题所在。当我添加const时,它告诉:/tmp/ccOKyd69.o:in function
main:test.cpp:(.text+0x87):未定义对
print(std::pair&)'collect2:ld的引用返回了1个退出状态,因此我们修复了一个问题,没有,我们还有另一个问题。喜欢模板化的.h文件及其编译器输出。完全是虚构的。我会在我的机器上建立这个,给我一分钟。这是我的错,我没有修改myutils.cpp。现在它编译时没有警告或错误,但它无法打印地图内容。当我运行程序时:./test,没有打印。我不喜欢你在文件中读取循环。你确定这工作正常吗?我修改了它,并放置了一个我更喜欢的版本。但是如果没有输入文件,我真的帮不上忙。您可以尝试在“read”函数之外的映射中填充一个值,以确保这确实是问题所在。
young001@server6:~/ctp/ctp_github/trader/src$ g++ -o test test.cpp myutils.cpp
In file included from /usr/include/c++/4.6/algorithm:63:0,
                 from test.cpp:3:
/usr/include/c++/4.6/bits/stl_algo.h: In function ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >, _Funct = void (*)(std::pair<std::basic_string<char>, std::basic_string<char> >&)]’:
test.cpp:15:48:   instantiated from here
/usr/include/c++/4.6/bits/stl_algo.h:4379:2: error: invalid initialization of reference of type ‘std::pair<std::basic_string<char>, std::basic_string<char> >&’ from expression of type ‘std::pair<const std::basic_string<char>, std::basic_string<char> >’
void print(pair<string,string> &p)
void print(pair<const string,string> &p)
while(getline(infile, config_line)) {
    size_t pos = config_line.find('=');
    if(pos != string::npos) {
        string key = config_line.substr(0,pos);
        string value = config_line.substr(pos+1);
        data_map[key]=value;
    } else {
        cout << "BAD INPUT PAIR" << endl; //throw exception?
    }
}
blah = bigblah
no equals on this one