首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在定义类的成员函数时包含参数将返回一个错误

在定义类的成员函数时包含参数将返回一个错误
EN

Stack Overflow用户
提问于 2010-11-11 22:22:00
回答 2查看 290关注 0票数 1

我有以下类定义,用C++编写,驻留在它自己的头文件(ManageFeed.h)中

代码语言:javascript
复制
#include <string>
#include <fstream>
#include <iostream>
#include <cstring>

class ManageFeed
{
    bool get_feed();
    void format_feed();
    bool refresh_feed();
    int find_start_of_string(string tag, ifstream& rssfile);
public:
    void display_feed_list();
    void display_feed_item();
    ManageFeed();
};

当我试图编译这段代码时,我会得到以下错误

代码语言:javascript
复制
custom-headers/ManageFeed.h:22: error: ‘string’ has not been declared
custom-headers/ManageFeed.h:22: error: ‘ifstream’ has not been declared

我发现,如果从int find_start_of_string()函数中删除参数,那么我可以成功地编译代码,但如果要将数据传递到函数中,不需要这些参数吗?如果我试图从main()调用此函数,则会收到以下错误

代码语言:javascript
复制
reader.cpp:6: error: prototype for ‘void ManageFeed::find_start_of_string(std::string, std::ifstream&)’ does not match any in class ‘ManageFeed’

因此,显然需要该函数才能使用。我使用的教科书在自己的头文件中有类定义的例子,其中包含参数,但是在我的代码的结构上似乎没有其他区别,也没有给出为什么图书代码工作和我的代码不工作的任何解释。

问题:是定义中不需要参数( ManageFeed.cpp中的函数定义指定了参数)还是我在这里做错了什么?

如果有人感兴趣,这是我的申请文件

代码语言:javascript
复制
#include "custom-headers/ManageFeed.h"
using namespace std;

ifstream rssfile; 
const string tag;
void ManageFeed::find_start_of_string(string tag, ifstream& rssfile);

int main()
{ 
      ManageFeed manage_example;
      rssfile.open("rss.xml");
      manage_example.find_start_of_string(tag, rssfile);

     return 0;
}

以及ManageFeed的实现文件

代码语言:javascript
复制
#include "ManageFeed.h"

#include <iostream>
#include <string>
#include <cstring>

ManageFeed::ManageFeed()
{
}

/*
    A function that will get the location of the RSS file from the user
*/
bool ManageFeed::get_feed()
{
cout << "Please specify the location of the feed: " << endl << endl;
cin >> feed_source;
return true;
}

 void ManageFeed::store_feed()
{
ifstream source_feed;
source_feed.open(feed_source);

ifstream local_feed;
local_feed.open
(
    "/../File System/Feed Source Files/Example Feed/Example Feed.xml"
);
local_feed << source_feed;
 }

 int ManageFeed::find_start_of_string(string tag, ifstream& rssfile)
 {
bool return_value = false;
string line;
size_t found;
do
{
    getline(rssfile, line, '\n');
    found = line.find(tag);
    if (found != string::npos)
    {
        return_value = true;
        return found;
    }
} while (!return_value && !rssfile.eof());

if (!return_value)
{
}
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-11 22:34:11

约翰有正确的解决办法。这是推理。

字符串和ifstream都存在于一个名为std的命名空间中。当您说string时,您是在告诉编译器查看全局命名空间并找到一个名为string的令牌。没有这回事。您必须告诉编译器在哪里可以找到字符串。

要做到这一点,您可以使用std::string和std::ifstream作为前缀,也可以在头文件的顶部添加using namesapce std;

仔细看一看,您的.cpp文件中确实有使用指令,但是将它放在包含头的后面。这意味着编译器在不使用命名空间的情况下解析标头,然后使用它解析文件的其余部分。如果您只是将using指令移到头包含的上方,它也将修复您的问题。但是,请注意,使用标头的任何其他东西也需要这样做。因此,以这种方式启动.cpp文件:

代码语言:javascript
复制
using namespace std;
#include "custom-headers/ManageFeed.h"
票数 2
EN

Stack Overflow用户

发布于 2010-11-11 22:25:22

更改:

代码语言:javascript
复制
int find_start_of_string(string tag, ifstream& rssfile);

至:

代码语言:javascript
复制
int find_start_of_string(std::string tag, std::ifstream& rssfile);

旁白:为什么今天会有这么多这样的问题?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4159853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档