我正在尝试为我的游戏实现一个实体组件系统(ECS)。我有一个基类“ Component”(在此称为A),该子类由子类(如HealthComponent(在此称为B),DamageComponent(在此称为C))继承。
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
class A
{
public:
A() : VarA(10){}
int VarA;
const std::string ClassName = "A";
virtual void prnt(){ std::cout << "A class" << std::endl; }
};
class B : public A
{
public:
B() : VarB(20){}
int VarB;
const std::string ClassName = "B";
void prnt(){ std::cout << "B class" << std::endl; }
bool operator== (const B& other) const
{
return this->ClassName == other.ClassName;
}
};
class C : public A
{
public:
C() : VarC(30){}
int VarC;
const std::string ClassName = "C";
void prnt(){ std::cout << "C class" << std::endl; }
bool operator== (const B& other) const
{
return this->ClassName == other.ClassName;
}
};
int main()
{
std::vector<std::unique_ptr<A>> ObjVector;
std::vector<std::unique_ptr<A>>::iterator ObjIterator;
A* object1 = new B();
std::unique_ptr<A> bptr{object1};
ObjVector.emplace_back(std::move(bptr));
A* object2 = new C();
std::unique_ptr<A> cptr{object2};
ObjVector.emplace_back(std::move(cptr));
ObjIterator = std::find(ObjVector.begin(), ObjVector.end(), B);
return 0;
}
关于编译代码
-------------- Build: Debug in STL (compiler: GNU GCC Compiler)---------------
x86_64-w64-mingw32-g++.exe -Wall -g -std=c++11 -c C:\Users\admin\Desktop\code\C++\STL\main.cpp -o obj\Debug\main.o
x86_64-w64-mingw32-g++.exe -o bin\Debug\STL.exe obj\Debug\main.o
C:\Users\admin\Desktop\code\C++\STL\main.cpp: In function 'int main()':
C:\Users\admin\Desktop\code\C++\STL\main.cpp:58:66: error: expected primary-expression before ')' token
58 | ObjIterator = std::find(ObjVector.begin(), ObjVector.end(), B);
| ^
我尝试使用“ new B()”和“(&B)”作为std :: find函数中的最后一个参数,但它仍然给我同样的错误。请帮忙。