我想知道为什么我不能将STL映射用于用户定义的类.当我编译下面的代码时,我得到这个神秘的错误消息.这是什么意思?此外,为什么它只发生在用户定义的类型?(当用于密钥时,原始类型是可以的)
C:\ MinGW\bin ..\lib\gcc\mingw32\3.4.5 ........\include\c ++\3.4.5\bits\stl_function.h ||在成员函数`bool std :: less <_Tp> :: operator()(const _Tp&,const _Tp&)const [with _Tp = Class1]':
C:\ MinGW\bin ..\lib\gcc\mingw32\3.4.5 ........\include\c ++\3.4.5\bits\stl_map.h | 338 |从`_Tp&std ::实例化map <_Key,_Tp,_Compare,_Alloc> :: operator [](const _Key&)[with _Key = Class1,_Tp = int,_Compare = std :: less,_Alloc = std :: allocator>]'|
C:\ Users\Admin\Documents\dev\sandbox\sandbox\sandbox.cpp | 24 |从这里实例化|
C:\ MinGW\bin ..\lib\gcc\mingw32\3.4.5 ........\include\c ++\3.4.5\bits\stl_function.h | 227 |错误:不匹配'运算符<'in'__ x <__y'| || ===构建完成:1个错误,0个警告=== |
#include <iostream>
#include <map>
using namespace std;
class Class1
{
public:
Class1(int id);
private:
int id; …
Run Code Online (Sandbox Code Playgroud) 在C或C++中是否存在锯齿状数组?
当我编译这个:
int jagged[][] = { {0,1}, {1,2,3} };
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误:声明`jagged'作为多维数组必须具有除第一个之外的所有维度的边界
我有一个名为vertex的结构,我创建了一些指向它们的指针.我想要做的是将这些指针添加到列表中.下面的代码,当它试图将指针插入列表时,会产生分段错误.有人可以解释一下发生了什么吗?
#include <iostream>
#include <list>
#define NUM_VERTICES 8
using namespace std;
enum { WHITE, GRAY, BLACK };
struct vertex
{
int color;
int distance;
char parent;
};
int main()
{
//create the vertices
vertex r = {WHITE, NULL, NULL};
//create pointer to the vertex structures
vertex *pr = &r;
//create a list to hold the vertices
list<vertex*> *r_list = new list<vertex*>;
list<vertex*>::iterator it;
r_list->insert(it, pr);
}
Run Code Online (Sandbox Code Playgroud)