字符串作为函数中的参数

问题描述:

我编写了一个函数来搜索它的参数是否在给定的表retrieveb1.txt中.... optab表有两列,其中参数只能位于第一列。 ..in的aviasm.h文件我写了这个代码....字符串作为函数中的参数

class aviasm 
{ 
public: 
    aviasm(char *,char *); 
    ~aviasm(); 

    void crsymtab(); 
    bool in1(string); 

} 

在aviasm.cpp文件我写...

bool aviasm::in1(string s) 
{ 
ifstream in("optab1.txt",ios::in);//opening the optab1.txt 
char c; 
string x,y; 
while((c=in.get())!=EOF) 
{ 
    in.putback(c);//putting back the charcter into stream 
    in>>x;//first field 
    in>>y; 
    if(x==s) 
     return true; 
    else 
     return false; 
} 
} 

,但我遇到了几个编译错误.. ..

'bool aviasm::in1(std::string)' : overloaded member function not found in 'aviasm' 
'aviasm::in1' : function does not take 1 arguments 
'syntax error : identifier 'string' 

...任何人都可以帮忙吗?

+5

包括在.h文件中? –

+3

你用分号结束了类的定义吗? – Mahesh

+0

此外,通常只需将只读std :: string对象作为const std :: string&'传递给函数即可。注意'const'和'&'(引用)。 –

看来好像你要使用字符串,不正确的声明,则需要在这个文件的顶部:

#include <string> 
using std::string; 
+1

他也可能发现用分号结束他的类定义很有帮助。 – Hugh

+0

@vaughn ....我没有使用名称空间std ....但它应该使用,即使我们不使用任何字符串函数 – avinash