我尝试将以下函数添加到我的班级
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)’:
任何人都可以指导我。谢谢!
以前的函数我只是在主类中使用all,但现在我尝试将登录部分拆分为另一个类,因此我遇到了在该类中使用此函数的问题..
答案 0 :(得分: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);
}
答案 1 :(得分:0)
之所以发生这种情况是因为您在标题中定义了此函数,而不是在源文件将函数定义移至cpp
或将其标记为内嵌