另外就是const string&也就是取代c里面的const char*的作用吧,但是下面代码编译的时候就出了问题,还不明白原因。
头文件kmp.h
#include <string>
using namespace std;
class Kmp
{
private:
string modelstr;
string mainstr;
int *next;
bool findnext();
public:
Kmp(const string& modstr,const string& mnstr);
~Kmp();
int substr();
};
实现文件kmp.cpp
#include "kmp.h"
using namespace std;
Kmp::Kmp(const string& modstr,const string& mnstr):modelstr(modstr),mainstr(mnstr)
{
next=new int[modelstr.length()];
findnext();
}
Kmp::~Kmp()
{
delete []next;
}
bool Kmp::findnext()
{
int s=0,t=0;
if(!next)
return false;
next[0]=0;
for(s=1,t=0;s<mainstr.length();s++)
{
while(t>0&&modelstr[s]!=modelstr[t])
t=next[t];
if(modelstr[s]==modelstr[t])
{
t++;
next[s]=t;
}
else
next[s]=0;
}
return true;
}
int Kmp::substr()
{
int s=0,i=0; //i是文本串指针,s是模式串指针;
if(!next)
return -1;
for(i=0,s=0;i<mainstr.length();i++)
{
while(s>0&&mainstr[i]!=modelstr[s])
s=next[s];
if(mainstr[i]==modelstr[s])
s++;
if(s==modelstr.length()-1)
return i-s;
}
return -1;
}
希望闲的无聊的大牛帮忙看下。c++提示的编译错误不知道怎么改。。
7 个解决方案
#1
没排好版
kmp.h
kmp.cpp
kmp.h
#include <string>
using namespace std;
class Kmp
{
private:
string modelstr;
string mainstr;
int *next;
bool findnext();
public:
Kmp(const string& modstr,const string& mnstr);
~Kmp();
int substr();
};
kmp.cpp
#include "kmp.h"
using namespace std;
Kmp::Kmp(const string& modstr,const string& mnstr):modelstr(modstr),mainstr(mnstr)
{
next=new int[modelstr.length()];
findnext();
}
Kmp::~Kmp()
{
delete []next;
}
bool Kmp::findnext()
{
int s=0,t=0;
if(!next)
return false;
next[0]=0;
for(s=1,t=0;s<mainstr.length();s++)
{
while(t>0&&modelstr[s]!=modelstr[t])
t=next[t];
if(modelstr[s]==modelstr[t])
{
t++;
next[s]=t;
}
else
next[s]=0;
}
return true;
}
int Kmp::substr()
{
int s=0,i=0; //i是文本串指针,s是模式串指针;
if(!next)
return -1;
for(i=0,s=0;i<mainstr.length();i++)
{
while(s>0&&mainstr[i]!=modelstr[s])
s=next[s];
if(mainstr[i]==modelstr[s])
s++;
if(s==modelstr.length()-1)
return i-s;
}
return -1;
}
#2
语法错误,楼主还是把编译器报的错贴出来吧
#3
Error 1 error C3646: ':modelstr' : unknown override specifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 3 error C2761: '{ctor}' : member function redeclaration not allowed e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 5 error C2448: 'mainstr' : function-style initializer appears to be a function definition e:\tools\vs projects\kmp\kmp\kmp.cpp 5 1 kmp
Error 2 error C2065: 'modstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 4 error C2065: 'mnstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 3 error C2761: '{ctor}' : member function redeclaration not allowed e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 5 error C2448: 'mainstr' : function-style initializer appears to be a function definition e:\tools\vs projects\kmp\kmp\kmp.cpp 5 1 kmp
Error 2 error C2065: 'modstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 4 error C2065: 'mnstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
#4
Kmp::Kmp(const string& modstr,const string& mnstr):modelstr(modstr),mainstr(mnstr)
这句话中":"是中文的
这句话中":"是中文的
#5
万分感谢,真不知道怎么打的。。请问是根据那个提示判断的呢?还是经验?
#6
提示是modelstr附近出错,就在这附近看看,然后就找到了
#7
谢谢,学习。
#1
没排好版
kmp.h
kmp.cpp
kmp.h
#include <string>
using namespace std;
class Kmp
{
private:
string modelstr;
string mainstr;
int *next;
bool findnext();
public:
Kmp(const string& modstr,const string& mnstr);
~Kmp();
int substr();
};
kmp.cpp
#include "kmp.h"
using namespace std;
Kmp::Kmp(const string& modstr,const string& mnstr):modelstr(modstr),mainstr(mnstr)
{
next=new int[modelstr.length()];
findnext();
}
Kmp::~Kmp()
{
delete []next;
}
bool Kmp::findnext()
{
int s=0,t=0;
if(!next)
return false;
next[0]=0;
for(s=1,t=0;s<mainstr.length();s++)
{
while(t>0&&modelstr[s]!=modelstr[t])
t=next[t];
if(modelstr[s]==modelstr[t])
{
t++;
next[s]=t;
}
else
next[s]=0;
}
return true;
}
int Kmp::substr()
{
int s=0,i=0; //i是文本串指针,s是模式串指针;
if(!next)
return -1;
for(i=0,s=0;i<mainstr.length();i++)
{
while(s>0&&mainstr[i]!=modelstr[s])
s=next[s];
if(mainstr[i]==modelstr[s])
s++;
if(s==modelstr.length()-1)
return i-s;
}
return -1;
}
#2
语法错误,楼主还是把编译器报的错贴出来吧
#3
Error 1 error C3646: ':modelstr' : unknown override specifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 3 error C2761: '{ctor}' : member function redeclaration not allowed e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 5 error C2448: 'mainstr' : function-style initializer appears to be a function definition e:\tools\vs projects\kmp\kmp\kmp.cpp 5 1 kmp
Error 2 error C2065: 'modstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 4 error C2065: 'mnstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 3 error C2761: '{ctor}' : member function redeclaration not allowed e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 5 error C2448: 'mainstr' : function-style initializer appears to be a function definition e:\tools\vs projects\kmp\kmp\kmp.cpp 5 1 kmp
Error 2 error C2065: 'modstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
Error 4 error C2065: 'mnstr' : undeclared identifier e:\tools\vs projects\kmp\kmp\kmp.cpp 4 1 kmp
#4
Kmp::Kmp(const string& modstr,const string& mnstr):modelstr(modstr),mainstr(mnstr)
这句话中":"是中文的
这句话中":"是中文的
#5
万分感谢,真不知道怎么打的。。请问是根据那个提示判断的呢?还是经验?
#6
提示是modelstr附近出错,就在这附近看看,然后就找到了
#7
谢谢,学习。