C++ 错误:未在作用域中声明
C++ Error: Was not declared in the scope
嘿伙计们,我正在做一个项目,在我碰到这堵墙之前,我做得很好。
我收到两个错误:
错误:"二进制搜索"未在此范围内声明
错误:"addInOrder"未在此范围内声明
这是我的文件,我已经尝试了很多事情,但无济于事。帮助将不胜感激。
直方图.cpp
#include "histogram.h"
#include "countedLocs.h"
//#include "vectorUtils.h"
#include <string>
#include <vector>
using namespace std;
void histogram (istream& input, ostream& output)
{
// Step 1 - set up the data
vector<CountedLocations> countedLocs;
// Step 2 - read and count the requested locators
string logEntry;
getline (input, logEntry);
while (input)
{
string request = extractTheRequest(logEntry);
if (isAGet(request))
{
string locator = extractLocator(request);
int position = binarySearch (countedLocs,
CountedLocations(locator, 0));
/** Hint - when looking CountedLocations up in any kind
of container, we really don't care if the counts match up
or not, just so long as the URLs are the same. ***/
if (position >= 0)
{
// We found this locator already in the array.
// Increment its count
++countedLocs[position].count;
}
else
{
// This is a new locator. Add it.
CountedLocations newLocation (locator, 1);
addInOrder (countedLocs, newLocation);
}
}
getline (input, logEntry);
}
// Step 3 - write the output report
for (int i = 0; i < countedLocs.size(); ++i)
output << countedLocs[i] << endl;
}
伯爵位.cpp
#include "countedLocs.h"
#include <iostream>
#include <vector>
using namespace std;
int CountedLocations::binarySearch(const vector<CountedLocations> list, CountedLocations searchItem)
{
//Code was here
}
int CountedLocations::addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value)
{
//Code was here
}
countedLocs.h
#ifndef COUNTEDLOCATIONS
#define COUNTEDLOCATIONS
#include <iostream>
#include <string>
#include <vector>
struct CountedLocations
{
std::string url;
int count;
CountedLocations (){
url = "";
count = 0;
}
CountedLocations(std::string a, int b){
url = a;
count = b;
}
int addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value);
int binarySearch (const std::vector<CountedLocations> list, CountedLocations searchItem);
};
inline
std::ostream& operator<< (std::ostream &out, CountedLocations& cL)
{
//out << "URL: " << cL.url << " count: " << cL.count << std::endl;
out << """ << cL.url << ""," << cL.count;
return out;
}
#endif
这些方法是 CountedLocations 的成员方法...使用something.extractLocator
和something.binarySearch
或使histogram()
也成为计数位置的成员方法...(something
属于CountedLocations
类型,很可能是countedLocs[position]
)
你有一个自由函数histogram
,你在其中尝试使用两个成员函数,addInOrder
和 binarySearch
。为了使用它们,您需要有一个 CountedLocations
的实例。
如果这些是某种不依赖于实际CountedLocations
实例的辅助函数,我会将它们转换为这样的静态函数(您只需要更改标头):
static int addInOrder (std::vector<CountedLocations>& vectr, CountedLocations value);
然后,您可以通过指定类的类型来调用此函数:
CountedLocations::addInOrder(...);
您正在尝试调用没有该类型对象的结构的成员方法。奇怪。
您需要查看命名空间是什么。
你声明了一个类 CountedLocations,到目前为止一切顺利。但是,您尝试在 CountedLocations 命名空间之外使用成员函数,这显然永远不会起作用。
int position = binarySearch (countedLocs,
CountedLocations(locator, 0));
二进制搜索是 CountedLocations 命名空间的成员函数。如果要调用该函数,则必须创建一个包含对该成员函数的引用的对象。
CountedLocation myObject;
int position = myObject.binarySearch (countedLocs, CountedLocations(locator, 0));
我不知道这是否能解决你的问题,但在你尝试解决问题之前,你应该知道这一点。
- 未在作用域中声明unordered_map
- 未在此作用域OpenCV3.4中声明cvSaveImage
- 未在此作用域中声明的函数和变量 (C++)
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 重载时未在 C++ 的作用域错误中声明
- 在 C++ 中使用 "transform" 会给出一个错误,指出这未在作用域中声明
- Tictactoe 游戏变量未在作用域中声明
- 当变量在多个函数作用域中使用时,我应该在类 private 中声明该变量吗?
- 如何在C++中嵌套词法作用域可访问的作用域中声明静态信息?
- gets()未在作用域中声明
- 在C++14中,在哪个作用域中声明了重新声明枚举的非范围枚举器
- C++11 外部作用域变量声明为 auto
- 未在此作用域中声明类 (c++)
- 如何声明在函数作用域中定义的结构
- 未在此作用域中声明函数,即使存在头文件也是如此
- 变量声明中结构的作用域解析是什么意思
- 在C++中,在给定的相同作用域内声明相同的变量名
- 我可以使用相同的名称为周围作用域中的类型声明成员类型别名吗
- 类未在作用域中声明,但已声明该类
- 使用新的外部函数作用域声明和初始化变量