【问题标题】:How to declare this function in a class [duplicate]如何在类中声明此函数[重复]
【发布时间】:2013-02-05 14:53:35
【问题描述】:

我试图在下面的这个函数中添加到我的班级

vector<string> &split(const string &s, char delim, vector<string> &elems) {

    stringstream ss(s);

    string item;

    while(getline(ss, item, delim)) {

        elems.push_back(item);

    }

    return elems;

}



vector<string> split(const string &s, char delim) {

    vector<string> elems;

    return split(s, delim, elems);

}

下面是我的类文件

#ifndef LOGIN_H  
#define LOGIN_H

#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>

using namespace std;


    class Login
    {

    private:
    ifstream infile;
    string sLine,username,password;
    stringstream ss;
    int counter,logCounter;
    bool verify,end;

    public:
    Login();
    Login(string,string);
    bool loginValidate(string,string);
    };

    Login::Login()
    {

    }

    Login::Login(string user,string pass)
    {
    username=user;
    password=pass;
    }





    bool Login::loginValidate(string user,string pass)
    {
    bool result,verify;
    result=false;


    /* Begin Read account File */
    infile.open("account.txt");

    if(infile.is_open())
    {
    while (infile.good())
    {
    getline(infile, sLine);

    if(sLine!="")
    {
    //do vector split
    vector<string> x = split(sLine,':');




    x.clear();

    }//end if sLine


    }//end while loop
    }//end if

    infile.close();

    return result;

    }//end function loginValidate



    #endif

我正在尝试在登录的 loginValidate 函数中使用 split 函数 - 登录类,但我尝试在上面的登录验证中声明它,我收到一些错误,例如

 user1@ubuntu:~/yes222/New folder$ g++ test.cpp login.cpp -o main
    /tmp/ccv6uiN1.o: In function `split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)':
    login.cpp:(.text+0x0): multiple definition of `split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)'
    /tmp/ccCcz7T2.o:test.cpp:(.text+0x0): first defined here
    /tmp/ccv6uiN1.o: In function `split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)':
    login.cpp:(.text+0xe9): multiple definition of `split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char)'
    /tmp/ccCcz7T2.o:test.cpp:(.text+0xe9): first defined here
    collect2: ld returned 1 exit status
    user1@ubuntu:~/yes222/New folder$ g++ test.cpp login.cpp -o main
    login.cpp: In member function ‘bool Login::loginValidate(std::string, std::string)’:

谁能指导我。谢谢!

以前我只是在一个主类中使用所有函数,但现在我试图将登录部分拆分到另一个类,因此我在该类中使用这个函数时遇到了问题..

【问题讨论】:

    标签: c++


    【解决方案1】:

    login.h

    #ifndef __LOGIN__
    #define __LOGIN__
    
    #include <fstream>
    #include <sstream>
    #include <iostream>
    #include <vector>
    #include <string>
    
    class Login
    {
    
        private:
            std::ifstream       infile;
            std::string         sLine,username,password;
            std::stringstream   ss;
            int                 counter,logCounter;
            bool                verify,end;
    
        public:
                                        Login(void);
                                        Login(std::string, std::string);
            bool                        loginValidate(std::string, std::string);
            std::vector<std::string>    split(const std::string& s, char delim);
            std::vector<std::string>&   split(const std::string& s, char delim, std::vector<std::string>& elems);
    };
    
    #endif
    

    login.cpp

    #include <login.h>
    
    Login::Login(void)
    {
    
    }
    
    Login::Login(std::string user, std::string pass)
    {
        username = user;
        password = pass;
    }
    
    bool
    Login::loginValidate(std::string user, std::string pass)
    {
        bool    result,verify;
    
        result = false;
        infile.open("account.txt");
        if (infile.is_open())
        {
            while (infile.good())
            {
                getline(infile, sLine);
                if (sLine != "")
                {
                    std::vector<std::string> x = split(sLine,':');
                    x.clear();
                }
            }
        }
        infile.close();
        return result;
    
    }
    
    std::vector<std::string>&
    Login::split(const std::string& s, char delim, std::vector<std::string>& elems)
    {
        std::stringstream   ss(s);
        std::string         item;
    
        while (getline(ss, item, delim))
            elems.push_back(item);
        return elems;
    }
    
    
    
    std::vector<std::string>
    Login::split(const std::string& s, char delim)
    {
        std::vector<std::string>    elems;
    
        return split(s, delim, elems);
    }
    

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您在标头中定义了此函数,而不是在源代码中。将函数定义移动到cpp或将它们标记为内联

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-07
        • 2010-12-27
        • 1970-01-01
        • 1970-01-01
        • 2010-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多