- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
此代码产生 17 错误 C2995:函数模板已被定义;在添加#include "set.h" header 之前存在一组单独的错误。有一个与此关联的私有(private) .cpp 和 .h 文件。
/*
* File: private/set.cpp
* Last modified on Thu Jun 11 09:34:08 2009 by eroberts
* -----------------------------------------------------
* This file contains the implementation of the set.h interface.
* Because of the way C++ compiles templates, this code must be
* available to the compiler when it reads the header file.
*/
//#ifdef _set_h //original code
#ifndef _set_h
#define _set_h
#include "stdafx.h"
#include "set.h"
using namespace std;
template <typename ElemType>
Set<ElemType>::Set(int (*cmp)(ElemType, ElemType)) : bst(cmp) {
cmpFn = cmp;
}
template <typename ElemType>
Set<ElemType>::~Set() {
/* Empty */
}
template <typename ElemType>
int Set<ElemType>::size() {
return bst.size();
}
template <typename ElemType>
bool Set<ElemType>::isEmpty() {
return bst.isEmpty();
}
template <typename ElemType>
void Set<ElemType>::add(ElemType element) {
bst.add(element);
}
template <typename ElemType>
void Set<ElemType>::remove(ElemType element) {
bst.remove(element);
}
template <typename ElemType>
bool Set<ElemType>::contains(ElemType element) {
return find(element) != NULL;
}
template <typename ElemType>
ElemType *Set<ElemType>::find(ElemType element) {
return bst.find(element);
}
template <typename ElemType>
void Set<ElemType>::clear() {
bst.clear();
}
/*
* Implementation notes: Set operations
* ------------------------------------
* The code for equals, isSubsetOf, unionWith, intersectWith, and subtract
* is similar in structure. Each one uses an iterator to walk over
* one (or both) sets, doing add/remove/comparision.
*/
template <typename ElemType>
bool Set<ElemType>::equals(Set & otherSet) {
if (cmpFn != otherSet.cmpFn) {
Error("Equals: sets have different comparison functions");
}
Iterator thisItr = iterator(), otherItr = otherSet.iterator();
while (thisItr.hasNext() && otherItr.hasNext()) {
if (cmpFn(thisItr.next(), otherItr.next()) != 0) return false;
}
return !thisItr.hasNext() && !otherItr.hasNext();
}
template <typename ElemType>
bool Set<ElemType>::isSubsetOf(Set & otherSet) {
if (cmpFn != otherSet.cmpFn) {
Error("isSubsetOf: sets have different comparison functions");
}
Iterator iter = iterator();
while (iter.hasNext()) {
if (!otherSet.contains(iter.next())) return false;
}
return true;
}
template <typename ElemType>
void Set<ElemType>::unionWith(Set & otherSet) {
if (cmpFn != otherSet.cmpFn) {
Error("unionWith: sets have different comparison functions");
}
Iterator iter = otherSet.iterator();
while (iter.hasNext()) {
add(iter.next());
}
}
/*
* Implementation notes: intersectWith
* -----------------------------------
* The most obvious way to write this method (iterating over
* one set and deleting members that are not in the second)
* fails because you can't change the contents of a collection
* over which you're iterating. This code puts the elements
* to be deleted in a vector and then deletes those.
*/
template <typename ElemType>
void Set<ElemType>::intersectWith(Set & otherSet) {
if (cmpFn != otherSet.cmpFn) {
Error("intersectWith:"
" sets have different comparison functions");
}
Iterator iter = iterator();
Vector<ElemType> toDelete;
while (iter.hasNext()) {
ElemType elem = iter.next();
if (!otherSet.contains(elem)) toDelete.add(elem);
}
for (int i = 0; i < toDelete.size(); i++) {
remove(toDelete[i]);
}
}
template <typename ElemType>
void Set<ElemType>::intersect(Set & otherSet) {
if (cmpFn != otherSet.cmpFn) {
Error("intersect: sets have different comparison functions");
}
intersectWith(otherSet);
}
template <typename ElemType>
void Set<ElemType>::subtract(Set & otherSet) {
if (cmpFn != otherSet.cmpFn) {
Error("subtract: sets have different comparison functions");
}
Iterator iter = otherSet.iterator();
while (iter.hasNext()) {
remove(iter.next());
}
}
template <typename ElemType>
void Set<ElemType>::mapAll(void (*fn)(ElemType)) {
bst.mapAll(fn);
}
template <typename ElemType>
template <typename ClientDataType>
void Set<ElemType>::mapAll(void (*fn)(ElemType, ClientDataType &),
ClientDataType & data) {
bst.mapAll(fn, data);
}
/*
* Set::Iterator class implementation
* ----------------------------------
* The Iterator for Set relies on the underlying implementation of the
* Iterator for the BST class.
*/
template <typename ElemType>
Set<ElemType>::Iterator::Iterator() {
/* Empty */
}
template <typename ElemType>
typename Set<ElemType>::Iterator Set<ElemType>::iterator() {
return Iterator(this);
}
template <typename ElemType>
Set<ElemType>::Iterator::Iterator(Set *setptr) {
iterator = setptr->bst.iterator();
}
template <typename ElemType>
bool Set<ElemType>::Iterator::hasNext() {
return iterator.hasNext();
}
template <typename ElemType>
ElemType Set<ElemType>::Iterator::next() {
return iterator.next();
}
template <typename ElemType>
ElemType Set<ElemType>::foreachHook(FE_State & fe) {
if (fe.state == 0) fe.iter = new Iterator(this);
if (((Iterator *) fe.iter)->hasNext()) {
fe.state = 1;
return ((Iterator *) fe.iter)->next();
} else {
fe.state = 2;
return ElemType();
}
}
#endif
头文件
/*
* File: set.h
* Last modified on Thu Jun 11 09:17:43 2009 by eroberts
* modified on Tue Jan 2 14:34:06 2007 by zelenski
* -----------------------------------------------------
* This interface file contains the Set class template, a
* collection for efficiently storing a set of distinct elements.
*/
#ifndef _set_h
#define _set_h
#include "cmpfn.h"
#include "bst.h"
#include "vector.h"
#include "foreach.h"
/*
* Class: Set
* ----------
* This interface defines a class template that stores a collection of
* distinct elements, using a sorted relation on the elements to
* provide efficient managaement of the collection.
* For maximum generality, the Set is supplied as a class template.
* The element type is determined by the client. The client configures
* the set to hold values of a specific type, e.g. Set<int> or
* Set<studentT>. The one requirement on the element type is that the
* client must supply a comparison function that compares two elements
* (or be willing to use the default comparison function that uses
* the built-on operators < and ==).
*/
template <typename ElemType>
class Set {
public:
/* Forward references */
class Iterator;
/*
* Constructor: Set
* Usage: Set<int> set;
* Set<student> students(CompareStudentsById);
* Set<string> *sp = new Set<string>;
* -----------------------------------------
* The constructor initializes an empty set. The optional
* argument is a function pointer that is applied to
* two elements to determine their relative ordering. The
* comparison function should return 0 if the two elements
* are equal, a negative result if first is "less than" second,
* and a positive resut if first is "greater than" second. If
* no argument is supplied, the OperatorCmp template is used as
* a default, which applies the bulit-in < and == to the
* elements to determine ordering.
*/
Set(int (*cmpFn)(ElemType, ElemType) = OperatorCmp);
/*
* Destructor: ~Set
* Usage: delete sp;
* -----------------
* The destructor deallocates storage associated with set.
*/
~Set();
/*
* Method: size
* Usage: count = set.size();
* --------------------------
* This method returns the number of elements in this set.
*/
int size();
/*
* Method: isEmpty
* Usage: if (set.isEmpty())...
* ----------------------------
* This method returns true if this set contains no
* elements, false otherwise.
*/
bool isEmpty();
/*
* Method: add
* Usage: set.add(value);
* ----------------------
* This method adds an element to this set. If the
* value was already contained in the set, the existing entry is
* overwritten by the new copy, and the set's size is unchanged.
* Otherwise, the value is added and set's size increases by one.
*/
void add(ElemType elem);
/*
* Method: remove
* Usage: set.remove(value);
* -----------------------
* This method removes an element from this set. If the
* element was not contained in the set, the set is unchanged.
* Otherwise, the element is removed and the set's size decreases
* by one.
*/
void remove(ElemType elem);
/*
* Method: contains
* Usage: if (set.contains(value))...
* -----------------------------------
* Returns true if the element in this set, false otherwise.
*/
bool contains(ElemType elem);
/*
* Method: find
* Usage: eptr = set.find(elem);
* -----------------------------
* If the element is contained in this set, returns a pointer
* to that elem. The pointer allows you to update that element
* in place. If element is not contained in this set, NULL is
* returned.
*/
ElemType *find(ElemType elem);
/*
* Method: equals
* Usage: if (set.equals(set2)) . . .
* -----------------------------------
* This predicate function implements the equality relation
* on sets. It returns true if this set and set2 contain
* exactly the same elements, false otherwise.
*/
bool equals(Set & otherSet);
/*
* Method: isSubsetOf
* Usage: if (set.isSubsetOf(set2)) . . .
* --------------------------------------
* This predicate function implements the subset relation
* on sets. It returns true if all of the elements in this
* set are contained in set2. The set2 does not have to
* be a proper subset (that is, it may be equals).
*/
bool isSubsetOf(Set & otherSet);
/*
* Methods: unionWith, intersectWith, subtract
* Usage: set.unionWith(set2);
* set.intersectWith(set2);
* set.subtract(set2);
* -------------------------------
* These fmember unctions modify the receiver set as follows:
*
* set.unionWith(set2); Adds all elements from set2 to this set.
* set.intersectWith(set2); Removes any element not in set2 from this set.
* set.subtract(set2); Removes all element in set2 from this set.
*/
void unionWith(Set & otherSet);
void intersectWith(Set & otherSet);
void subtract(Set & otherSet);
/*
* Method: clear
* Usage: set.clear();
* -------------------
* This method removes all elements from this set. The
* set is made empty and will have size() = 0 after being cleared.
*/
void clear();
/*
* SPECIAL NOTE: mapping/iteration support
* ---------------------------------------
* The set supports both a mapping operation and an iterator which
* allow the client access to all elements one by one. In general,
* these are intended for _viewing_ elements and can behave
* unpredictably if you attempt to modify the set's contents during
* mapping/iteration.
*/
/*
* Method: mapAll
* Usage: set.mapAll(Print);
* -------------------------
* This method iterates through this set's contents
* and calls the function fn once for each element.
*/
void mapAll(void (*fn)(ElemType elem));
/*
* Method: mapAll
* Usage: set.mapAll(PrintToFile, outputStream);
* --------------------------------------------
* This method iterates through this set's contents
* and calls the function fn once for each element, passing
* the element and the client's data. That data can be of whatever
* type is needed for the client's callback.
*/
template <typename ClientDataType>
void mapAll(void (*fn)(ElemType elem, ClientDataType & data),
ClientDataType & data);
/*
* Method: iterator
* Usage: iter = set.iterator();
* -----------------------------
* This method creates an iterator that allows the client to
* iterate through the elements in this set. The elements are
* returned in the order determined by the comparison function.
*
* The idiomatic code for accessing elements using an iterator is
* to create the iterator from the collection and then enter a loop
* that calls next() while hasNext() is true, like this:
*
* Set<int>::Iterator iter = set.iterator();
* while (iter.hasNext()) {
* int value = iter.next();
* . . .
* }
*
* This pattern can be abbreviated to the following more readable form:
*
* foreach (int value in set) {
* . . .
* }
*
* To avoid exposing the details of the class, the definition of the
* Iterator class itself appears in the private/set.h file.
*/
Iterator iterator();
private:
#include "private/set.h"
};
#include "private/set.cpp"
#endif
这是哪里出错了
最佳答案
问题是循环依赖。 set.h 包含 set.cpp,set.cpp 包含 set.h。
请记住,包含文件只是粘贴其代码。 set.cpp 不需要知道 set.h 因为它们在编译时将成为一个文件。
此外,您不应将 set.cpp 称为 cpp 文件。 cpp 文件是用来生成目标文件的文件。必须为每个单独的类型参数重新编译模板类的实现,因此它必须在 header 中并且不能形成单独的对象。将实现与声明分开是可以的,但在 set_implementation.h 之类的文件中进行以避免混淆。
关于c++ - 错误 C2995 : function template has already been defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11094310/
main.cpp #include "Primes.h" #include int main(){ std::string choose; int num1, num2; w
似乎函数 qwertyInches() 应该可以工作但是当我在 main() 中调用它时它给了我 [Error] called object 'qwertyInches' is not a funct
我无法理解 C++ 语法的工作原理。 #include using namespace std; class Accumulator{ private: int value; public:
在 类中声明 函数成员时,我们可以同时执行这两种操作; Function first; Function() second; 它们之间有什么区别? 最佳答案 Function 代表任意函数: void
“colonna”怎么可能是一个简单的字符串: $('td.' + colonna).css('background-color','#ffddaa'); 可以正确突出显示有趣单元格的背景,并且: $
我正在尝试将网页中的动态参数中继到函数中,然后函数将它们传递给函数内部的调用。比如下面这个简化的代码片段,现在这样,直接传入参数是没有问题的。但是,如何在不为每个可能的 colorbox 参数设置 s
C++ 中是否有一种模式允许您返回一个函数,它返回一个函数本身。例如 std::function func = ...; do { func = func(); } while (func);
我正在将 Windows 程序集移植到 Linux。我有一些代码要移植。我实际上是 linux 中 C 的新手。我知道 C 基础知识是一样的! typedef struct sReader {
我一直在寻找一个很好的解释,所以我很清楚。示例: this.onDeleteHandler(index)}/> 对比 对比 this.nameChangedhandler(event, perso
function(){}.__proto__ === Function.prototype 和 Function.prototype === function(){}.__proto__ 得到不同的结
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function 据说 Propert
VBA 中的函数没有特殊类型。我很难理解如何在 Excel VBA 中将函数作为参数添加到函数中。 我想要完成的是这样的事情: function f(g as function, x as strin
所以我正在尝试制作一个包(我没有在下面包含我的 roxygen2 header ): 我有这个功能: date_from_text % dplyr::mutate(!!name := lubr
尝试从 std::function 派生一个类,对于初学者来说,继承构造函数。这是我的猜测: #include #include using namespace std; template cla
我正在尝试编写一个返回另一个函数的函数。我的目标是编写一个函数,它接受一个对象并返回另一个函数“search”。当我使用键调用搜索函数时,我想从第一个函数中给定的对象返回该键的值。 propertyO
我非常清楚函数式编程技术和命令式编程技术之间的区别。但是现在有一种普遍的趋势是谈论“函数式语言”,这确实让我感到困惑。 当然,像 Haskell 这样的一些语言比 C 等其他语言更欢迎函数式编程。但即
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我在stackoverflow上查过很多类似的问题,比如call.call 1 , call.call 2 ,但我是新人,无法发表任何评论。我希望我能找到关于 JavaScript 解释器如何执行这些
向 Twilio 发送 SMS 时,Twilio 会向指定的 URL 发送多个请求,以通过 Webhook 提供该 SMS 传送的状态。我想让这个回调异步,所以我开发了一个 Cloud Functio
作为 IaC 的一部分,A 功能应用 ,让我们将其命名为 FuncAppX 是使用 Terraform 部署的,它有一个内置函数。 我需要使用 Terraform 在函数应用程序中访问相同函数的 Ur
我是一名优秀的程序员,十分优秀!