Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::可以为我们自己的类找到函数吗?_C++ - Fatal编程技术网

C++ std::可以为我们自己的类找到函数吗?

C++ std::可以为我们自己的类找到函数吗?,c++,C++,我有一套课程。每个类都从另一个类继承。关系如下。(我只是发布了一个类是如何从另一个类继承而来的,只是想让大家都知道) class LineNumberList:public MyVector//顶级 类MyVector:public std::vector 类行号:公共元素号 class ElementNumber{//这是基类 受保护的: 整数; 公众: ElementNumber(int p){number=p;} //更多代码// } 现在,我想实现一个函数,它可以用来查找topclas

我有一套课程。每个类都从另一个类继承。关系如下。(我只是发布了一个类是如何从另一个类继承而来的,只是想让大家都知道)

class LineNumberList:public MyVector//顶级
类MyVector:public std::vector
类行号:公共元素号
class ElementNumber{//这是基类
受保护的:
整数;
公众:
ElementNumber(int p){number=p;}
//更多代码//
}
现在,我想实现一个函数,它可以用来
查找topclass中的
元素,即
LineNumberList
。我试过使用标准的
find
函数,但它不起作用。非常感谢有人能帮我为我的案例实现类似的查找功能。

“我尝试了标准查找功能,但不起作用。”适用于我:

#include <vector>
#include <iostream>
#include <algorithm>

class ElementNumber {
  protected:
    int number;
  public:
    ElementNumber(int p) :number(p) {}
    bool operator==(const ElementNumber&e) { return number == e.number; }
};
class LineNumber : public ElementNumber {
  public:
    LineNumber(int p) : ElementNumber(p) {}
};
template <class Type>
class MyVector : public std::vector<Type> {
};
class LineNumberList : public MyVector<LineNumber> {
};

// EDIT: add local implementation of std::find
template<class InputIterator, class T>
InputIterator myfind ( InputIterator first, InputIterator last, const T& value )
{
  http://www.cplusplus.com/reference/algorithm/find/
  for ( ;first!=last; first++) if ( *first==value ) break;
  return first;
}

int main() {
  LineNumberList ll;
  LineNumber l(7);
  ll.push_back(l);
  std::cout << std::boolalpha << !(std::find(ll.begin(), ll.end(), l) == ll.end()) << "\n";
  std::cout << std::boolalpha << !(::myfind(ll.begin(), ll.end(), l) == ll.end()) << "\n";
}
#包括
#包括
#包括
类元素号{
受保护的:
整数;
公众:
元素编号(int p):编号(p){}
bool运算符==(const ElementNumber&e){return number==e.number;}
};
类行号:公共元素号{
公众:
行号(int p):元素号(p){}
};
模板
类MyVector:public std::vector{
};
类LineNumberList:公共MyVector{
};
//编辑:添加std::find的本地实现
模板
输入计算器myfind(输入计算器第一个、输入计算器最后一个、常量T和值)
{
http://www.cplusplus.com/reference/algorithm/find/
对于(;first!=last;first++)如果(*first==value)中断;
先返回;
}
int main(){
LineNumberList ll;
行号l(7);
l.向后推(l);
std::cout“我试过使用标准的查找函数,但它不起作用。”对我有用:

#include <vector>
#include <iostream>
#include <algorithm>

class ElementNumber {
  protected:
    int number;
  public:
    ElementNumber(int p) :number(p) {}
    bool operator==(const ElementNumber&e) { return number == e.number; }
};
class LineNumber : public ElementNumber {
  public:
    LineNumber(int p) : ElementNumber(p) {}
};
template <class Type>
class MyVector : public std::vector<Type> {
};
class LineNumberList : public MyVector<LineNumber> {
};

// EDIT: add local implementation of std::find
template<class InputIterator, class T>
InputIterator myfind ( InputIterator first, InputIterator last, const T& value )
{
  http://www.cplusplus.com/reference/algorithm/find/
  for ( ;first!=last; first++) if ( *first==value ) break;
  return first;
}

int main() {
  LineNumberList ll;
  LineNumber l(7);
  ll.push_back(l);
  std::cout << std::boolalpha << !(std::find(ll.begin(), ll.end(), l) == ll.end()) << "\n";
  std::cout << std::boolalpha << !(::myfind(ll.begin(), ll.end(), l) == ll.end()) << "\n";
}
#包括
#包括
#包括
类元素号{
受保护的:
整数;
公众:
元素编号(int p):编号(p){}
bool运算符==(const ElementNumber&e){return number==e.number;}
};
类行号:公共元素号{
公众:
行号(int p):元素号(p){}
};
模板
类MyVector:public std::vector{
};
类LineNumberList:公共MyVector{
};
//编辑:添加std::find的本地实现
模板
输入计算器myfind(输入计算器第一个、输入计算器最后一个、常量T和值)
{
http://www.cplusplus.com/reference/algorithm/find/
对于(;first!=last;first++)如果(*first==value)中断;
先返回;
}
int main(){
LineNumberList ll;
行号l(7);
l.向后推(l);

std::cout实际的代码是什么?这不可能是你的代码。
LineNumber
MyVector
Type
、和
LineNumberList
?你为什么要从
std::vector
派生出来,因为这里的组合似乎是更好的选择?我忍不住想到你你设计这个太过分了。你想做什么?你试过std::find_if()吗。另外,从vector派生而不是将其用作成员变量也是很奇怪的……您几乎肯定不想这样做。
vector
不是设计为或打算用作基类的,这样使用会带来麻烦。要小心从没有明确设计为超类的类继承(即标准::向量)。它有一个非虚拟析构函数。实际的代码是什么?这不可能是你所拥有的。
LineNumber
MyVector
Type
、和
LineNumberList
?你为什么要从
std::vector
派生出来,因为合成似乎是这里更好的选择?我不能这样做lp但想想你设计得太过火了。你想做什么?你试过std::find_if()吗。另外,从vector派生而不是将其用作成员变量也是很奇怪的……您几乎肯定不想这样做。
vector
不是设计为或打算用作基类的,这样使用会带来麻烦。要小心从没有明确设计为超类的类继承(即std::vector)。它有一个非虚拟析构函数。@rob,谢谢你的回答。但是,我想了解如何为我们自己的类实现std::find而不是调用该函数。有没有可能做到这一点,先生?@g_niro:“而不是调用该函数”不是调用什么函数?他直接演示了如何使用类类型调用
std::find
。@g_niro,为什么不想调用
std::find
?(严肃地说,不要像这样使用继承,除非你真的知道你在做什么,否则避免在C++中继承。模板和对象组合几乎总是提出更好的选择。)@g_niro-我一定误解了你的问题。如果你想自己实现std::find,请参阅我的编辑。@rob,谢谢你的回答。但是,我想了解如何为我们自己的类实现std::find而不是调用该函数。有没有可能这样做,先生?@g_niro:“而不是调用该函数”不是调用什么函数?他直接演示了如何使用类类型调用
std::find
。@g_niro,为什么不想调用
std::find
?(严肃地说,不要使用这样的继承,除非你真的知道你在做什么,否则不要在C++中继承继承。模板和对象合成几乎总是提出更好的选择。)GyNIRO-我一定误解了你的问题。如果你想实现STD::你自己去找,看看我的编辑。