0

在 .h 文件中,我有以下代码。

#ifndef COUNTEDLOCATIONS
#define COUNTEDLOCATIONS

#include <iostream>
#include <string>
struct CountedLocations {
CountedLocations();
CountedLocations(std::string url, int counter);
std::string url;
int count;
//below is code for a later portion of the project
bool operator== (const CountedLocations&) const;
bool operator< (const CountedLocations&) const;
};

在包含 .h 文件的 .cpp 文件中,我的代码是

#include "countedLocs.h"
#include <iostream>
#include <string>
using namespace std;
CountedLocations(std::string url, int counter)
{

}

我在 'url' 之前收到错误“Expected ')'。我尝试注释掉 .h 文件中的空构造函数,我尝试使用分号,我尝试删除前缀为 ' 的 std:: string url',但似乎没有任何效果。我尝试在 StackOverflow 上查看类似的问题,但所有三个解决方案均无济于事。我该如何解决这个问题?

编辑:最初,我有

CountedLocations::CountedLocations(std::string url, int counter) 

代替

CountedLocations(std::string url, int counter)

但这给了我错误“成员'CountedLocations'[-fpermissive]上的额外资格'CountedLocations::',所以我选择不使用它。

4

3 回答 3

2

将需要#include <string>从 .cpp 移动到 .h 文件,以便文件countedLocs.h知道std::string定义。在您使用一个 cpp 的情况下,您可以切换包含的顺序,但如果您还打算在其他地方使用它,最好将它放在标题中(countedLocs.h)。

#include <iostream>
#include <string>
#include "countedLocs.h"
于 2013-11-13T18:07:36.840 回答
2

如果这确实是您的所有代码,那么在定义结构之前您没有 std::string 的定义(即 #include )。

.h 文件应该可以自己编译。将 #include 放在 .h 文件中(还有一些包含守卫!)

于 2013-11-13T18:10:03.353 回答
1

在你的 cpp 文件(不是你的头文件)中,你应该有这个:

CountedLocations::CountedLocations(std::string url, int counter)
{

}

不是这个:

CountedLocations(std::string url, int counter)
{

}

但这给了我错误“成员'CountedLocations'[-fpermissive]上的额外资格'CountedLocations::',所以我选择不使用它。

如果您将限定条件放在类主体中的构造函数声明上,这就是您会得到的错误。

于 2013-11-13T18:18:08.067 回答